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 HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
46 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
47 static ULONG WINAPI IStream_fnRelease(IStream *iface);
48 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
49 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
50 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
51 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
52 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
53 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
54 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
55 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
56 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
57 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
58 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
60 static IStreamVtbl stvt =
62 IStream_fnQueryInterface,
73 IStream_fnUnlockRegion,
80 { IStreamVtbl *lpvtst;
85 /**************************************************************************
86 * CreateStreamOnFile()
88 * similar to CreateStreamOnHGlobal
90 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
94 DWORD access = GENERIC_READ, creat;
96 if( grfMode & STGM_TRANSACTED )
99 if( grfMode & STGM_WRITE )
100 access |= GENERIC_WRITE;
101 if( grfMode & STGM_READWRITE )
102 access = GENERIC_WRITE | GENERIC_READ;
104 if( grfMode & STGM_CREATE )
105 creat = CREATE_ALWAYS;
107 creat = OPEN_EXISTING;
109 TRACE("Opening %s\n", debugstr_w(pszFilename) );
111 handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
112 if( handle == INVALID_HANDLE_VALUE )
115 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
116 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
121 fstr->handle = handle;
123 (*ppstm) = (IStream*)fstr;
128 /**************************************************************************
129 * IStream_fnQueryInterface
131 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
133 ISHFileStream *This = (ISHFileStream *)iface;
135 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
139 if(IsEqualIID(riid, &IID_IUnknown) ||
140 IsEqualIID(riid, &IID_IStream))
147 IStream_AddRef((IStream*)*ppvObj);
148 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
151 TRACE("-- Interface: E_NOINTERFACE\n");
152 return E_NOINTERFACE;
155 /**************************************************************************
158 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
160 ISHFileStream *This = (ISHFileStream *)iface;
162 TRACE("(%p)->(count=%lu)\n",This, This->ref);
164 return ++(This->ref);
167 /**************************************************************************
170 static ULONG WINAPI IStream_fnRelease(IStream *iface)
172 ISHFileStream *This = (ISHFileStream *)iface;
174 TRACE("(%p)->()\n",This);
178 TRACE(" destroying SHFileStream (%p)\n",This);
179 CloseHandle(This->handle);
180 HeapFree(GetProcessHeap(),0,This);
185 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
187 ISHFileStream *This = (ISHFileStream *)iface;
189 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
192 return STG_E_INVALIDPOINTER;
194 if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
200 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
203 ISHFileStream *This = (ISHFileStream *)iface;
205 TRACE("(%p)\n",This);
208 return STG_E_INVALIDPOINTER;
210 /* WriteFile() doesn't allow to specify NULL as write count pointer */
212 pcbWritten = &dummy_count;
214 if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
220 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
222 DWORD pos, newposlo, newposhi;
224 ISHFileStream *This = (ISHFileStream *)iface;
226 TRACE("(%p)\n",This);
228 pos = dlibMove.QuadPart; /* FIXME: truncates */
230 newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
231 if( newposlo == INVALID_SET_FILE_POINTER )
234 plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
239 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
241 ISHFileStream *This = (ISHFileStream *)iface;
243 TRACE("(%p)\n",This);
245 if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
248 if( ! SetEndOfFile( This->handle ) )
253 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
255 ISHFileStream *This = (ISHFileStream *)iface;
257 TRACE("(%p)\n",This);
261 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
263 ISHFileStream *This = (ISHFileStream *)iface;
265 TRACE("(%p)\n",This);
269 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
271 ISHFileStream *This = (ISHFileStream *)iface;
273 TRACE("(%p)\n",This);
277 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
279 ISHFileStream *This = (ISHFileStream *)iface;
281 TRACE("(%p)\n",This);
285 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
287 ISHFileStream *This = (ISHFileStream *)iface;
289 TRACE("(%p)\n",This);
293 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
295 ISHFileStream *This = (ISHFileStream *)iface;
297 TRACE("(%p)\n",This);
301 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
303 ISHFileStream *This = (ISHFileStream *)iface;
305 TRACE("(%p)\n",This);