Correct registering shell folders (values in Shell Folders key weren't
[wine] / dlls / shell32 / memorystream.c
1 /*
2  *      this class implements a pure IStream object
3  *      and can be used for many purposes
4  *
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.
10  *
11  * Copyright 1999 Juergen Schmied
12  * Copyright 2003 Mike McCormack for CodeWeavers
13  *
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.
18  *
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.
23  *
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
27  */
28
29 #include <stdarg.h>
30 #include <string.h>
31
32 #define COBJMACROS
33
34 #include "windef.h"
35 #include "winbase.h"
36 #include "winerror.h"
37 #include "winuser.h"
38 #include "wingdi.h"
39 #include "shlobj.h"
40 #include "wine/debug.h"
41 #include "shell32_main.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(shell);
44
45 static const IStreamVtbl stvt;
46
47 typedef struct
48 {       
49         const IStreamVtbl       *lpvtst;
50         DWORD           ref;
51         HANDLE          handle;
52 } ISHFileStream;
53
54 /**************************************************************************
55  *   CreateStreamOnFile()
56  *
57  *   similar to CreateStreamOnHGlobal
58  */
59 HRESULT CreateStreamOnFile (LPCWSTR pszFilename, DWORD grfMode, IStream ** ppstm)
60 {
61         ISHFileStream*  fstr;
62         HANDLE          handle;
63         DWORD           access = GENERIC_READ, creat;
64
65         if( grfMode & STGM_TRANSACTED )
66                 return E_INVALIDARG;
67
68         if( grfMode & STGM_WRITE )
69                 access |= GENERIC_WRITE;
70         if( grfMode & STGM_READWRITE )
71                 access = GENERIC_WRITE | GENERIC_READ;
72
73         if( grfMode & STGM_CREATE )
74                 creat = CREATE_ALWAYS;
75         else
76                 creat = OPEN_EXISTING;
77
78         TRACE("Opening %s\n", debugstr_w(pszFilename) );
79
80        handle = CreateFileW( pszFilename, access, FILE_SHARE_READ, NULL, creat, 0, NULL );
81         if( handle == INVALID_HANDLE_VALUE )
82                 return HRESULT_FROM_WIN32(GetLastError());
83
84         fstr = (ISHFileStream*)HeapAlloc(GetProcessHeap(),
85                 HEAP_ZERO_MEMORY,sizeof(ISHFileStream));
86         if( !fstr )
87                 return E_OUTOFMEMORY;
88         fstr->lpvtst=&stvt;
89         fstr->ref = 1;
90         fstr->handle = handle;
91
92         (*ppstm) = (IStream*)fstr;
93
94         return S_OK;
95 }
96
97 /**************************************************************************
98 *  IStream_fnQueryInterface
99 */
100 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
101 {
102         ISHFileStream *This = (ISHFileStream *)iface;
103
104         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
105
106         *ppvObj = NULL;
107
108         if(IsEqualIID(riid, &IID_IUnknown) ||
109            IsEqualIID(riid, &IID_IStream))
110         {
111           *ppvObj = This;
112         }
113
114         if(*ppvObj)
115         {
116           IStream_AddRef((IStream*)*ppvObj);
117           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
118           return S_OK;
119         }
120         TRACE("-- Interface: E_NOINTERFACE\n");
121         return E_NOINTERFACE;
122 }
123
124 /**************************************************************************
125 *  IStream_fnAddRef
126 */
127 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
128 {
129         ISHFileStream *This = (ISHFileStream *)iface;
130         ULONG refCount = InterlockedIncrement(&This->ref);
131
132         TRACE("(%p)->(count=%lu)\n", This, refCount - 1);
133
134         return refCount;
135 }
136
137 /**************************************************************************
138 *  IStream_fnRelease
139 */
140 static ULONG WINAPI IStream_fnRelease(IStream *iface)
141 {
142         ISHFileStream *This = (ISHFileStream *)iface;
143         ULONG refCount = InterlockedDecrement(&This->ref);
144
145         TRACE("(%p)->(count=%lu)\n", This, refCount + 1);
146
147         if (!refCount)
148         {
149                 TRACE(" destroying SHFileStream (%p)\n",This);
150                 CloseHandle(This->handle);
151                 HeapFree(GetProcessHeap(),0,This);
152         }
153         return refCount;
154 }
155
156 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
157 {
158         ISHFileStream *This = (ISHFileStream *)iface;
159
160         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
161
162         if ( !pv )
163                 return STG_E_INVALIDPOINTER;
164
165         if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
166                 return S_FALSE;
167
168         return S_OK;
169 }
170
171 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
172 {
173        DWORD dummy_count;
174         ISHFileStream *This = (ISHFileStream *)iface;
175
176         TRACE("(%p)\n",This);
177
178         if( !pv )
179                 return STG_E_INVALIDPOINTER;
180
181        /* WriteFile() doesn't allow to specify NULL as write count pointer */
182        if (!pcbWritten)
183                pcbWritten = &dummy_count;
184
185         if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
186                 return E_FAIL;
187
188         return S_OK;
189 }
190
191 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
192 {
193         DWORD pos, newposlo, newposhi;
194
195         ISHFileStream *This = (ISHFileStream *)iface;
196
197         TRACE("(%p)\n",This);
198
199         pos = dlibMove.QuadPart; /* FIXME: truncates */
200         newposhi = 0;
201         newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
202         if( newposlo == INVALID_SET_FILE_POINTER )
203                 return E_FAIL;
204
205         plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
206
207         return S_OK;
208 }
209
210 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
211 {
212         ISHFileStream *This = (ISHFileStream *)iface;
213
214         TRACE("(%p)\n",This);
215
216         if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
217                 return E_FAIL;
218
219         if( ! SetEndOfFile( This->handle ) )
220                 return E_FAIL;
221
222         return S_OK;
223 }
224 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
225 {
226         ISHFileStream *This = (ISHFileStream *)iface;
227
228         TRACE("(%p)\n",This);
229
230         return E_NOTIMPL;
231 }
232 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
233 {
234         ISHFileStream *This = (ISHFileStream *)iface;
235
236         TRACE("(%p)\n",This);
237
238         return E_NOTIMPL;
239 }
240 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
241 {
242         ISHFileStream *This = (ISHFileStream *)iface;
243
244         TRACE("(%p)\n",This);
245
246         return E_NOTIMPL;
247 }
248 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
249 {
250         ISHFileStream *This = (ISHFileStream *)iface;
251
252         TRACE("(%p)\n",This);
253
254         return E_NOTIMPL;
255 }
256 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
257 {
258         ISHFileStream *This = (ISHFileStream *)iface;
259
260         TRACE("(%p)\n",This);
261
262         return E_NOTIMPL;
263 }
264 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
265 {
266         ISHFileStream *This = (ISHFileStream *)iface;
267
268         TRACE("(%p)\n",This);
269
270         return E_NOTIMPL;
271 }
272 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
273 {
274         ISHFileStream *This = (ISHFileStream *)iface;
275
276         TRACE("(%p)\n",This);
277
278         return E_NOTIMPL;
279 }
280
281 static const IStreamVtbl stvt =
282 {
283         IStream_fnQueryInterface,
284         IStream_fnAddRef,
285         IStream_fnRelease,
286         IStream_fnRead,
287         IStream_fnWrite,
288         IStream_fnSeek,
289         IStream_fnSetSize,
290         IStream_fnCopyTo,
291         IStream_fnCommit,
292         IStream_fnRevert,
293         IStream_fnLockRegion,
294         IStream_fnUnlockRegion,
295         IStream_fnStat,
296         IStream_fnClone
297
298 };