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.
17 #include "debugtools.h"
19 #include "shell32_main.h"
21 DEFAULT_DEBUG_CHANNEL(shell);
23 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
24 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
25 static ULONG WINAPI IStream_fnRelease(IStream *iface);
26 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
27 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
28 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
29 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
30 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
31 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
32 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
33 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
34 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
35 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
36 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
38 static ICOM_VTABLE(IStream) stvt =
40 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
41 IStream_fnQueryInterface,
52 IStream_fnUnlockRegion,
59 { ICOM_VTABLE(IStream) *lpvtst;
67 /**************************************************************************
68 * CreateStreamOnFile()
70 * similar to CreateStreamOnHGlobal
72 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
76 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
79 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
82 fstr->dwLength = GetFileSize (hFile, NULL);
86 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
88 WARN("failed to create filemap.\n");
92 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
94 WARN("failed to mmap filemap.\n");
101 end_3: CloseHandle(fstr->hMapping);
102 end_2: HeapFree(GetProcessHeap(), 0, fstr);
105 end_1: _lclose(hFile);
106 (*ppstm) = (IStream*)fstr;
110 /**************************************************************************
111 * IStream_fnQueryInterface
113 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
115 ICOM_THIS(ISHFileStream, iface);
117 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
121 if(IsEqualIID(riid, &IID_IUnknown) ||
122 IsEqualIID(riid, &IID_IStream))
129 IStream_AddRef((IStream*)*ppvObj);
130 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
133 TRACE("-- Interface: E_NOINTERFACE\n");
134 return E_NOINTERFACE;
137 /**************************************************************************
140 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
142 ICOM_THIS(ISHFileStream, iface);
144 TRACE("(%p)->(count=%lu)\n",This, This->ref);
147 return ++(This->ref);
150 /**************************************************************************
153 static ULONG WINAPI IStream_fnRelease(IStream *iface)
155 ICOM_THIS(ISHFileStream, iface);
157 TRACE("(%p)->()\n",This);
162 { TRACE(" destroying SHFileStream (%p)\n",This);
164 UnmapViewOfFile(This->pImage);
165 CloseHandle(This->hMapping);
167 HeapFree(GetProcessHeap(),0,This);
173 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
175 ICOM_THIS(ISHFileStream, iface);
177 DWORD dwBytesToRead, dwBytesLeft;
179 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
182 return STG_E_INVALIDPOINTER;
184 dwBytesLeft = This->dwLength - This->dwPos;
186 if ( 0 >= dwBytesLeft ) /* end of buffer */
189 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
191 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
193 This->dwPos += dwBytesToRead; /* adjust pointer */
196 *pcbRead = dwBytesToRead;
200 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
202 ICOM_THIS(ISHFileStream, iface);
204 TRACE("(%p)\n",This);
208 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
210 ICOM_THIS(ISHFileStream, iface);
212 TRACE("(%p)\n",This);
216 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
218 ICOM_THIS(ISHFileStream, iface);
220 TRACE("(%p)\n",This);
224 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
226 ICOM_THIS(ISHFileStream, iface);
228 TRACE("(%p)\n",This);
232 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
234 ICOM_THIS(ISHFileStream, iface);
236 TRACE("(%p)\n",This);
240 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
242 ICOM_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 ICOM_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 ICOM_THIS(ISHFileStream, iface);
260 TRACE("(%p)\n",This);
264 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
266 ICOM_THIS(ISHFileStream, iface);
268 TRACE("(%p)\n",This);
272 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
274 ICOM_THIS(ISHFileStream, iface);
276 TRACE("(%p)\n",This);