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
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
36 { IStreamVtbl
*lpVtbl
;
44 /**************************************************************************
45 * IStream_fnQueryInterface
47 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
49 ISHRegStream
*This
= (ISHRegStream
*)iface
;
51 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
55 if(IsEqualIID(riid
, &IID_IUnknown
)) /*IUnknown*/
57 else if(IsEqualIID(riid
, &IID_IStream
)) /*IStream*/
62 IStream_AddRef((IStream
*)*ppvObj
);
63 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
66 TRACE("-- Interface: E_NOINTERFACE\n");
70 /**************************************************************************
73 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
75 ISHRegStream
*This
= (ISHRegStream
*)iface
;
77 TRACE("(%p)->(count=%lu)\n",This
, This
->ref
);
79 return InterlockedIncrement(&This
->ref
);
82 /**************************************************************************
85 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
87 ISHRegStream
*This
= (ISHRegStream
*)iface
;
89 TRACE("(%p)->()\n",This
);
91 if (!InterlockedDecrement(&This
->ref
))
93 TRACE(" destroying SHReg IStream (%p)\n",This
);
96 HeapFree(GetProcessHeap(),0,This
->pbBuffer
);
99 RegCloseKey(This
->hKey
);
101 HeapFree(GetProcessHeap(),0,This
);
107 /**************************************************************************
110 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
112 ISHRegStream
*This
= (ISHRegStream
*)iface
;
114 DWORD dwBytesToRead
, dwBytesLeft
;
116 TRACE("(%p)->(%p,0x%08lx,%p)\n",This
, pv
, cb
, pcbRead
);
119 return STG_E_INVALIDPOINTER
;
121 dwBytesLeft
= This
->dwLength
- This
->dwPos
;
123 if ( 0 >= dwBytesLeft
) /* end of buffer */
126 dwBytesToRead
= ( cb
> dwBytesLeft
) ? dwBytesLeft
: cb
;
128 memmove ( pv
, (This
->pbBuffer
) + (This
->dwPos
), dwBytesToRead
);
130 This
->dwPos
+= dwBytesToRead
; /* adjust pointer */
133 *pcbRead
= dwBytesToRead
;
138 /**************************************************************************
141 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
143 ISHRegStream
*This
= (ISHRegStream
*)iface
;
145 TRACE("(%p)\n",This
);
153 /**************************************************************************
156 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
)
158 ISHRegStream
*This
= (ISHRegStream
*)iface
;
160 TRACE("(%p)\n",This
);
163 plibNewPosition
->QuadPart
= 0;
167 /**************************************************************************
170 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
)
172 ISHRegStream
*This
= (ISHRegStream
*)iface
;
174 TRACE("(%p)\n",This
);
178 /**************************************************************************
181 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
183 ISHRegStream
*This
= (ISHRegStream
*)iface
;
185 TRACE("(%p)\n",This
);
187 pcbRead
->QuadPart
= 0;
189 pcbWritten
->QuadPart
= 0;
193 /**************************************************************************
196 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
)
198 ISHRegStream
*This
= (ISHRegStream
*)iface
;
200 TRACE("(%p)\n",This
);
205 /**************************************************************************
208 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
)
210 ISHRegStream
*This
= (ISHRegStream
*)iface
;
212 TRACE("(%p)\n",This
);
217 /**************************************************************************
218 * IStream_fnLockUnlockRegion
220 static HRESULT WINAPI
IStream_fnLockUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
222 ISHRegStream
*This
= (ISHRegStream
*)iface
;
224 TRACE("(%p)\n",This
);
229 /*************************************************************************
232 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
)
234 ISHRegStream
*This
= (ISHRegStream
*)iface
;
236 TRACE("(%p)\n",This
);
241 /*************************************************************************
244 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
)
246 ISHRegStream
*This
= (ISHRegStream
*)iface
;
248 TRACE("(%p)\n",This
);
254 static struct IStreamVtbl rstvt
=
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 ISHRegStream
*This
= (ISHRegStream
*)iface
;
280 TRACE("(%p)\n", This
);
284 /**************************************************************************
285 * IStream_fnReleaseDummy
287 static ULONG WINAPI
IStream_fnReleaseDummy(IStream
*iface
)
289 ISHRegStream
*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 IStreamVtbl DummyRegStreamVTable
=
306 IStream_fnQueryInterface
,
307 IStream_fnAddRefDummy
, /* Overridden */
308 IStream_fnReleaseDummy
, /* Overridden */
309 IStream_fnReadDummy
, /* Overridden */
316 IStream_fnLockUnlockRegion
,
317 IStream_fnLockUnlockRegion
,
322 /* Dummy registry stream object */
323 static ISHRegStream rsDummyRegStream
=
325 &DummyRegStreamVTable
,
333 /**************************************************************************
336 * Internal helper: Create and initialise a new registry stream object.
338 static IStream
*IStream_Create(HKEY hKey
, LPBYTE pbBuffer
, DWORD dwLength
)
340 ISHRegStream
* regStream
;
342 regStream
= (ISHRegStream
*)HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream
));
346 regStream
->lpVtbl
= &rstvt
;
348 regStream
->hKey
= hKey
;
349 regStream
->pbBuffer
= pbBuffer
;
350 regStream
->dwLength
= dwLength
;
351 regStream
->dwPos
= 0;
353 TRACE ("Returning %p\n", regStream
);
354 return (IStream
*)regStream
;
357 /*************************************************************************
358 * SHOpenRegStream2A [SHLWAPI.@]
360 * Create a stream to read binary registry data.
363 * hKey [I] Registry handle
364 * pszSubkey [I] The sub key name
365 * pszValue [I] The value name under the sub key
369 * Success: An IStream interface referring to the registry data
370 * Failure: NULL, if the registry key could not be opened or is not binary.
372 IStream
* WINAPI
SHOpenRegStream2A(HKEY hKey
, LPCSTR pszSubkey
,
373 LPCSTR pszValue
,DWORD dwMode
)
376 LPBYTE lpBuff
= NULL
;
377 DWORD dwLength
, dwType
;
379 TRACE("(%p,%s,%s,0x%08lx)\n", hKey
, pszSubkey
, pszValue
, dwMode
);
381 /* Open the key, read in binary data and create stream */
382 if (!RegOpenKeyExA (hKey
, pszSubkey
, 0, KEY_READ
, &hStrKey
) &&
383 !RegQueryValueExA (hStrKey
, pszValue
, 0, 0, 0, &dwLength
) &&
384 (lpBuff
= HeapAlloc (GetProcessHeap(), 0, dwLength
)) &&
385 !RegQueryValueExA (hStrKey
, pszValue
, 0, &dwType
, lpBuff
, &dwLength
) &&
386 dwType
== REG_BINARY
)
387 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
);
420 HeapFree (GetProcessHeap(), 0, lpBuff
);
422 RegCloseKey(hStrKey
);
426 /*************************************************************************
427 * SHOpenRegStreamA [SHLWAPI.@]
429 * Create a stream to read binary registry data.
432 * hKey [I] Registry handle
433 * pszSubkey [I] The sub key name
434 * pszValue [I] The value name under the sub key
435 * dwMode [I] STGM mode for opening the file
438 * Success: An IStream interface referring to the registry data
439 * Failure: If the registry key could not be opened or is not binary,
440 * A dummy (empty) IStream object is returned.
442 IStream
* WINAPI
SHOpenRegStreamA(HKEY hkey
, LPCSTR pszSubkey
,
443 LPCSTR pszValue
, DWORD dwMode
)
447 TRACE("(%p,%s,%s,0x%08lx)\n", hkey
, pszSubkey
, pszValue
, dwMode
);
449 iStream
= SHOpenRegStream2A(hkey
, pszSubkey
, pszValue
, dwMode
);
450 return iStream
? iStream
: (IStream
*)&rsDummyRegStream
;
453 /*************************************************************************
454 * SHOpenRegStreamW [SHLWAPI.@]
456 * See SHOpenRegStreamA.
458 IStream
* WINAPI
SHOpenRegStreamW(HKEY hkey
, LPCWSTR pszSubkey
,
459 LPCWSTR pszValue
, DWORD dwMode
)
463 TRACE("(%p,%s,%s,0x%08lx)\n", hkey
, debugstr_w(pszSubkey
),
464 debugstr_w(pszValue
), dwMode
);
465 iStream
= SHOpenRegStream2W(hkey
, pszSubkey
, pszValue
, dwMode
);
466 return iStream
? iStream
: (IStream
*)&rsDummyRegStream
;
469 /*************************************************************************
472 * Create an IStream object on a block of memory.
475 * lpbData [I] Memory block to create the IStream object on
476 * dwDataLen [I] Length of data block
479 * Success: A pointer to the IStream object.
480 * Failure: NULL, if any parameters are invalid or an error occurs.
483 * A copy of the memory pointed to by lpbData is made, and is freed
484 * when the stream is released.
486 IStream
* WINAPI
SHCreateMemStream(LPBYTE lpbData
, DWORD dwDataLen
)
488 IStream
*iStrmRet
= NULL
;
490 TRACE("(%p,%ld)\n", lpbData
, dwDataLen
);
494 LPBYTE lpbDup
= (LPBYTE
)HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
498 memcpy(lpbDup
, lpbData
, dwDataLen
);
499 iStrmRet
= IStream_Create(NULL
, lpbDup
, dwDataLen
);
502 HeapFree(GetProcessHeap(), 0, lpbDup
);
508 /*************************************************************************
509 * SHCreateStreamWrapper [SHLWAPI.@]
511 * Create an IStream object on a block of memory.
514 * lpbData [I] Memory block to create the IStream object on
515 * dwDataLen [I] Length of data block
516 * dwReserved [I] Reserved, Must be 0.
517 * lppStream [O] Destination for IStream object
520 * Success: S_OK. lppStream contains the new IStream object.
521 * Failure: E_INVALIDARG, if any parameters are invalid,
522 * E_OUTOFMEMORY if memory allocation fails.
525 * The stream assumes ownership of the memory passed to it.
527 HRESULT WINAPI
SHCreateStreamWrapper(LPBYTE lpbData
, DWORD dwDataLen
,
528 DWORD dwReserved
, IStream
**lppStream
)
535 if(dwReserved
|| !lppStream
)
538 lpStream
= IStream_Create(NULL
, lpbData
, dwDataLen
);
541 return E_OUTOFMEMORY
;
543 IStream_QueryInterface(lpStream
, &IID_IStream
, (void**)lppStream
);
544 IStream_Release(lpStream
);