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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
40 const IStreamVtbl *lpVtbl;
48 /**************************************************************************
49 * IStream_fnQueryInterface
51 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
53 ISHRegStream *This = (ISHRegStream *)iface;
55 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
59 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
61 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
66 IStream_AddRef((IStream*)*ppvObj);
67 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
70 TRACE("-- Interface: E_NOINTERFACE\n");
74 /**************************************************************************
77 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
79 ISHRegStream *This = (ISHRegStream *)iface;
80 ULONG refCount = InterlockedIncrement(&This->ref);
82 TRACE("(%p)->(ref before=%u)\n",This, refCount - 1);
87 /**************************************************************************
90 static ULONG WINAPI IStream_fnRelease(IStream *iface)
92 ISHRegStream *This = (ISHRegStream *)iface;
93 ULONG refCount = InterlockedDecrement(&This->ref);
95 TRACE("(%p)->(ref before=%u)\n",This, refCount + 1);
99 TRACE(" destroying SHReg IStream (%p)\n",This);
101 HeapFree(GetProcessHeap(),0,This->pbBuffer);
104 RegCloseKey(This->hKey);
106 HeapFree(GetProcessHeap(),0,This);
113 /**************************************************************************
116 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
118 ISHRegStream *This = (ISHRegStream *)iface;
120 DWORD dwBytesToRead, dwBytesLeft;
122 TRACE("(%p)->(%p,0x%08x,%p)\n",This, pv, cb, pcbRead);
125 return STG_E_INVALIDPOINTER;
127 dwBytesLeft = This->dwLength - This->dwPos;
129 if ( 0 >= dwBytesLeft ) /* end of buffer */
132 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
134 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
136 This->dwPos += dwBytesToRead; /* adjust pointer */
139 *pcbRead = dwBytesToRead;
144 /**************************************************************************
147 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
149 ISHRegStream *This = (ISHRegStream *)iface;
151 TRACE("(%p)\n",This);
159 /**************************************************************************
162 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
164 ISHRegStream *This = (ISHRegStream *)iface;
166 TRACE("(%p)\n",This);
169 plibNewPosition->QuadPart = 0;
173 /**************************************************************************
176 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
178 ISHRegStream *This = (ISHRegStream *)iface;
180 TRACE("(%p)\n",This);
184 /**************************************************************************
187 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
189 ISHRegStream *This = (ISHRegStream *)iface;
191 TRACE("(%p)\n",This);
193 pcbRead->QuadPart = 0;
195 pcbWritten->QuadPart = 0;
199 /**************************************************************************
202 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
204 ISHRegStream *This = (ISHRegStream *)iface;
206 TRACE("(%p)\n",This);
211 /**************************************************************************
214 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
216 ISHRegStream *This = (ISHRegStream *)iface;
218 TRACE("(%p)\n",This);
223 /**************************************************************************
224 * IStream_fnLockUnlockRegion
226 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
228 ISHRegStream *This = (ISHRegStream *)iface;
230 TRACE("(%p)\n",This);
235 /*************************************************************************
238 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
240 ISHRegStream *This = (ISHRegStream *)iface;
242 TRACE("(%p)\n",This);
247 /*************************************************************************
250 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
252 ISHRegStream *This = (ISHRegStream *)iface;
254 TRACE("(%p)\n",This);
260 static const IStreamVtbl rstvt =
262 IStream_fnQueryInterface,
272 IStream_fnLockUnlockRegion,
273 IStream_fnLockUnlockRegion,
278 /* Methods overridden by the dummy stream */
280 /**************************************************************************
281 * IStream_fnAddRefDummy
283 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
285 ISHRegStream *This = (ISHRegStream *)iface;
286 TRACE("(%p)\n", This);
290 /**************************************************************************
291 * IStream_fnReleaseDummy
293 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
295 ISHRegStream *This = (ISHRegStream *)iface;
296 TRACE("(%p)\n", This);
300 /**************************************************************************
301 * IStream_fnReadDummy
303 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
310 static const IStreamVtbl DummyRegStreamVTable =
312 IStream_fnQueryInterface,
313 IStream_fnAddRefDummy, /* Overridden */
314 IStream_fnReleaseDummy, /* Overridden */
315 IStream_fnReadDummy, /* Overridden */
322 IStream_fnLockUnlockRegion,
323 IStream_fnLockUnlockRegion,
328 /* Dummy registry stream object */
329 static ISHRegStream rsDummyRegStream =
331 &DummyRegStreamVTable,
339 /**************************************************************************
342 * Internal helper: Create and initialise a new registry stream object.
344 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
346 ISHRegStream* regStream;
348 regStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
352 regStream->lpVtbl = &rstvt;
354 regStream->hKey = hKey;
355 regStream->pbBuffer = pbBuffer;
356 regStream->dwLength = dwLength;
357 regStream->dwPos = 0;
359 TRACE ("Returning %p\n", regStream);
360 return (IStream *)regStream;
363 /*************************************************************************
364 * SHOpenRegStream2A [SHLWAPI.@]
366 * Create a stream to read binary registry data.
369 * hKey [I] Registry handle
370 * pszSubkey [I] The sub key name
371 * pszValue [I] The value name under the sub key
375 * Success: An IStream interface referring to the registry data
376 * Failure: NULL, if the registry key could not be opened or is not binary.
378 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
379 LPCSTR pszValue,DWORD dwMode)
382 LPBYTE lpBuff = NULL;
383 DWORD dwLength, dwType;
385 TRACE("(%p,%s,%s,0x%08x)\n", hKey, pszSubkey, pszValue, dwMode);
387 /* Open the key, read in binary data and create stream */
388 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
389 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
390 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
391 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
392 dwType == REG_BINARY)
393 return IStream_Create(hStrKey, lpBuff, dwLength);
395 HeapFree (GetProcessHeap(), 0, lpBuff);
397 RegCloseKey(hStrKey);
401 /*************************************************************************
402 * SHOpenRegStream2W [SHLWAPI.@]
404 * See SHOpenRegStream2A.
406 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
407 LPCWSTR pszValue, DWORD dwMode)
410 LPBYTE lpBuff = NULL;
411 DWORD dwLength, dwType;
413 TRACE("(%p,%s,%s,0x%08x)\n", hKey, debugstr_w(pszSubkey),
414 debugstr_w(pszValue), dwMode);
416 /* Open the key, read in binary data and create stream */
417 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
418 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
419 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
420 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
421 dwType == REG_BINARY)
422 return IStream_Create(hStrKey, lpBuff, dwLength);
424 HeapFree (GetProcessHeap(), 0, lpBuff);
426 RegCloseKey(hStrKey);
430 /*************************************************************************
431 * SHOpenRegStreamA [SHLWAPI.@]
433 * Create a stream to read binary registry data.
436 * hKey [I] Registry handle
437 * pszSubkey [I] The sub key name
438 * pszValue [I] The value name under the sub key
439 * dwMode [I] STGM mode for opening the file
442 * Success: An IStream interface referring to the registry data
443 * Failure: If the registry key could not be opened or is not binary,
444 * A dummy (empty) IStream object is returned.
446 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
447 LPCSTR pszValue, DWORD dwMode)
451 TRACE("(%p,%s,%s,0x%08x)\n", hkey, pszSubkey, pszValue, dwMode);
453 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
454 return iStream ? iStream : (IStream *)&rsDummyRegStream;
457 /*************************************************************************
458 * SHOpenRegStreamW [SHLWAPI.@]
460 * See SHOpenRegStreamA.
462 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
463 LPCWSTR pszValue, DWORD dwMode)
467 TRACE("(%p,%s,%s,0x%08x)\n", hkey, debugstr_w(pszSubkey),
468 debugstr_w(pszValue), dwMode);
469 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
470 return iStream ? iStream : (IStream *)&rsDummyRegStream;
473 /*************************************************************************
476 * Create an IStream object on a block of memory.
479 * lpbData [I] Memory block to create the IStream object on
480 * dwDataLen [I] Length of data block
483 * Success: A pointer to the IStream object.
484 * Failure: NULL, if any parameters are invalid or an error occurs.
487 * A copy of the memory pointed to by lpbData is made, and is freed
488 * when the stream is released.
490 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
492 IStream *iStrmRet = NULL;
494 TRACE("(%p,%d)\n", lpbData, dwDataLen);
498 LPBYTE lpbDup = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
502 memcpy(lpbDup, lpbData, dwDataLen);
503 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
506 HeapFree(GetProcessHeap(), 0, lpbDup);
512 /*************************************************************************
513 * SHCreateStreamWrapper [SHLWAPI.@]
515 * Create an IStream object on a block of memory.
518 * lpbData [I] Memory block to create the IStream object on
519 * dwDataLen [I] Length of data block
520 * dwReserved [I] Reserved, Must be 0.
521 * lppStream [O] Destination for IStream object
524 * Success: S_OK. lppStream contains the new IStream object.
525 * Failure: E_INVALIDARG, if any parameters are invalid,
526 * E_OUTOFMEMORY if memory allocation fails.
529 * The stream assumes ownership of the memory passed to it.
531 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
532 DWORD dwReserved, IStream **lppStream)
539 if(dwReserved || !lppStream)
542 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
545 return E_OUTOFMEMORY;
547 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
548 IStream_Release(lpStream);