2 * SHLWAPI Registry Stream functions
4 * Copyright 1999 Juergen Schmied
5 * Copyright 2002 Jon Griffiths
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "wine/obj_base.h"
28 #include "wine/obj_storage.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(shell);
35 { ICOM_VFIELD(IStream);
43 /**************************************************************************
44 * IStream_fnQueryInterface
46 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
48 ICOM_THIS(ISHRegStream, iface);
50 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
54 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
56 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
61 IStream_AddRef((IStream*)*ppvObj);
62 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
65 TRACE("-- Interface: E_NOINTERFACE\n");
69 /**************************************************************************
72 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
74 ICOM_THIS(ISHRegStream, iface);
76 TRACE("(%p)->(count=%lu)\n",This, This->ref);
78 return InterlockedIncrement(&This->ref);
81 /**************************************************************************
84 static ULONG WINAPI IStream_fnRelease(IStream *iface)
86 ICOM_THIS(ISHRegStream, iface);
88 TRACE("(%p)->()\n",This);
90 if (!InterlockedDecrement(&This->ref))
92 TRACE(" destroying SHReg IStream (%p)\n",This);
95 HeapFree(GetProcessHeap(),0,This->pbBuffer);
98 RegCloseKey(This->hKey);
100 HeapFree(GetProcessHeap(),0,This);
106 /**************************************************************************
109 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
111 ICOM_THIS(ISHRegStream, iface);
113 DWORD dwBytesToRead, dwBytesLeft;
115 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
118 return STG_E_INVALIDPOINTER;
120 dwBytesLeft = This->dwLength - This->dwPos;
122 if ( 0 >= dwBytesLeft ) /* end of buffer */
125 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
127 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
129 This->dwPos += dwBytesToRead; /* adjust pointer */
132 *pcbRead = dwBytesToRead;
137 /**************************************************************************
140 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
142 ICOM_THIS(ISHRegStream, iface);
144 TRACE("(%p)\n",This);
152 /**************************************************************************
155 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
157 ICOM_THIS(ISHRegStream, iface);
159 TRACE("(%p)\n",This);
162 plibNewPosition->QuadPart = 0;
166 /**************************************************************************
169 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
171 ICOM_THIS(ISHRegStream, iface);
173 TRACE("(%p)\n",This);
177 /**************************************************************************
180 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
182 ICOM_THIS(ISHRegStream, iface);
184 TRACE("(%p)\n",This);
186 pcbRead->QuadPart = 0;
188 pcbWritten->QuadPart = 0;
192 /**************************************************************************
195 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
197 ICOM_THIS(ISHRegStream, iface);
199 TRACE("(%p)\n",This);
204 /**************************************************************************
207 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
209 ICOM_THIS(ISHRegStream, iface);
211 TRACE("(%p)\n",This);
216 /**************************************************************************
217 * IStream_fnLockUnlockRegion
219 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
221 ICOM_THIS(ISHRegStream, iface);
223 TRACE("(%p)\n",This);
228 /*************************************************************************
231 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
233 ICOM_THIS(ISHRegStream, iface);
235 TRACE("(%p)\n",This);
240 /*************************************************************************
243 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
245 ICOM_THIS(ISHRegStream, iface);
247 TRACE("(%p)\n",This);
253 static struct ICOM_VTABLE(IStream) rstvt =
255 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
256 IStream_fnQueryInterface,
266 IStream_fnLockUnlockRegion,
267 IStream_fnLockUnlockRegion,
272 /* Methods overridden by the dummy stream */
274 /**************************************************************************
275 * IStream_fnAddRefDummy
277 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
279 ICOM_THIS(ISHRegStream, iface);
280 TRACE("(%p)\n", This);
284 /**************************************************************************
285 * IStream_fnReleaseDummy
287 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
289 ICOM_THIS(ISHRegStream, iface);
290 TRACE("(%p)\n", This);
294 /**************************************************************************
295 * IStream_fnReadDummy
297 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
304 static struct ICOM_VTABLE(IStream) DummyRegStreamVTable =
306 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
307 IStream_fnQueryInterface,
308 IStream_fnAddRefDummy, /* Overridden */
309 IStream_fnReleaseDummy, /* Overridden */
310 IStream_fnReadDummy, /* Overridden */
317 IStream_fnLockUnlockRegion,
318 IStream_fnLockUnlockRegion,
323 /* Dummy registry stream object */
324 static ISHRegStream rsDummyRegStream =
326 &DummyRegStreamVTable,
334 /**************************************************************************
337 * Internal helper: Create and initialise a new registry stream object.
339 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
341 ISHRegStream* regStream;
343 regStream = (ISHRegStream*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
347 ICOM_VTBL(regStream) = &rstvt;
349 regStream->hKey = hKey;
350 regStream->pbBuffer = pbBuffer;
351 regStream->dwLength = dwLength;
352 regStream->dwPos = 0;
354 TRACE ("Returning %p\n", regStream);
355 return (IStream *)regStream;
358 /*************************************************************************
359 * SHOpenRegStream2A [SHLWAPI.@]
361 * Create a stream to read binary registry data.
364 * hKey [I] Registry handle
365 * pszSubkey [I] The sub key name
366 * pszValue [I] The value name under the sub key
370 * Success: An IStream interface referring to the registry data
371 * Failure: NULL, if the registry key could not be opened or is not binary.
373 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
374 LPCSTR pszValue,DWORD dwMode)
376 HKEY hStrKey = (HKEY)0;
377 LPBYTE lpBuff = NULL;
378 DWORD dwLength, dwType;
380 TRACE("(0x%08x,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
382 /* Open the key, read in binary data and create stream */
383 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
384 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
385 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
386 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
387 dwType == REG_BINARY)
388 return IStream_Create(hStrKey, lpBuff, dwLength);
391 HeapFree (GetProcessHeap(), 0, lpBuff);
393 RegCloseKey(hStrKey);
397 /*************************************************************************
398 * SHOpenRegStream2W [SHLWAPI.@]
400 * See SHOpenRegStream2A.
402 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
403 LPCWSTR pszValue, DWORD dwMode)
405 HKEY hStrKey = (HKEY)0;
406 LPBYTE lpBuff = NULL;
407 DWORD dwLength, dwType;
409 TRACE("(0x%08x,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
410 debugstr_w(pszValue), dwMode);
412 /* Open the key, read in binary data and create stream */
413 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
414 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
415 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
416 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
417 dwType == REG_BINARY)
418 return IStream_Create(hStrKey, lpBuff, dwLength);
421 HeapFree (GetProcessHeap(), 0, lpBuff);
423 RegCloseKey(hStrKey);
427 /*************************************************************************
428 * SHOpenRegStreamA [SHLWAPI.@]
430 * Create a stream to read binary registry data.
433 * hKey [I] Registry handle
434 * pszSubkey [I] The sub key name
435 * pszValue [I] The value name under the sub key
436 * dwMode [I] STGM mode for opening the file
439 * Success: An IStream interface referring to the registry data
440 * Failure: If the registry key could not be opened or is not binary,
441 * A dummy (empty) IStream object is returned.
443 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
444 LPCSTR pszValue, DWORD dwMode)
448 TRACE("(0x%08x,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
450 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
451 return iStream ? iStream : (IStream *)&rsDummyRegStream;
454 /*************************************************************************
455 * SHOpenRegStreamW [SHLWAPI.@]
457 * See SHOpenRegStreamA.
459 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
460 LPCWSTR pszValue, DWORD dwMode)
464 TRACE("(0x%08x,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
465 debugstr_w(pszValue), dwMode);
466 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
467 return iStream ? iStream : (IStream *)&rsDummyRegStream;
470 /*************************************************************************
473 * Create a stream on a block of memory.
476 * lpbData [I] Memory block to create the stream on
477 * dwDataLen [I] Length of data block
480 * Success: A pointer to the stream object.
481 * Failure: NULL, if any parameters are invalid or an error occurs.
484 * A copy of the memory pointed to by lpbData is made, and is freed
485 * when the stream is released.
487 IStream * WINAPI SHLWAPI_12(LPBYTE lpbData, DWORD dwDataLen)
489 IStream *iStrmRet = NULL;
491 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
495 LPBYTE lpbDup = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
499 memcpy(lpbDup, lpbData, dwDataLen);
500 iStrmRet = IStream_Create((HKEY)0, lpbDup, dwDataLen);
503 HeapFree(GetProcessHeap(), 0, lpbDup);