2 * this class implements a pure IStream object
3 * and can be used for many purposes
5 * the main reason for implementing this was
6 * a cleaner implementation of IShellLink which
7 * needs to be able to load lnk's from a IStream
8 * interface so it was obvious to capsule the file
9 * access in a IStream to.
11 * Copyright 1999 Juergen Schmied
12 * Copyright 2003 Mike McCormack for CodeWeavers
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public
16 * License as published by the Free Software Foundation; either
17 * version 2.1 of the License, or (at your option) any later version.
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40 #include "wine/debug.h"
41 #include "shell32_main.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
45 static const IStreamVtbl stvt;
49 const IStreamVtbl *lpvtst;
54 /**************************************************************************
55 * CreateStreamOnFile()
57 * similar to CreateStreamOnHGlobal
59 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
63 DWORD access = GENERIC_READ, creat;
65 if( grfMode & STGM_TRANSACTED )
68 if( grfMode & STGM_WRITE )
69 access |= GENERIC_WRITE;
70 if( grfMode & STGM_READWRITE )
71 access = GENERIC_WRITE | GENERIC_READ;
73 if( grfMode & STGM_CREATE )
74 creat = CREATE_ALWAYS;
76 creat = OPEN_EXISTING;
78 TRACE("Opening %s\n", debugstr_w(pszFilename) );
80 handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
81 if( handle == INVALID_HANDLE_VALUE )
82 return HRESULT_FROM_WIN32(GetLastError());
84 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
85 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
90 fstr->handle = handle;
92 (*ppstm) = (IStream*)fstr;
97 /**************************************************************************
98 * IStream_fnQueryInterface
100 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
102 ISHFileStream *This = (ISHFileStream *)iface;
104 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
108 if(IsEqualIID(riid, &IID_IUnknown) ||
109 IsEqualIID(riid, &IID_IStream))
116 IStream_AddRef((IStream*)*ppvObj);
117 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
120 TRACE("-- Interface: E_NOINTERFACE\n");
121 return E_NOINTERFACE;
124 /**************************************************************************
127 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
129 ISHFileStream *This = (ISHFileStream *)iface;
130 ULONG refCount = InterlockedIncrement(&This->ref);
132 TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
137 /**************************************************************************
140 static ULONG WINAPI IStream_fnRelease(IStream *iface)
142 ISHFileStream *This = (ISHFileStream *)iface;
143 ULONG refCount = InterlockedDecrement(&This->ref);
145 TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
149 TRACE(" destroying SHFileStream (%p)\n",This);
150 CloseHandle(This->handle);
151 HeapFree(GetProcessHeap(),0,This);
156 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
158 ISHFileStream *This = (ISHFileStream *)iface;
160 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
163 return STG_E_INVALIDPOINTER;
165 if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
171 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
174 ISHFileStream *This = (ISHFileStream *)iface;
176 TRACE("(%p)\n",This);
179 return STG_E_INVALIDPOINTER;
181 /* WriteFile() doesn't allow to specify NULL as write count pointer */
183 pcbWritten = &dummy_count;
185 if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
191 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
193 DWORD pos, newposlo, newposhi;
195 ISHFileStream *This = (ISHFileStream *)iface;
197 TRACE("(%p)\n",This);
199 pos = dlibMove.QuadPart; /* FIXME: truncates */
201 newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
202 if( newposlo == INVALID_SET_FILE_POINTER )
205 plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
210 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
212 ISHFileStream *This = (ISHFileStream *)iface;
214 TRACE("(%p)\n",This);
216 if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
219 if( ! SetEndOfFile( This->handle ) )
224 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
226 ISHFileStream *This = (ISHFileStream *)iface;
228 TRACE("(%p)\n",This);
232 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
234 ISHFileStream *This = (ISHFileStream *)iface;
236 TRACE("(%p)\n",This);
240 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
242 ISHFileStream *This = (ISHFileStream *)iface;
244 TRACE("(%p)\n",This);
248 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
250 ISHFileStream *This = (ISHFileStream *)iface;
252 TRACE("(%p)\n",This);
256 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
258 ISHFileStream *This = (ISHFileStream *)iface;
260 TRACE("(%p)\n",This);
264 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
266 ISHFileStream *This = (ISHFileStream *)iface;
268 TRACE("(%p)\n",This);
272 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
274 ISHFileStream *This = (ISHFileStream *)iface;
276 TRACE("(%p)\n",This);
281 static const IStreamVtbl stvt =
283 IStream_fnQueryInterface,
293 IStream_fnLockRegion,
294 IStream_fnUnlockRegion,