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
33 #include "wine/debug.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 { IStreamVtbl *lpVtbl;
46 /**************************************************************************
47 * IStream_fnQueryInterface
49 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
51 ISHRegStream *This = (ISHRegStream *)iface;
53 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
57 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
59 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
64 IStream_AddRef((IStream*)*ppvObj);
65 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
68 TRACE("-- Interface: E_NOINTERFACE\n");
72 /**************************************************************************
75 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
77 ISHRegStream *This = (ISHRegStream *)iface;
79 TRACE("(%p)->(count=%lu)\n",This, This->ref);
81 return InterlockedIncrement(&This->ref);
84 /**************************************************************************
87 static ULONG WINAPI IStream_fnRelease(IStream *iface)
89 ISHRegStream *This = (ISHRegStream *)iface;
91 TRACE("(%p)->()\n",This);
93 if (!InterlockedDecrement(&This->ref))
95 TRACE(" destroying SHReg IStream (%p)\n",This);
97 HeapFree(GetProcessHeap(),0,This->pbBuffer);
100 RegCloseKey(This->hKey);
102 HeapFree(GetProcessHeap(),0,This);
108 /**************************************************************************
111 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
113 ISHRegStream *This = (ISHRegStream *)iface;
115 DWORD dwBytesToRead, dwBytesLeft;
117 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
120 return STG_E_INVALIDPOINTER;
122 dwBytesLeft = This->dwLength - This->dwPos;
124 if ( 0 >= dwBytesLeft ) /* end of buffer */
127 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
129 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
131 This->dwPos += dwBytesToRead; /* adjust pointer */
134 *pcbRead = dwBytesToRead;
139 /**************************************************************************
142 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
144 ISHRegStream *This = (ISHRegStream *)iface;
146 TRACE("(%p)\n",This);
154 /**************************************************************************
157 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
159 ISHRegStream *This = (ISHRegStream *)iface;
161 TRACE("(%p)\n",This);
164 plibNewPosition->QuadPart = 0;
168 /**************************************************************************
171 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
173 ISHRegStream *This = (ISHRegStream *)iface;
175 TRACE("(%p)\n",This);
179 /**************************************************************************
182 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
184 ISHRegStream *This = (ISHRegStream *)iface;
186 TRACE("(%p)\n",This);
188 pcbRead->QuadPart = 0;
190 pcbWritten->QuadPart = 0;
194 /**************************************************************************
197 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
199 ISHRegStream *This = (ISHRegStream *)iface;
201 TRACE("(%p)\n",This);
206 /**************************************************************************
209 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
211 ISHRegStream *This = (ISHRegStream *)iface;
213 TRACE("(%p)\n",This);
218 /**************************************************************************
219 * IStream_fnLockUnlockRegion
221 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
223 ISHRegStream *This = (ISHRegStream *)iface;
225 TRACE("(%p)\n",This);
230 /*************************************************************************
233 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
235 ISHRegStream *This = (ISHRegStream *)iface;
237 TRACE("(%p)\n",This);
242 /*************************************************************************
245 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
247 ISHRegStream *This = (ISHRegStream *)iface;
249 TRACE("(%p)\n",This);
255 static struct IStreamVtbl rstvt =
257 IStream_fnQueryInterface,
267 IStream_fnLockUnlockRegion,
268 IStream_fnLockUnlockRegion,
273 /* Methods overridden by the dummy stream */
275 /**************************************************************************
276 * IStream_fnAddRefDummy
278 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
280 ISHRegStream *This = (ISHRegStream *)iface;
281 TRACE("(%p)\n", This);
285 /**************************************************************************
286 * IStream_fnReleaseDummy
288 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
290 ISHRegStream *This = (ISHRegStream *)iface;
291 TRACE("(%p)\n", This);
295 /**************************************************************************
296 * IStream_fnReadDummy
298 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
305 static struct IStreamVtbl DummyRegStreamVTable =
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 regStream->lpVtbl = &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)
377 LPBYTE lpBuff = NULL;
378 DWORD dwLength, dwType;
380 TRACE("(%p,%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);
390 HeapFree (GetProcessHeap(), 0, lpBuff);
392 RegCloseKey(hStrKey);
396 /*************************************************************************
397 * SHOpenRegStream2W [SHLWAPI.@]
399 * See SHOpenRegStream2A.
401 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
402 LPCWSTR pszValue, DWORD dwMode)
405 LPBYTE lpBuff = NULL;
406 DWORD dwLength, dwType;
408 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
409 debugstr_w(pszValue), dwMode);
411 /* Open the key, read in binary data and create stream */
412 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
413 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
414 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
415 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
416 dwType == REG_BINARY)
417 return IStream_Create(hStrKey, lpBuff, dwLength);
419 HeapFree (GetProcessHeap(), 0, lpBuff);
421 RegCloseKey(hStrKey);
425 /*************************************************************************
426 * SHOpenRegStreamA [SHLWAPI.@]
428 * Create a stream to read binary registry data.
431 * hKey [I] Registry handle
432 * pszSubkey [I] The sub key name
433 * pszValue [I] The value name under the sub key
434 * dwMode [I] STGM mode for opening the file
437 * Success: An IStream interface referring to the registry data
438 * Failure: If the registry key could not be opened or is not binary,
439 * A dummy (empty) IStream object is returned.
441 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
442 LPCSTR pszValue, DWORD dwMode)
446 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
448 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
449 return iStream ? iStream : (IStream *)&rsDummyRegStream;
452 /*************************************************************************
453 * SHOpenRegStreamW [SHLWAPI.@]
455 * See SHOpenRegStreamA.
457 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
458 LPCWSTR pszValue, DWORD dwMode)
462 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
463 debugstr_w(pszValue), dwMode);
464 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
465 return iStream ? iStream : (IStream *)&rsDummyRegStream;
468 /*************************************************************************
471 * Create an IStream object on a block of memory.
474 * lpbData [I] Memory block to create the IStream object on
475 * dwDataLen [I] Length of data block
478 * Success: A pointer to the IStream object.
479 * Failure: NULL, if any parameters are invalid or an error occurs.
482 * A copy of the memory pointed to by lpbData is made, and is freed
483 * when the stream is released.
485 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
487 IStream *iStrmRet = NULL;
489 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
493 LPBYTE lpbDup = (LPBYTE)HeapAlloc(GetProcessHeap(), 0, dwDataLen);
497 memcpy(lpbDup, lpbData, dwDataLen);
498 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
501 HeapFree(GetProcessHeap(), 0, lpbDup);
507 /*************************************************************************
508 * SHCreateStreamWrapper [SHLWAPI.@]
510 * Create an IStream object on a block of memory.
513 * lpbData [I] Memory block to create the IStream object on
514 * dwDataLen [I] Length of data block
515 * dwReserved [I] Reserved, Must be 0.
516 * lppStream [O] Destination for IStream object
519 * Success: S_OK. lppStream contains the new IStream object.
520 * Failure: E_INVALIDARG, if any parameters are invalid,
521 * E_OUTOFMEMORY if memory allocation fails.
524 * The stream assumes ownership of the memory passed to it.
526 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
527 DWORD dwReserved, IStream **lppStream)
534 if(dwReserved || !lppStream)
537 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
540 return E_OUTOFMEMORY;
542 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
543 IStream_Release(lpStream);