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
;
54 /**************************************************************************
55 * IStream_fnQueryInterface
57 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
59 ISHRegStream
*This
= (ISHRegStream
*)iface
;
61 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
65 if(IsEqualIID(riid
, &IID_IUnknown
)) /*IUnknown*/
67 else if(IsEqualIID(riid
, &IID_IStream
)) /*IStream*/
72 IStream_AddRef((IStream
*)*ppvObj
);
73 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
76 TRACE("-- Interface: E_NOINTERFACE\n");
80 /**************************************************************************
83 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
85 ISHRegStream
*This
= (ISHRegStream
*)iface
;
86 ULONG refCount
= InterlockedIncrement(&This
->ref
);
88 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
93 /**************************************************************************
96 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
98 ISHRegStream
*This
= (ISHRegStream
*)iface
;
99 ULONG refCount
= InterlockedDecrement(&This
->ref
);
101 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
105 TRACE(" destroying SHReg IStream (%p)\n",This
);
109 /* write back data in REG_BINARY */
110 if (This
->dwMode
== STGM_READWRITE
|| This
->dwMode
== STGM_WRITE
)
115 RegSetValueExW(This
->hKey
, This
->u
.keyNameW
, 0, REG_BINARY
,
116 (const BYTE
*) This
->pbBuffer
, This
->dwLength
);
118 RegSetValueExA(This
->hKey
, This
->u
.keyNameA
, 0, REG_BINARY
,
119 (const BYTE
*) This
->pbBuffer
, This
->dwLength
);
124 RegDeleteValueW(This
->hKey
, This
->u
.keyNameW
);
126 RegDeleteValueA(This
->hKey
, This
->u
.keyNameA
);
130 RegCloseKey(This
->hKey
);
133 HeapFree(GetProcessHeap(),0,This
->u
.keyNameA
);
134 HeapFree(GetProcessHeap(),0,This
->pbBuffer
);
135 HeapFree(GetProcessHeap(),0,This
);
142 /**************************************************************************
145 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
147 ISHRegStream
*This
= (ISHRegStream
*)iface
;
150 TRACE("(%p)->(%p,0x%08x,%p)\n",This
, pv
, cb
, pcbRead
);
152 if (This
->dwPos
>= This
->dwLength
)
155 dwBytesToRead
= This
->dwLength
- This
->dwPos
;
157 dwBytesToRead
= (cb
> dwBytesToRead
) ? dwBytesToRead
: cb
;
158 if (dwBytesToRead
!= 0) /* not at end of buffer and we want to read something */
160 memmove(pv
, This
->pbBuffer
+ This
->dwPos
, dwBytesToRead
);
161 This
->dwPos
+= dwBytesToRead
; /* adjust pointer */
165 *pcbRead
= dwBytesToRead
;
170 /**************************************************************************
173 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
175 ISHRegStream
*This
= (ISHRegStream
*)iface
;
176 DWORD newLen
= This
->dwPos
+ cb
;
178 TRACE("(%p, %p, %d, %p)\n",This
, pv
, cb
, pcbWritten
);
180 if (newLen
< This
->dwPos
) /* overflow */
181 return STG_E_INSUFFICIENTMEMORY
;
183 if (newLen
> This
->dwLength
)
185 LPBYTE newBuf
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pbBuffer
, newLen
);
187 return STG_E_INSUFFICIENTMEMORY
;
189 This
->dwLength
= newLen
;
190 This
->pbBuffer
= newBuf
;
192 memmove(This
->pbBuffer
+ This
->dwPos
, pv
, cb
);
193 This
->dwPos
+= cb
; /* adjust pointer */
201 /**************************************************************************
204 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
)
206 ISHRegStream
*This
= (ISHRegStream
*)iface
;
208 TRACE("(%p, %s, %d %p)\n", This
,
209 wine_dbgstr_longlong(dlibMove
.QuadPart
), dwOrigin
, plibNewPosition
);
211 if (dwOrigin
== STREAM_SEEK_SET
)
213 else if (dwOrigin
== STREAM_SEEK_CUR
)
214 tmp
.QuadPart
= This
->dwPos
+ dlibMove
.QuadPart
;
215 else if (dwOrigin
== STREAM_SEEK_END
)
216 tmp
.QuadPart
= This
->dwLength
+ dlibMove
.QuadPart
;
218 return STG_E_INVALIDPARAMETER
;
220 if (tmp
.QuadPart
< 0)
221 return STG_E_INVALIDFUNCTION
;
223 /* we cut off the high part here */
224 This
->dwPos
= tmp
.u
.LowPart
;
227 plibNewPosition
->QuadPart
= This
->dwPos
;
231 /**************************************************************************
234 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
)
236 ISHRegStream
*This
= (ISHRegStream
*)iface
;
240 TRACE("(%p, %s)\n", This
, wine_dbgstr_longlong(libNewSize
.QuadPart
));
242 /* we cut off the high part here */
243 newLen
= libNewSize
.u
.LowPart
;
244 newBuf
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pbBuffer
, newLen
);
246 return STG_E_INSUFFICIENTMEMORY
;
248 This
->pbBuffer
= newBuf
;
249 This
->dwLength
= newLen
;
254 /**************************************************************************
257 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
259 ISHRegStream
*This
= (ISHRegStream
*)iface
;
261 TRACE("(%p)\n",This
);
263 pcbRead
->QuadPart
= 0;
265 pcbWritten
->QuadPart
= 0;
271 /**************************************************************************
274 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
)
276 ISHRegStream
*This
= (ISHRegStream
*)iface
;
278 TRACE("(%p)\n",This
);
280 /* commit not supported by this stream */
284 /**************************************************************************
287 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
)
289 ISHRegStream
*This
= (ISHRegStream
*)iface
;
291 TRACE("(%p)\n",This
);
293 /* revert not supported by this stream */
297 /**************************************************************************
298 * IStream_fnLockUnlockRegion
300 static HRESULT WINAPI
IStream_fnLockUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
302 ISHRegStream
*This
= (ISHRegStream
*)iface
;
304 TRACE("(%p)\n",This
);
306 /* lock/unlock not supported by this stream */
310 /*************************************************************************
313 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
)
315 ISHRegStream
*This
= (ISHRegStream
*)iface
;
317 TRACE("(%p, %p, %d)\n",This
,pstatstg
,grfStatFlag
);
319 pstatstg
->pwcsName
= NULL
;
320 pstatstg
->type
= STGTY_STREAM
;
321 pstatstg
->cbSize
.QuadPart
= This
->dwLength
;
322 pstatstg
->mtime
.dwHighDateTime
= 0;
323 pstatstg
->mtime
.dwLowDateTime
= 0;
324 pstatstg
->ctime
.dwHighDateTime
= 0;
325 pstatstg
->ctime
.dwLowDateTime
= 0;
326 pstatstg
->atime
.dwHighDateTime
= 0;
327 pstatstg
->atime
.dwLowDateTime
= 0;
328 pstatstg
->grfMode
= This
->dwMode
;
329 pstatstg
->grfLocksSupported
= 0;
330 pstatstg
->clsid
= CLSID_NULL
;
331 pstatstg
->grfStateBits
= 0;
332 pstatstg
->reserved
= 0;
337 /*************************************************************************
340 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
)
342 ISHRegStream
*This
= (ISHRegStream
*)iface
;
344 TRACE("(%p)\n",This
);
347 /* clone not supported by this stream */
351 static const IStreamVtbl rstvt
=
353 IStream_fnQueryInterface
,
363 IStream_fnLockUnlockRegion
,
364 IStream_fnLockUnlockRegion
,
369 /* Methods overridden by the dummy stream */
371 /**************************************************************************
372 * IStream_fnAddRefDummy
374 static ULONG WINAPI
IStream_fnAddRefDummy(IStream
*iface
)
376 ISHRegStream
*This
= (ISHRegStream
*)iface
;
377 TRACE("(%p)\n", This
);
381 /**************************************************************************
382 * IStream_fnReleaseDummy
384 static ULONG WINAPI
IStream_fnReleaseDummy(IStream
*iface
)
386 ISHRegStream
*This
= (ISHRegStream
*)iface
;
387 TRACE("(%p)\n", This
);
391 /**************************************************************************
392 * IStream_fnReadDummy
394 static HRESULT WINAPI
IStream_fnReadDummy(IStream
*iface
, LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
401 static const IStreamVtbl DummyRegStreamVTable
=
403 IStream_fnQueryInterface
,
404 IStream_fnAddRefDummy
, /* Overridden */
405 IStream_fnReleaseDummy
, /* Overridden */
406 IStream_fnReadDummy
, /* Overridden */
413 IStream_fnLockUnlockRegion
,
414 IStream_fnLockUnlockRegion
,
419 /* Dummy registry stream object */
420 static ISHRegStream rsDummyRegStream
=
422 &DummyRegStreamVTable
,
433 /**************************************************************************
436 * Internal helper: Create and initialise a new registry stream object.
438 static ISHRegStream
*IStream_Create(HKEY hKey
, LPBYTE pbBuffer
, DWORD dwLength
)
440 ISHRegStream
* regStream
;
442 regStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream
));
446 regStream
->lpVtbl
= &rstvt
;
448 regStream
->hKey
= hKey
;
449 regStream
->pbBuffer
= pbBuffer
;
450 regStream
->dwLength
= dwLength
;
451 regStream
->dwPos
= 0;
452 regStream
->dwMode
= STGM_READWRITE
;
453 regStream
->u
.keyNameA
= NULL
;
454 regStream
->bUnicode
= FALSE
;
456 TRACE ("Returning %p\n", regStream
);
460 /*************************************************************************
461 * SHOpenRegStream2A [SHLWAPI.@]
463 * Create a stream to read binary registry data.
466 * hKey [I] Registry handle
467 * pszSubkey [I] The sub key name
468 * pszValue [I] The value name under the sub key
472 * Success: An IStream interface referring to the registry data
473 * Failure: NULL, if the registry key could not be opened or is not binary.
475 IStream
* WINAPI
SHOpenRegStream2A(HKEY hKey
, LPCSTR pszSubkey
,
476 LPCSTR pszValue
,DWORD dwMode
)
480 LPBYTE lpBuff
= NULL
;
484 TRACE("(%p,%s,%s,0x%08x)\n", hKey
, pszSubkey
, pszValue
, dwMode
);
486 if (dwMode
== STGM_READ
)
487 ret
= RegOpenKeyExA(hKey
, pszSubkey
, 0, KEY_READ
, &hStrKey
);
488 else /* in write mode we make sure the subkey exits */
489 ret
= RegCreateKeyExA(hKey
, pszSubkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
491 if (ret
== ERROR_SUCCESS
)
493 if (dwMode
== STGM_READ
|| dwMode
== STGM_READWRITE
)
495 /* read initial data */
496 ret
= RegQueryValueExA(hStrKey
, pszValue
, 0, 0, 0, &dwLength
);
497 if (ret
== ERROR_SUCCESS
&& dwLength
)
499 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
500 RegQueryValueExA(hStrKey
, pszValue
, 0, 0, lpBuff
, &dwLength
);
505 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
507 tmp
= IStream_Create(hStrKey
, lpBuff
, dwLength
);
512 int len
= lstrlenA(pszValue
) + 1;
513 tmp
->u
.keyNameA
= HeapAlloc(GetProcessHeap(), 0, len
);
514 memcpy(tmp
->u
.keyNameA
, pszValue
, len
);
517 tmp
->dwMode
= dwMode
;
518 tmp
->bUnicode
= FALSE
;
519 return (IStream
*)tmp
;
523 HeapFree(GetProcessHeap(), 0, lpBuff
);
525 RegCloseKey(hStrKey
);
529 /*************************************************************************
530 * SHOpenRegStream2W [SHLWAPI.@]
532 * See SHOpenRegStream2A.
534 IStream
* WINAPI
SHOpenRegStream2W(HKEY hKey
, LPCWSTR pszSubkey
,
535 LPCWSTR pszValue
, DWORD dwMode
)
539 LPBYTE lpBuff
= NULL
;
543 TRACE("(%p,%s,%s,0x%08x)\n", hKey
, debugstr_w(pszSubkey
),
544 debugstr_w(pszValue
), dwMode
);
546 if (dwMode
== STGM_READ
)
547 ret
= RegOpenKeyExW(hKey
, pszSubkey
, 0, KEY_READ
, &hStrKey
);
548 else /* in write mode we make sure the subkey exits */
549 ret
= RegCreateKeyExW(hKey
, pszSubkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
551 if (ret
== ERROR_SUCCESS
)
553 if (dwMode
== STGM_READ
|| dwMode
== STGM_READWRITE
)
555 /* read initial data */
556 ret
= RegQueryValueExW(hStrKey
, pszValue
, 0, 0, 0, &dwLength
);
557 if (ret
== ERROR_SUCCESS
&& dwLength
)
559 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
560 RegQueryValueExW(hStrKey
, pszValue
, 0, 0, lpBuff
, &dwLength
);
565 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
567 tmp
= IStream_Create(hStrKey
, lpBuff
, dwLength
);
572 int len
= lstrlenW(pszValue
) + 1;
573 tmp
->u
.keyNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
574 memcpy(tmp
->u
.keyNameW
, pszValue
, len
* sizeof(WCHAR
));
577 tmp
->dwMode
= dwMode
;
578 tmp
->bUnicode
= TRUE
;
579 return (IStream
*)tmp
;
583 HeapFree(GetProcessHeap(), 0, lpBuff
);
585 RegCloseKey(hStrKey
);
589 /*************************************************************************
590 * SHOpenRegStreamA [SHLWAPI.@]
592 * Create a stream to read binary registry data.
595 * hKey [I] Registry handle
596 * pszSubkey [I] The sub key name
597 * pszValue [I] The value name under the sub key
598 * dwMode [I] STGM mode for opening the file
601 * Success: An IStream interface referring to the registry data
602 * Failure: If the registry key could not be opened or is not binary,
603 * A dummy (empty) IStream object is returned.
605 IStream
* WINAPI
SHOpenRegStreamA(HKEY hkey
, LPCSTR pszSubkey
,
606 LPCSTR pszValue
, DWORD dwMode
)
610 TRACE("(%p,%s,%s,0x%08x)\n", hkey
, pszSubkey
, pszValue
, dwMode
);
612 iStream
= SHOpenRegStream2A(hkey
, pszSubkey
, pszValue
, dwMode
);
613 return iStream
? iStream
: (IStream
*)&rsDummyRegStream
;
616 /*************************************************************************
617 * SHOpenRegStreamW [SHLWAPI.@]
619 * See SHOpenRegStreamA.
621 IStream
* WINAPI
SHOpenRegStreamW(HKEY hkey
, LPCWSTR pszSubkey
,
622 LPCWSTR pszValue
, DWORD dwMode
)
626 TRACE("(%p,%s,%s,0x%08x)\n", hkey
, debugstr_w(pszSubkey
),
627 debugstr_w(pszValue
), dwMode
);
628 iStream
= SHOpenRegStream2W(hkey
, pszSubkey
, pszValue
, dwMode
);
629 return iStream
? iStream
: (IStream
*)&rsDummyRegStream
;
632 /*************************************************************************
635 * Create an IStream object on a block of memory.
638 * lpbData [I] Memory block to create the IStream object on
639 * dwDataLen [I] Length of data block
642 * Success: A pointer to the IStream object.
643 * Failure: NULL, if any parameters are invalid or an error occurs.
646 * A copy of the memory pointed to by lpbData is made, and is freed
647 * when the stream is released.
649 IStream
* WINAPI
SHCreateMemStream(const BYTE
*lpbData
, UINT dwDataLen
)
651 IStream
*iStrmRet
= NULL
;
654 TRACE("(%p,%d)\n", lpbData
, dwDataLen
);
659 lpbDup
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
663 memcpy(lpbDup
, lpbData
, dwDataLen
);
664 iStrmRet
= (IStream
*)IStream_Create(NULL
, lpbDup
, dwDataLen
);
667 HeapFree(GetProcessHeap(), 0, lpbDup
);
672 /*************************************************************************
673 * SHCreateStreamWrapper [SHLWAPI.@]
675 * Create an IStream object on a block of memory.
678 * lpbData [I] Memory block to create the IStream object on
679 * dwDataLen [I] Length of data block
680 * dwReserved [I] Reserved, Must be 0.
681 * lppStream [O] Destination for IStream object
684 * Success: S_OK. lppStream contains the new IStream object.
685 * Failure: E_INVALIDARG, if any parameters are invalid,
686 * E_OUTOFMEMORY if memory allocation fails.
689 * The stream assumes ownership of the memory passed to it.
691 HRESULT WINAPI
SHCreateStreamWrapper(LPBYTE lpbData
, DWORD dwDataLen
,
692 DWORD dwReserved
, IStream
**lppStream
)
699 if(dwReserved
|| !lppStream
)
702 lpStream
= (IStream
*)IStream_Create(NULL
, lpbData
, dwDataLen
);
705 return E_OUTOFMEMORY
;
707 IStream_QueryInterface(lpStream
, &IID_IStream
, (void**)lppStream
);
708 IStream_Release(lpStream
);