Pass the main exe name in the CREATE_PROCESS debug event.
[wine.git] / dlls / shell32 / memorystream.c
blobad210022cc901bfb250626ef074908848174b93c
1 /*
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.
12 #include <string.h>
14 #include "wine/obj_storage.h"
15 #include "heap.h"
16 #include "winerror.h"
17 #include "debugtools.h"
18 #include "shell32_main.h"
20 DEFAULT_DEBUG_CHANNEL(shell)
22 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj);
23 static ULONG WINAPI IStream_fnAddRef(IStream *iface);
24 static ULONG WINAPI IStream_fnRelease(IStream *iface);
25 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead);
26 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten);
27 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition);
28 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize);
29 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten);
30 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags);
31 static HRESULT WINAPI IStream_fnRevert (IStream * iface);
32 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
33 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType);
34 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag);
35 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm);
37 static ICOM_VTABLE(IStream) stvt =
39 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
40 IStream_fnQueryInterface,
41 IStream_fnAddRef,
42 IStream_fnRelease,
43 IStream_fnRead,
44 IStream_fnWrite,
45 IStream_fnSeek,
46 IStream_fnSetSize,
47 IStream_fnCopyTo,
48 IStream_fnCommit,
49 IStream_fnRevert,
50 IStream_fnLockRegion,
51 IStream_fnUnlockRegion,
52 IStream_fnStat,
53 IStream_fnClone
57 typedef struct
58 { ICOM_VTABLE(IStream) *lpvtst;
59 DWORD ref;
60 LPBYTE pImage;
61 HANDLE hMapping;
62 DWORD dwLength;
63 DWORD dwPos;
64 } ISHFileStream;
66 /**************************************************************************
67 * CreateStreamOnFile()
69 * similar to CreateStreamOnHGlobal
71 HRESULT CreateStreamOnFile (LPCSTR pszFilename, IStream ** ppstm)
73 ISHFileStream* fstr;
74 OFSTRUCT ofs;
75 HFILE hFile = OpenFile( pszFilename, &ofs, OF_READ );
76 HRESULT ret = E_FAIL;
78 fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
79 fstr->lpvtst=&stvt;
80 fstr->ref = 1;
81 fstr->dwLength = GetFileSize (hFile, NULL);
83 shell32_ObjCount++;
85 if (!(fstr->hMapping = CreateFileMappingA(hFile,NULL,PAGE_READONLY|SEC_COMMIT,0,0,NULL)))
87 WARN("failed to create filemap.\n");
88 goto end_2;
91 if (!(fstr->pImage = MapViewOfFile(fstr->hMapping,FILE_MAP_READ,0,0,0)))
93 WARN("failed to mmap filemap.\n");
94 goto end_3;
97 ret = S_OK;
98 goto end_1;
100 end_3: CloseHandle(fstr->hMapping);
101 end_2: HeapFree(GetProcessHeap(), 0, fstr);
102 fstr = NULL;
104 end_1: _lclose(hFile);
105 (*ppstm) = (IStream*)fstr;
106 return ret;
109 /**************************************************************************
110 * IStream_fnQueryInterface
112 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
114 ICOM_THIS(ISHFileStream, iface);
116 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
118 *ppvObj = NULL;
120 if(IsEqualIID(riid, &IID_IUnknown) ||
121 IsEqualIID(riid, &IID_IStream))
123 *ppvObj = This;
126 if(*ppvObj)
128 IStream_AddRef((IStream*)*ppvObj);
129 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
130 return S_OK;
132 TRACE("-- Interface: E_NOINTERFACE\n");
133 return E_NOINTERFACE;
136 /**************************************************************************
137 * IStream_fnAddRef
139 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
141 ICOM_THIS(ISHFileStream, iface);
143 TRACE("(%p)->(count=%lu)\n",This, This->ref);
145 shell32_ObjCount++;
146 return ++(This->ref);
149 /**************************************************************************
150 * IStream_fnRelease
152 static ULONG WINAPI IStream_fnRelease(IStream *iface)
154 ICOM_THIS(ISHFileStream, iface);
156 TRACE("(%p)->()\n",This);
158 shell32_ObjCount--;
160 if (!--(This->ref))
161 { TRACE(" destroying SHFileStream (%p)\n",This);
163 UnmapViewOfFile(This->pImage);
164 CloseHandle(This->hMapping);
166 HeapFree(GetProcessHeap(),0,This);
167 return 0;
169 return This->ref;
172 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
174 ICOM_THIS(ISHFileStream, iface);
176 DWORD dwBytesToRead, dwBytesLeft;
178 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
180 if ( !pv )
181 return STG_E_INVALIDPOINTER;
183 dwBytesLeft = This->dwLength - This->dwPos;
185 if ( 0 >= dwBytesLeft ) /* end of buffer */
186 return S_FALSE;
188 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
190 memmove ( pv, (This->pImage) + (This->dwPos), dwBytesToRead);
192 This->dwPos += dwBytesToRead; /* adjust pointer */
194 if (pcbRead)
195 *pcbRead = dwBytesToRead;
197 return S_OK;
199 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
201 ICOM_THIS(ISHFileStream, iface);
203 TRACE("(%p)\n",This);
205 return E_NOTIMPL;
207 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
209 ICOM_THIS(ISHFileStream, iface);
211 TRACE("(%p)\n",This);
213 return E_NOTIMPL;
215 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
217 ICOM_THIS(ISHFileStream, iface);
219 TRACE("(%p)\n",This);
221 return E_NOTIMPL;
223 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
225 ICOM_THIS(ISHFileStream, iface);
227 TRACE("(%p)\n",This);
229 return E_NOTIMPL;
231 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
233 ICOM_THIS(ISHFileStream, iface);
235 TRACE("(%p)\n",This);
237 return E_NOTIMPL;
239 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
241 ICOM_THIS(ISHFileStream, iface);
243 TRACE("(%p)\n",This);
245 return E_NOTIMPL;
247 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
249 ICOM_THIS(ISHFileStream, iface);
251 TRACE("(%p)\n",This);
253 return E_NOTIMPL;
255 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257 ICOM_THIS(ISHFileStream, iface);
259 TRACE("(%p)\n",This);
261 return E_NOTIMPL;
263 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
265 ICOM_THIS(ISHFileStream, iface);
267 TRACE("(%p)\n",This);
269 return E_NOTIMPL;
271 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
273 ICOM_THIS(ISHFileStream, iface);
275 TRACE("(%p)\n",This);
277 return E_NOTIMPL;