Added debugstr_guid function and used it to replace
[wine] / dlls / shell32 / regstream.c
1 /*
2  *      SHRegOpenStream
3  */
4 #include <string.h>
5
6 #include "wine/obj_storage.h"
7
8 #include "debugtools.h"
9 #include "heap.h"
10 #include "winerror.h"
11 #include "winreg.h"
12
13 #include "shell32_main.h"
14
15 DEFAULT_DEBUG_CHANNEL(shell)
16
17 typedef struct 
18 {       ICOM_VFIELD(IStream);
19         DWORD           ref;
20         HKEY            hKey;
21         LPSTR           pszSubKey;
22         LPSTR           pszValue;
23         LPBYTE          pbBuffer;
24         DWORD           dwLength;
25         DWORD           dwPos;
26 } ISHRegStream;
27
28 static struct ICOM_VTABLE(IStream) rstvt;
29
30 /**************************************************************************
31 *   IStream_Constructor()
32 */
33 IStream *IStream_Constructor(HKEY hKey, LPCSTR pszSubKey, LPCSTR pszValue, DWORD grfMode)
34 {       ISHRegStream*   rstr;
35         DWORD           dwType;
36         
37         rstr = (ISHRegStream*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ISHRegStream));
38         ICOM_VTBL(rstr)=&rstvt;
39         rstr->ref = 1;
40
41         if ( ERROR_SUCCESS == RegOpenKeyExA (hKey, pszSubKey, 0, KEY_READ, &(rstr->hKey)))
42         { if ( ERROR_SUCCESS == RegQueryValueExA(rstr->hKey, (LPSTR)pszValue,0,0,0,&(rstr->dwLength)))
43           { 
44             /* read the binary data into the buffer */
45             rstr->pbBuffer = HeapAlloc(GetProcessHeap(),0,rstr->dwLength);
46             if (rstr->pbBuffer)
47             { if ( ERROR_SUCCESS == RegQueryValueExA(rstr->hKey, (LPSTR)pszValue,0,&dwType,rstr->pbBuffer,&(rstr->dwLength)))
48               { if (dwType == REG_BINARY )
49                 { rstr->pszSubKey = HEAP_strdupA (GetProcessHeap(),0, pszSubKey);
50                   rstr->pszValue = HEAP_strdupA (GetProcessHeap(),0, pszValue);         
51                   TRACE("(%p)->0x%08x,%s,%s,0x%08lx\n", rstr, hKey, pszSubKey, pszValue, grfMode);
52                   shell32_ObjCount++;
53                   return (IStream*)rstr;
54                 }
55               }
56               HeapFree (GetProcessHeap(),0,rstr->pbBuffer);
57             }
58           }
59           RegCloseKey(rstr->hKey);
60
61         }
62         HeapFree (GetProcessHeap(),0,rstr);
63         return NULL;
64 }
65
66 /**************************************************************************
67 *  IStream_fnQueryInterface
68 */
69 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
70 {
71         ICOM_THIS(ISHRegStream, iface);
72
73         TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
74
75         *ppvObj = NULL;
76
77         if(IsEqualIID(riid, &IID_IUnknown))     /*IUnknown*/
78         { *ppvObj = This; 
79         }
80         else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
81         { *ppvObj = This;
82         }   
83
84         if(*ppvObj)
85         { 
86           IStream_AddRef((IStream*)*ppvObj);      
87           TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
88           return S_OK;
89         }
90         TRACE("-- Interface: E_NOINTERFACE\n");
91         return E_NOINTERFACE;
92 }
93
94 /**************************************************************************
95 *  IStream_fnAddRef
96 */
97 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
98 {
99         ICOM_THIS(ISHRegStream, iface);
100
101         TRACE("(%p)->(count=%lu)\n",This, This->ref);
102
103         shell32_ObjCount++;
104         return ++(This->ref);
105 }
106
107 /**************************************************************************
108 *  IStream_fnRelease
109 */
110 static ULONG WINAPI IStream_fnRelease(IStream *iface)
111 {
112         ICOM_THIS(ISHRegStream, iface);
113
114         TRACE("(%p)->()\n",This);
115
116         shell32_ObjCount--;
117
118         if (!--(This->ref)) 
119         { TRACE(" destroying SHReg IStream (%p)\n",This);
120
121           if (This->pszSubKey)
122             HeapFree(GetProcessHeap(),0,This->pszSubKey);
123
124           if (This->pszValue)
125             HeapFree(GetProcessHeap(),0,This->pszValue);
126
127           if (This->pbBuffer)
128             HeapFree(GetProcessHeap(),0,This->pbBuffer);
129
130           if (This->hKey)
131             RegCloseKey(This->hKey);
132
133           HeapFree(GetProcessHeap(),0,This);
134           return 0;
135         }
136         return This->ref;
137 }
138
139 HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
140 {
141         ICOM_THIS(ISHRegStream, iface);
142
143         DWORD dwBytesToRead, dwBytesLeft;
144         
145         TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
146         
147         if ( !pv )
148           return STG_E_INVALIDPOINTER;
149           
150         dwBytesLeft = This->dwLength - This->dwPos;
151
152         if ( 0 >= dwBytesLeft )                                         /* end of buffer */
153           return S_FALSE;
154         
155         dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
156
157         memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
158         
159         This->dwPos += dwBytesToRead;                                   /* adjust pointer */
160
161         if (pcbRead)
162           *pcbRead = dwBytesToRead;
163
164         return S_OK;
165 }
166 HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
167 {
168         ICOM_THIS(ISHRegStream, iface);
169
170         TRACE("(%p)\n",This);
171
172         return E_NOTIMPL;
173 }
174 HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
175 {
176         ICOM_THIS(ISHRegStream, iface);
177
178         TRACE("(%p)\n",This);
179
180         return E_NOTIMPL;
181 }
182 HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
183 {
184         ICOM_THIS(ISHRegStream, iface);
185
186         TRACE("(%p)\n",This);
187
188         return E_NOTIMPL;
189 }
190 HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
191 {
192         ICOM_THIS(ISHRegStream, iface);
193
194         TRACE("(%p)\n",This);
195
196         return E_NOTIMPL;
197 }
198 HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
199 {
200         ICOM_THIS(ISHRegStream, iface);
201
202         TRACE("(%p)\n",This);
203
204         return E_NOTIMPL;
205 }
206 HRESULT WINAPI IStream_fnRevert (IStream * iface)
207 {
208         ICOM_THIS(ISHRegStream, iface);
209
210         TRACE("(%p)\n",This);
211
212         return E_NOTIMPL;
213 }
214 HRESULT WINAPI IStream_fnLockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
215 {
216         ICOM_THIS(ISHRegStream, iface);
217
218         TRACE("(%p)\n",This);
219
220         return E_NOTIMPL;
221 }
222 HRESULT WINAPI IStream_fnUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
223 {
224         ICOM_THIS(ISHRegStream, iface);
225
226         TRACE("(%p)\n",This);
227
228         return E_NOTIMPL;
229 }
230 HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG*   pstatstg, DWORD grfStatFlag)
231 {
232         ICOM_THIS(ISHRegStream, iface);
233
234         TRACE("(%p)\n",This);
235
236         return E_NOTIMPL;
237 }
238 HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
239 {
240         ICOM_THIS(ISHRegStream, iface);
241
242         TRACE("(%p)\n",This);
243
244         return E_NOTIMPL;
245 }
246
247 static struct ICOM_VTABLE(IStream) rstvt = 
248 {       
249         ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
250         IStream_fnQueryInterface,
251         IStream_fnAddRef,
252         IStream_fnRelease,
253         IStream_fnRead,
254         IStream_fnWrite,
255         IStream_fnSeek,
256         IStream_fnSetSize,
257         IStream_fnCopyTo,
258         IStream_fnCommit,
259         IStream_fnRevert,
260         IStream_fnLockRegion,
261         IStream_fnUnlockRegion,
262         IStream_fnStat,
263         IStream_fnClone
264         
265 };
266
267 /*************************************************************************
268  * OpenRegStream                                [SHELL32.85]
269  *
270  * NOTES
271  *     exported by ordinal
272  */
273 IStream * WINAPI OpenRegStream(HKEY hkey, LPCSTR pszSubkey, LPCSTR pszValue, DWORD grfMode)
274 {
275         TRACE("(0x%08x,%s,%s,0x%08lx)\n",hkey, pszSubkey, pszValue, grfMode);
276         return IStream_Constructor(hkey, pszSubkey, pszValue, grfMode);
277 }