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
38 #include "wine/debug.h"
39 #include "shell32_main.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(shell);
43 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
44 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
45 static ULONG WINAPI IStream_fnRelease(IStream *iface);
46 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
47 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
48 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
49 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
50 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
51 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
52 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
53 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
54 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
55 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
56 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
58 static IStreamVtbl stvt =
60 IStream_fnQueryInterface,
71 IStream_fnUnlockRegion,
78 { IStreamVtbl *lpvtst;
83 /**************************************************************************
84 * CreateStreamOnFile()
86 * similar to CreateStreamOnHGlobal
88 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
92 DWORD access = GENERIC_READ, creat;
94 if( grfMode & STGM_TRANSACTED )
97 if( grfMode & STGM_WRITE )
98 access |= GENERIC_WRITE;
99 if( grfMode & STGM_READWRITE )
100 access = GENERIC_WRITE | GENERIC_READ;
102 if( grfMode & STGM_CREATE )
103 creat = CREATE_ALWAYS;
105 creat = OPEN_EXISTING;
107 TRACE("Opening %s\n", debugstr_w(pszFilename) );
109 handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
110 if( handle == INVALID_HANDLE_VALUE )
113 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
114 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
119 fstr->handle = handle;
121 (*ppstm) = (IStream*)fstr;
126 /**************************************************************************
127 * IStream_fnQueryInterface
129 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
131 ISHFileStream *This = (ISHFileStream *)iface;
133 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
137 if(IsEqualIID(riid, &IID_IUnknown) ||
138 IsEqualIID(riid, &IID_IStream))
145 IStream_AddRef((IStream*)*ppvObj);
146 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
149 TRACE("-- Interface: E_NOINTERFACE\n");
150 return E_NOINTERFACE;
153 /**************************************************************************
156 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
158 ISHFileStream *This = (ISHFileStream *)iface;
160 TRACE("(%p)->(count=%lu)\n",This, This->ref);
162 return ++(This->ref);
165 /**************************************************************************
168 static ULONG WINAPI IStream_fnRelease(IStream *iface)
170 ISHFileStream *This = (ISHFileStream *)iface;
172 TRACE("(%p)->()\n",This);
176 TRACE(" destroying SHFileStream (%p)\n",This);
177 CloseHandle(This->handle);
178 HeapFree(GetProcessHeap(),0,This);
183 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
185 ISHFileStream *This = (ISHFileStream *)iface;
187 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
190 return STG_E_INVALIDPOINTER;
192 if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
198 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
201 ISHFileStream *This = (ISHFileStream *)iface;
203 TRACE("(%p)\n",This);
206 return STG_E_INVALIDPOINTER;
208 /* WriteFile() doesn't allow to specify NULL as write count pointer */
210 pcbWritten = &dummy_count;
212 if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
218 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
220 DWORD pos, newposlo, newposhi;
222 ISHFileStream *This = (ISHFileStream *)iface;
224 TRACE("(%p)\n",This);
226 pos = dlibMove.QuadPart; /* FIXME: truncates */
228 newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
229 if( newposlo == INVALID_SET_FILE_POINTER )
232 plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
237 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
239 ISHFileStream *This = (ISHFileStream *)iface;
241 TRACE("(%p)\n",This);
243 if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
246 if( ! SetEndOfFile( This->handle ) )
251 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
253 ISHFileStream *This = (ISHFileStream *)iface;
255 TRACE("(%p)\n",This);
259 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
261 ISHFileStream *This = (ISHFileStream *)iface;
263 TRACE("(%p)\n",This);
267 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
269 ISHFileStream *This = (ISHFileStream *)iface;
271 TRACE("(%p)\n",This);
275 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
277 ISHFileStream *This = (ISHFileStream *)iface;
279 TRACE("(%p)\n",This);
283 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
285 ISHFileStream *This = (ISHFileStream *)iface;
287 TRACE("(%p)\n",This);
291 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
293 ISHFileStream *This = (ISHFileStream *)iface;
295 TRACE("(%p)\n",This);
299 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
301 ISHFileStream *This = (ISHFileStream *)iface;
303 TRACE("(%p)\n",This);