Removed W->A from DEFWND_ImmIsUIMessageW.
[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
131         TRACE("(%p)->(count=%lu)\n",This, This->ref);
132
133         return ++(This->ref);
134 }
135
136 /**************************************************************************
137 *  IStream_fnRelease
138 */
139 static ULONG WINAPI IStream_fnRelease(IStream *iface)
140 {
141         ISHFileStream *This = (ISHFileStream *)iface;
142
143         TRACE("(%p)->()\n",This);
144
145         if (!--(This->ref))
146         {
147                 TRACE(" destroying SHFileStream (%p)\n",This);
148                 CloseHandle(This->handle);
149                 HeapFree(GetProcessHeap(),0,This);
150         }
151         return This->ref;
152 }
153
154 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
155 {
156         ISHFileStream *This = (ISHFileStream *)iface;
157
158         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
159
160         if ( !pv )
161                 return STG_E_INVALIDPOINTER;
162
163         if ( ! ReadFile( This->handle, pv, cb, pcbRead, NULL ) )
164                 return S_FALSE;
165
166         return S_OK;
167 }
168
169 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
170 {
171        DWORD dummy_count;
172         ISHFileStream *This = (ISHFileStream *)iface;
173
174         TRACE("(%p)\n",This);
175
176         if( !pv )
177                 return STG_E_INVALIDPOINTER;
178
179        /* WriteFile() doesn't allow to specify NULL as write count pointer */
180        if (!pcbWritten)
181                pcbWritten = &dummy_count;
182
183         if( ! WriteFile( This->handle, pv, cb, pcbWritten, NULL ) )
184                 return E_FAIL;
185
186         return S_OK;
187 }
188
189 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
190 {
191         DWORD pos, newposlo, newposhi;
192
193         ISHFileStream *This = (ISHFileStream *)iface;
194
195         TRACE("(%p)\n",This);
196
197         pos = dlibMove.QuadPart; /* FIXME: truncates */
198         newposhi = 0;
199         newposlo = SetFilePointer( This->handle, pos, &newposhi, dwOrigin );
200         if( newposlo == INVALID_SET_FILE_POINTER )
201                 return E_FAIL;
202
203         plibNewPosition->QuadPart = newposlo | ( (LONGLONG)newposhi<<32);
204
205         return S_OK;
206 }
207
208 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
209 {
210         ISHFileStream *This = (ISHFileStream *)iface;
211
212         TRACE("(%p)\n",This);
213
214         if( ! SetFilePointer( This->handle, libNewSize.QuadPart, NULL, FILE_BEGIN ) )
215                 return E_FAIL;
216
217         if( ! SetEndOfFile( This->handle ) )
218                 return E_FAIL;
219
220         return S_OK;
221 }
222 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
223 {
224         ISHFileStream *This = (ISHFileStream *)iface;
225
226         TRACE("(%p)\n",This);
227
228         return E_NOTIMPL;
229 }
230 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
231 {
232         ISHFileStream *This = (ISHFileStream *)iface;
233
234         TRACE("(%p)\n",This);
235
236         return E_NOTIMPL;
237 }
238 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
239 {
240         ISHFileStream *This = (ISHFileStream *)iface;
241
242         TRACE("(%p)\n",This);
243
244         return E_NOTIMPL;
245 }
246 static HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
247 {
248         ISHFileStream *This = (ISHFileStream *)iface;
249
250         TRACE("(%p)\n",This);
251
252         return E_NOTIMPL;
253 }
254 static HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
255 {
256         ISHFileStream *This = (ISHFileStream *)iface;
257
258         TRACE("(%p)\n",This);
259
260         return E_NOTIMPL;
261 }
262 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
263 {
264         ISHFileStream *This = (ISHFileStream *)iface;
265
266         TRACE("(%p)\n",This);
267
268         return E_NOTIMPL;
269 }
270 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
271 {
272         ISHFileStream *This = (ISHFileStream *)iface;
273
274         TRACE("(%p)\n",This);
275
276         return E_NOTIMPL;
277 }
278
279 static const IStreamVtbl stvt =
280 {
281         IStream_fnQueryInterface,
282         IStream_fnAddRef,
283         IStream_fnRelease,
284         IStream_fnRead,
285         IStream_fnWrite,
286         IStream_fnSeek,
287         IStream_fnSetSize,
288         IStream_fnCopyTo,
289         IStream_fnCommit,
290         IStream_fnRevert,
291         IStream_fnLockRegion,
292         IStream_fnUnlockRegion,
293         IStream_fnStat,
294         IStream_fnClone
295
296 };