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;
78 ULONG refCount = InterlockedIncrement(&This->ref);
80 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
85 /**************************************************************************
88 static ULONG WINAPI IStream_fnRelease(IStream *iface)
90 ISHRegStream *This = (ISHRegStream *)iface;
91 ULONG refCount = InterlockedDecrement(&This->ref);
93 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
97 TRACE(" destroying SHReg IStream (%p)\n",This);
99 HeapFree(GetProcessHeap(),0,This->pbBuffer);
102 RegCloseKey(This->hKey);
104 HeapFree(GetProcessHeap(),0,This);
111 /**************************************************************************
114 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
116 ISHRegStream *This = (ISHRegStream *)iface;
118 DWORD dwBytesToRead, dwBytesLeft;
120 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
123 return STG_E_INVALIDPOINTER;
125 dwBytesLeft = This->dwLength - This->dwPos;
127 if ( 0 >= dwBytesLeft ) /* end of buffer */
130 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
132 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
134 This->dwPos += dwBytesToRead; /* adjust pointer */
137 *pcbRead = dwBytesToRead;
142 /**************************************************************************
145 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
147 ISHRegStream *This = (ISHRegStream *)iface;
149 TRACE("(%p)\n",This);
157 /**************************************************************************
160 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
162 ISHRegStream *This = (ISHRegStream *)iface;
164 TRACE("(%p)\n",This);
167 plibNewPosition->QuadPart = 0;
171 /**************************************************************************
174 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
176 ISHRegStream *This = (ISHRegStream *)iface;
178 TRACE("(%p)\n",This);
182 /**************************************************************************
185 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
187 ISHRegStream *This = (ISHRegStream *)iface;
189 TRACE("(%p)\n",This);
191 pcbRead->QuadPart = 0;
193 pcbWritten->QuadPart = 0;
197 /**************************************************************************
200 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
202 ISHRegStream *This = (ISHRegStream *)iface;
204 TRACE("(%p)\n",This);
209 /**************************************************************************
212 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
214 ISHRegStream *This = (ISHRegStream *)iface;
216 TRACE("(%p)\n",This);
221 /**************************************************************************
222 * IStream_fnLockUnlockRegion
224 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
226 ISHRegStream *This = (ISHRegStream *)iface;
228 TRACE("(%p)\n",This);
233 /*************************************************************************
236 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
238 ISHRegStream *This = (ISHRegStream *)iface;
240 TRACE("(%p)\n",This);
245 /*************************************************************************
248 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
250 ISHRegStream *This = (ISHRegStream *)iface;
252 TRACE("(%p)\n",This);
258 static struct IStreamVtbl rstvt =
260 IStream_fnQueryInterface,
270 IStream_fnLockUnlockRegion,
271 IStream_fnLockUnlockRegion,
276 /* Methods overridden by the dummy stream */
278 /**************************************************************************
279 * IStream_fnAddRefDummy
281 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
283 ISHRegStream *This = (ISHRegStream *)iface;
284 TRACE("(%p)\n", This);
288 /**************************************************************************
289 * IStream_fnReleaseDummy
291 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
293 ISHRegStream *This = (ISHRegStream *)iface;
294 TRACE("(%p)\n", This);
298 /**************************************************************************
299 * IStream_fnReadDummy
301 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
308 static struct IStreamVtbl DummyRegStreamVTable =
310 IStream_fnQueryInterface,
311 IStream_fnAddRefDummy, /* Overridden */
312 IStream_fnReleaseDummy, /* Overridden */
313 IStream_fnReadDummy, /* Overridden */
320 IStream_fnLockUnlockRegion,
321 IStream_fnLockUnlockRegion,
326 /* Dummy registry stream object */
327 static ISHRegStream rsDummyRegStream =
329 &DummyRegStreamVTable,
337 /**************************************************************************
340 * Internal helper: Create and initialise a new registry stream object.
342 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
344 ISHRegStream* regStream;
346 regStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
350 regStream->lpVtbl = &rstvt;
352 regStream->hKey = hKey;
353 regStream->pbBuffer = pbBuffer;
354 regStream->dwLength = dwLength;
355 regStream->dwPos = 0;
357 TRACE ("Returning %p\n", regStream);
358 return (IStream *)regStream;
361 /*************************************************************************
362 * SHOpenRegStream2A [SHLWAPI.@]
364 * Create a stream to read binary registry data.
367 * hKey [I] Registry handle
368 * pszSubkey [I] The sub key name
369 * pszValue [I] The value name under the sub key
373 * Success: An IStream interface referring to the registry data
374 * Failure: NULL, if the registry key could not be opened or is not binary.
376 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
377 LPCSTR pszValue,DWORD dwMode)
380 LPBYTE lpBuff = NULL;
381 DWORD dwLength, dwType;
383 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
385 /* Open the key, read in binary data and create stream */
386 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
387 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
388 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
389 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
390 dwType == REG_BINARY)
391 return IStream_Create(hStrKey, lpBuff, dwLength);
393 HeapFree (GetProcessHeap(), 0, lpBuff);
395 RegCloseKey(hStrKey);
399 /*************************************************************************
400 * SHOpenRegStream2W [SHLWAPI.@]
402 * See SHOpenRegStream2A.
404 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
405 LPCWSTR pszValue, DWORD dwMode)
408 LPBYTE lpBuff = NULL;
409 DWORD dwLength, dwType;
411 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
412 debugstr_w(pszValue), dwMode);
414 /* Open the key, read in binary data and create stream */
415 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
416 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
417 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
418 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
419 dwType == REG_BINARY)
420 return IStream_Create(hStrKey, lpBuff, dwLength);
422 HeapFree (GetProcessHeap(), 0, lpBuff);
424 RegCloseKey(hStrKey);
428 /*************************************************************************
429 * SHOpenRegStreamA [SHLWAPI.@]
431 * Create a stream to read binary registry data.
434 * hKey [I] Registry handle
435 * pszSubkey [I] The sub key name
436 * pszValue [I] The value name under the sub key
437 * dwMode [I] STGM mode for opening the file
440 * Success: An IStream interface referring to the registry data
441 * Failure: If the registry key could not be opened or is not binary,
442 * A dummy (empty) IStream object is returned.
444 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
445 LPCSTR pszValue, DWORD dwMode)
449 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
451 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
452 return iStream ? iStream : (IStream *)&rsDummyRegStream;
455 /*************************************************************************
456 * SHOpenRegStreamW [SHLWAPI.@]
458 * See SHOpenRegStreamA.
460 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
461 LPCWSTR pszValue, DWORD dwMode)
465 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
466 debugstr_w(pszValue), dwMode);
467 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
468 return iStream ? iStream : (IStream *)&rsDummyRegStream;
471 /*************************************************************************
474 * Create an IStream object on a block of memory.
477 * lpbData [I] Memory block to create the IStream object on
478 * dwDataLen [I] Length of data block
481 * Success: A pointer to the IStream object.
482 * Failure: NULL, if any parameters are invalid or an error occurs.
485 * A copy of the memory pointed to by lpbData is made, and is freed
486 * when the stream is released.
488 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
490 IStream *iStrmRet = NULL;
492 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
496 LPBYTE lpbDup = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
500 memcpy(lpbDup, lpbData, dwDataLen);
501 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
504 HeapFree(GetProcessHeap(), 0, lpbDup);
510 /*************************************************************************
511 * SHCreateStreamWrapper [SHLWAPI.@]
513 * Create an IStream object on a block of memory.
516 * lpbData [I] Memory block to create the IStream object on
517 * dwDataLen [I] Length of data block
518 * dwReserved [I] Reserved, Must be 0.
519 * lppStream [O] Destination for IStream object
522 * Success: S_OK. lppStream contains the new IStream object.
523 * Failure: E_INVALIDARG, if any parameters are invalid,
524 * E_OUTOFMEMORY if memory allocation fails.
527 * The stream assumes ownership of the memory passed to it.
529 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
530 DWORD dwReserved, IStream **lppStream)
537 if(dwReserved || !lppStream)
540 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
543 return E_OUTOFMEMORY;
545 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
546 IStream_Release(lpStream);