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
13 * This library is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU Lesser General Public
15 * License as published by the Free Software Foundation; either
16 * version 2.1 of the License, or (at your option) any later version.
18 * This library is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * Lesser General Public License for more details.
23 * You should have received a copy of the GNU Lesser General Public
24 * License along with this library; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
33 #include "wine/debug.h"
34 #include "shell32_main.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
38 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
);
39 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
);
40 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
);
41 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
);
42 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
);
43 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
);
44 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
);
45 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
);
46 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
);
47 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
);
48 static HRESULT WINAPI
IStream_fnLockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
);
49 static HRESULT WINAPI
IStream_fnUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
);
50 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
);
51 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
);
53 static ICOM_VTABLE(IStream
) stvt
=
55 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
56 IStream_fnQueryInterface
,
67 IStream_fnUnlockRegion
,
74 { ICOM_VTABLE(IStream
) *lpvtst
;
82 /**************************************************************************
83 * CreateStreamOnFile()
85 * similar to CreateStreamOnHGlobal
87 HRESULT
CreateStreamOnFile (LPCSTR pszFilename
, IStream
** ppstm
)
91 HFILE hFile
= OpenFile( pszFilename
, &ofs
, OF_READ
);
94 fstr
= (ISHFileStream
*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY
,sizeof(ISHFileStream
));
97 fstr
->dwLength
= GetFileSize ((HANDLE
)hFile
, NULL
);
99 if (!(fstr
->hMapping
= CreateFileMappingA((HANDLE
)hFile
,NULL
,PAGE_READONLY
|SEC_COMMIT
,0,0,NULL
)))
101 WARN("failed to create filemap.\n");
105 if (!(fstr
->pImage
= MapViewOfFile(fstr
->hMapping
,FILE_MAP_READ
,0,0,0)))
107 WARN("failed to mmap filemap.\n");
114 end_3
: CloseHandle(fstr
->hMapping
);
115 end_2
: HeapFree(GetProcessHeap(), 0, fstr
);
118 end_1
: _lclose(hFile
);
119 (*ppstm
) = (IStream
*)fstr
;
123 /**************************************************************************
124 * IStream_fnQueryInterface
126 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
128 ICOM_THIS(ISHFileStream
, iface
);
130 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
134 if(IsEqualIID(riid
, &IID_IUnknown
) ||
135 IsEqualIID(riid
, &IID_IStream
))
142 IStream_AddRef((IStream
*)*ppvObj
);
143 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
146 TRACE("-- Interface: E_NOINTERFACE\n");
147 return E_NOINTERFACE
;
150 /**************************************************************************
153 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
155 ICOM_THIS(ISHFileStream
, iface
);
157 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
159 return ++(This
->ref
);
162 /**************************************************************************
165 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
167 ICOM_THIS(ISHFileStream
, iface
);
169 TRACE("(%p)->()\n",This
);
172 { TRACE(" destroying SHFileStream (%p)\n",This
);
174 UnmapViewOfFile(This
->pImage
);
175 CloseHandle(This
->hMapping
);
177 HeapFree(GetProcessHeap(),0,This
);
183 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
185 ICOM_THIS(ISHFileStream
, iface
);
187 DWORD dwBytesToRead
, dwBytesLeft
;
189 TRACE("(%p)->(%p,0x%08lx,%p)\n",This
, pv
, cb
, pcbRead
);
192 return STG_E_INVALIDPOINTER
;
194 dwBytesLeft
= This
->dwLength
- This
->dwPos
;
196 if ( 0 >= dwBytesLeft
) /* end of buffer */
199 dwBytesToRead
= ( cb
> dwBytesLeft
) ? dwBytesLeft
: cb
;
201 memmove ( pv
, (This
->pImage
) + (This
->dwPos
), dwBytesToRead
);
203 This
->dwPos
+= dwBytesToRead
; /* adjust pointer */
206 *pcbRead
= dwBytesToRead
;
210 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
212 ICOM_THIS(ISHFileStream
, iface
);
214 TRACE("(%p)\n",This
);
218 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
)
220 ICOM_THIS(ISHFileStream
, iface
);
222 TRACE("(%p)\n",This
);
226 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
)
228 ICOM_THIS(ISHFileStream
, iface
);
230 TRACE("(%p)\n",This
);
234 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
236 ICOM_THIS(ISHFileStream
, iface
);
238 TRACE("(%p)\n",This
);
242 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
)
244 ICOM_THIS(ISHFileStream
, iface
);
246 TRACE("(%p)\n",This
);
250 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
)
252 ICOM_THIS(ISHFileStream
, iface
);
254 TRACE("(%p)\n",This
);
258 static HRESULT WINAPI
IStream_fnLockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
260 ICOM_THIS(ISHFileStream
, iface
);
262 TRACE("(%p)\n",This
);
266 static HRESULT WINAPI
IStream_fnUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
268 ICOM_THIS(ISHFileStream
, iface
);
270 TRACE("(%p)\n",This
);
274 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
)
276 ICOM_THIS(ISHFileStream
, iface
);
278 TRACE("(%p)\n",This
);
282 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
)
284 ICOM_THIS(ISHFileStream
, iface
);
286 TRACE("(%p)\n",This
);