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 IStream IStream_iface
;
54 static inline ISHRegStream
*impl_from_IStream(IStream
*iface
)
56 return CONTAINING_RECORD(iface
, ISHRegStream
, IStream_iface
);
59 /**************************************************************************
60 * IStream_fnQueryInterface
62 static HRESULT WINAPI
IStream_fnQueryInterface(IStream
*iface
, REFIID riid
, LPVOID
*ppvObj
)
64 ISHRegStream
*This
= impl_from_IStream(iface
);
66 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This
,debugstr_guid(riid
),ppvObj
);
70 if(IsEqualIID(riid
, &IID_IUnknown
) || IsEqualIID(riid
, &IID_IStream
))
71 *ppvObj
= &This
->IStream_iface
;
75 IStream_AddRef((IStream
*)*ppvObj
);
76 TRACE("-- Interface: (%p)->(%p)\n",ppvObj
,*ppvObj
);
79 TRACE("-- Interface: E_NOINTERFACE\n");
83 /**************************************************************************
86 static ULONG WINAPI
IStream_fnAddRef(IStream
*iface
)
88 ISHRegStream
*This
= impl_from_IStream(iface
);
89 ULONG refCount
= InterlockedIncrement(&This
->ref
);
91 TRACE("(%p)->(ref before=%u)\n",This
, refCount
- 1);
96 /**************************************************************************
99 static ULONG WINAPI
IStream_fnRelease(IStream
*iface
)
101 ISHRegStream
*This
= impl_from_IStream(iface
);
102 ULONG refCount
= InterlockedDecrement(&This
->ref
);
104 TRACE("(%p)->(ref before=%u)\n",This
, refCount
+ 1);
108 TRACE(" destroying SHReg IStream (%p)\n",This
);
112 /* write back data in REG_BINARY */
113 if (This
->dwMode
== STGM_READWRITE
|| This
->dwMode
== STGM_WRITE
)
118 RegSetValueExW(This
->hKey
, This
->u
.keyNameW
, 0, REG_BINARY
,
119 (const BYTE
*) This
->pbBuffer
, This
->dwLength
);
121 RegSetValueExA(This
->hKey
, This
->u
.keyNameA
, 0, REG_BINARY
,
122 (const BYTE
*) This
->pbBuffer
, This
->dwLength
);
127 RegDeleteValueW(This
->hKey
, This
->u
.keyNameW
);
129 RegDeleteValueA(This
->hKey
, This
->u
.keyNameA
);
133 RegCloseKey(This
->hKey
);
136 HeapFree(GetProcessHeap(),0,This
->u
.keyNameA
);
137 HeapFree(GetProcessHeap(),0,This
->pbBuffer
);
138 HeapFree(GetProcessHeap(),0,This
);
145 /**************************************************************************
148 static HRESULT WINAPI
IStream_fnRead (IStream
* iface
, void* pv
, ULONG cb
, ULONG
* pcbRead
)
150 ISHRegStream
*This
= impl_from_IStream(iface
);
153 TRACE("(%p)->(%p,0x%08x,%p)\n",This
, pv
, cb
, pcbRead
);
155 if (This
->dwPos
>= This
->dwLength
)
158 dwBytesToRead
= This
->dwLength
- This
->dwPos
;
160 dwBytesToRead
= (cb
> dwBytesToRead
) ? dwBytesToRead
: cb
;
161 if (dwBytesToRead
!= 0) /* not at end of buffer and we want to read something */
163 memmove(pv
, This
->pbBuffer
+ This
->dwPos
, dwBytesToRead
);
164 This
->dwPos
+= dwBytesToRead
; /* adjust pointer */
168 *pcbRead
= dwBytesToRead
;
173 /**************************************************************************
176 static HRESULT WINAPI
IStream_fnWrite (IStream
* iface
, const void* pv
, ULONG cb
, ULONG
* pcbWritten
)
178 ISHRegStream
*This
= impl_from_IStream(iface
);
179 DWORD newLen
= This
->dwPos
+ cb
;
181 TRACE("(%p, %p, %d, %p)\n",This
, pv
, cb
, pcbWritten
);
183 if (newLen
< This
->dwPos
) /* overflow */
184 return STG_E_INSUFFICIENTMEMORY
;
186 if (newLen
> This
->dwLength
)
188 LPBYTE newBuf
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pbBuffer
, newLen
);
190 return STG_E_INSUFFICIENTMEMORY
;
192 This
->dwLength
= newLen
;
193 This
->pbBuffer
= newBuf
;
195 memmove(This
->pbBuffer
+ This
->dwPos
, pv
, cb
);
196 This
->dwPos
+= cb
; /* adjust pointer */
204 /**************************************************************************
207 static HRESULT WINAPI
IStream_fnSeek (IStream
* iface
, LARGE_INTEGER dlibMove
, DWORD dwOrigin
, ULARGE_INTEGER
* plibNewPosition
)
209 ISHRegStream
*This
= impl_from_IStream(iface
);
211 TRACE("(%p, %s, %d %p)\n", This
,
212 wine_dbgstr_longlong(dlibMove
.QuadPart
), dwOrigin
, plibNewPosition
);
214 if (dwOrigin
== STREAM_SEEK_SET
)
216 else if (dwOrigin
== STREAM_SEEK_CUR
)
217 tmp
.QuadPart
= This
->dwPos
+ dlibMove
.QuadPart
;
218 else if (dwOrigin
== STREAM_SEEK_END
)
219 tmp
.QuadPart
= This
->dwLength
+ dlibMove
.QuadPart
;
221 return STG_E_INVALIDPARAMETER
;
223 if (tmp
.QuadPart
< 0)
224 return STG_E_INVALIDFUNCTION
;
226 /* we cut off the high part here */
227 This
->dwPos
= tmp
.u
.LowPart
;
230 plibNewPosition
->QuadPart
= This
->dwPos
;
234 /**************************************************************************
237 static HRESULT WINAPI
IStream_fnSetSize (IStream
* iface
, ULARGE_INTEGER libNewSize
)
239 ISHRegStream
*This
= impl_from_IStream(iface
);
243 TRACE("(%p, %s)\n", This
, wine_dbgstr_longlong(libNewSize
.QuadPart
));
245 /* we cut off the high part here */
246 newLen
= libNewSize
.u
.LowPart
;
247 newBuf
= HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, This
->pbBuffer
, newLen
);
249 return STG_E_INSUFFICIENTMEMORY
;
251 This
->pbBuffer
= newBuf
;
252 This
->dwLength
= newLen
;
257 /**************************************************************************
260 static HRESULT WINAPI
IStream_fnCopyTo (IStream
* iface
, IStream
* pstm
, ULARGE_INTEGER cb
, ULARGE_INTEGER
* pcbRead
, ULARGE_INTEGER
* pcbWritten
)
262 ISHRegStream
*This
= impl_from_IStream(iface
);
264 TRACE("(%p)\n",This
);
266 pcbRead
->QuadPart
= 0;
268 pcbWritten
->QuadPart
= 0;
274 /**************************************************************************
277 static HRESULT WINAPI
IStream_fnCommit (IStream
* iface
, DWORD grfCommitFlags
)
279 ISHRegStream
*This
= impl_from_IStream(iface
);
281 TRACE("(%p)\n",This
);
283 /* commit not supported by this stream */
287 /**************************************************************************
290 static HRESULT WINAPI
IStream_fnRevert (IStream
* iface
)
292 ISHRegStream
*This
= impl_from_IStream(iface
);
294 TRACE("(%p)\n",This
);
296 /* revert not supported by this stream */
300 /**************************************************************************
301 * IStream_fnLockUnlockRegion
303 static HRESULT WINAPI
IStream_fnLockUnlockRegion (IStream
* iface
, ULARGE_INTEGER libOffset
, ULARGE_INTEGER cb
, DWORD dwLockType
)
305 ISHRegStream
*This
= impl_from_IStream(iface
);
307 TRACE("(%p)\n",This
);
309 /* lock/unlock not supported by this stream */
313 /*************************************************************************
316 static HRESULT WINAPI
IStream_fnStat (IStream
* iface
, STATSTG
* pstatstg
, DWORD grfStatFlag
)
318 ISHRegStream
*This
= impl_from_IStream(iface
);
320 TRACE("(%p, %p, %d)\n",This
,pstatstg
,grfStatFlag
);
322 pstatstg
->pwcsName
= NULL
;
323 pstatstg
->type
= STGTY_STREAM
;
324 pstatstg
->cbSize
.QuadPart
= This
->dwLength
;
325 pstatstg
->mtime
.dwHighDateTime
= 0;
326 pstatstg
->mtime
.dwLowDateTime
= 0;
327 pstatstg
->ctime
.dwHighDateTime
= 0;
328 pstatstg
->ctime
.dwLowDateTime
= 0;
329 pstatstg
->atime
.dwHighDateTime
= 0;
330 pstatstg
->atime
.dwLowDateTime
= 0;
331 pstatstg
->grfMode
= This
->dwMode
;
332 pstatstg
->grfLocksSupported
= 0;
333 pstatstg
->clsid
= CLSID_NULL
;
334 pstatstg
->grfStateBits
= 0;
335 pstatstg
->reserved
= 0;
340 /*************************************************************************
343 static HRESULT WINAPI
IStream_fnClone (IStream
* iface
, IStream
** ppstm
)
345 ISHRegStream
*This
= impl_from_IStream(iface
);
347 TRACE("(%p)\n",This
);
350 /* clone not supported by this stream */
354 static const IStreamVtbl rstvt
=
356 IStream_fnQueryInterface
,
366 IStream_fnLockUnlockRegion
,
367 IStream_fnLockUnlockRegion
,
372 /* Methods overridden by the dummy stream */
374 /**************************************************************************
375 * IStream_fnAddRefDummy
377 static ULONG WINAPI
IStream_fnAddRefDummy(IStream
*iface
)
379 ISHRegStream
*This
= impl_from_IStream(iface
);
380 TRACE("(%p)\n", This
);
384 /**************************************************************************
385 * IStream_fnReleaseDummy
387 static ULONG WINAPI
IStream_fnReleaseDummy(IStream
*iface
)
389 ISHRegStream
*This
= impl_from_IStream(iface
);
390 TRACE("(%p)\n", This
);
394 /**************************************************************************
395 * IStream_fnReadDummy
397 static HRESULT WINAPI
IStream_fnReadDummy(IStream
*iface
, LPVOID pv
, ULONG cb
, ULONG
* pcbRead
)
404 static const IStreamVtbl DummyRegStreamVTable
=
406 IStream_fnQueryInterface
,
407 IStream_fnAddRefDummy
, /* Overridden */
408 IStream_fnReleaseDummy
, /* Overridden */
409 IStream_fnReadDummy
, /* Overridden */
416 IStream_fnLockUnlockRegion
,
417 IStream_fnLockUnlockRegion
,
422 /* Dummy registry stream object */
423 static ISHRegStream rsDummyRegStream
=
425 { &DummyRegStreamVTable
},
436 /**************************************************************************
439 * Internal helper: Create and initialise a new registry stream object.
441 static ISHRegStream
*IStream_Create(HKEY hKey
, LPBYTE pbBuffer
, DWORD dwLength
)
443 ISHRegStream
* regStream
;
445 regStream
= HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream
));
449 regStream
->IStream_iface
.lpVtbl
= &rstvt
;
451 regStream
->hKey
= hKey
;
452 regStream
->pbBuffer
= pbBuffer
;
453 regStream
->dwLength
= dwLength
;
454 regStream
->dwPos
= 0;
455 regStream
->dwMode
= STGM_READWRITE
;
456 regStream
->u
.keyNameA
= NULL
;
457 regStream
->bUnicode
= FALSE
;
459 TRACE ("Returning %p\n", regStream
);
463 /*************************************************************************
464 * SHOpenRegStream2A [SHLWAPI.@]
466 * Create a stream to read binary registry data.
469 * hKey [I] Registry handle
470 * pszSubkey [I] The sub key name
471 * pszValue [I] The value name under the sub key
475 * Success: An IStream interface referring to the registry data
476 * Failure: NULL, if the registry key could not be opened or is not binary.
478 IStream
* WINAPI
SHOpenRegStream2A(HKEY hKey
, LPCSTR pszSubkey
,
479 LPCSTR pszValue
,DWORD dwMode
)
483 LPBYTE lpBuff
= NULL
;
487 TRACE("(%p,%s,%s,0x%08x)\n", hKey
, pszSubkey
, pszValue
, dwMode
);
489 if (dwMode
== STGM_READ
)
490 ret
= RegOpenKeyExA(hKey
, pszSubkey
, 0, KEY_READ
, &hStrKey
);
491 else /* in write mode we make sure the subkey exits */
492 ret
= RegCreateKeyExA(hKey
, pszSubkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
494 if (ret
== ERROR_SUCCESS
)
496 if (dwMode
== STGM_READ
|| dwMode
== STGM_READWRITE
)
498 /* read initial data */
499 ret
= RegQueryValueExA(hStrKey
, pszValue
, 0, 0, 0, &dwLength
);
500 if (ret
== ERROR_SUCCESS
&& dwLength
)
502 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
503 RegQueryValueExA(hStrKey
, pszValue
, 0, 0, lpBuff
, &dwLength
);
508 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
510 tmp
= IStream_Create(hStrKey
, lpBuff
, dwLength
);
515 int len
= lstrlenA(pszValue
) + 1;
516 tmp
->u
.keyNameA
= HeapAlloc(GetProcessHeap(), 0, len
);
517 memcpy(tmp
->u
.keyNameA
, pszValue
, len
);
520 tmp
->dwMode
= dwMode
;
521 tmp
->bUnicode
= FALSE
;
522 return &tmp
->IStream_iface
;
526 HeapFree(GetProcessHeap(), 0, lpBuff
);
528 RegCloseKey(hStrKey
);
532 /*************************************************************************
533 * SHOpenRegStream2W [SHLWAPI.@]
535 * See SHOpenRegStream2A.
537 IStream
* WINAPI
SHOpenRegStream2W(HKEY hKey
, LPCWSTR pszSubkey
,
538 LPCWSTR pszValue
, DWORD dwMode
)
542 LPBYTE lpBuff
= NULL
;
546 TRACE("(%p,%s,%s,0x%08x)\n", hKey
, debugstr_w(pszSubkey
),
547 debugstr_w(pszValue
), dwMode
);
549 if (dwMode
== STGM_READ
)
550 ret
= RegOpenKeyExW(hKey
, pszSubkey
, 0, KEY_READ
, &hStrKey
);
551 else /* in write mode we make sure the subkey exits */
552 ret
= RegCreateKeyExW(hKey
, pszSubkey
, 0, NULL
, 0, KEY_READ
| KEY_WRITE
, NULL
, &hStrKey
, NULL
);
554 if (ret
== ERROR_SUCCESS
)
556 if (dwMode
== STGM_READ
|| dwMode
== STGM_READWRITE
)
558 /* read initial data */
559 ret
= RegQueryValueExW(hStrKey
, pszValue
, 0, 0, 0, &dwLength
);
560 if (ret
== ERROR_SUCCESS
&& dwLength
)
562 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
563 RegQueryValueExW(hStrKey
, pszValue
, 0, 0, lpBuff
, &dwLength
);
568 lpBuff
= HeapAlloc(GetProcessHeap(), 0, dwLength
);
570 tmp
= IStream_Create(hStrKey
, lpBuff
, dwLength
);
575 int len
= lstrlenW(pszValue
) + 1;
576 tmp
->u
.keyNameW
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
));
577 memcpy(tmp
->u
.keyNameW
, pszValue
, len
* sizeof(WCHAR
));
580 tmp
->dwMode
= dwMode
;
581 tmp
->bUnicode
= TRUE
;
582 return &tmp
->IStream_iface
;
586 HeapFree(GetProcessHeap(), 0, lpBuff
);
588 RegCloseKey(hStrKey
);
592 /*************************************************************************
593 * SHOpenRegStreamA [SHLWAPI.@]
595 * Create a stream to read binary registry data.
598 * hKey [I] Registry handle
599 * pszSubkey [I] The sub key name
600 * pszValue [I] The value name under the sub key
601 * dwMode [I] STGM mode for opening the file
604 * Success: An IStream interface referring to the registry data
605 * Failure: If the registry key could not be opened or is not binary,
606 * A dummy (empty) IStream object is returned.
608 IStream
* WINAPI
SHOpenRegStreamA(HKEY hkey
, LPCSTR pszSubkey
,
609 LPCSTR pszValue
, DWORD dwMode
)
613 TRACE("(%p,%s,%s,0x%08x)\n", hkey
, pszSubkey
, pszValue
, dwMode
);
615 iStream
= SHOpenRegStream2A(hkey
, pszSubkey
, pszValue
, dwMode
);
616 return iStream
? iStream
: &rsDummyRegStream
.IStream_iface
;
619 /*************************************************************************
620 * SHOpenRegStreamW [SHLWAPI.@]
622 * See SHOpenRegStreamA.
624 IStream
* WINAPI
SHOpenRegStreamW(HKEY hkey
, LPCWSTR pszSubkey
,
625 LPCWSTR pszValue
, DWORD dwMode
)
629 TRACE("(%p,%s,%s,0x%08x)\n", hkey
, debugstr_w(pszSubkey
),
630 debugstr_w(pszValue
), dwMode
);
631 iStream
= SHOpenRegStream2W(hkey
, pszSubkey
, pszValue
, dwMode
);
632 return iStream
? iStream
: &rsDummyRegStream
.IStream_iface
;
635 /*************************************************************************
638 * Create an IStream object on a block of memory.
641 * lpbData [I] Memory block to create the IStream object on
642 * dwDataLen [I] Length of data block
645 * Success: A pointer to the IStream object.
646 * Failure: NULL, if any parameters are invalid or an error occurs.
649 * A copy of the memory pointed to by lpbData is made, and is freed
650 * when the stream is released.
652 IStream
* WINAPI
SHCreateMemStream(const BYTE
*lpbData
, UINT dwDataLen
)
654 ISHRegStream
*strm
= NULL
;
657 TRACE("(%p,%d)\n", lpbData
, dwDataLen
);
662 lpbDup
= HeapAlloc(GetProcessHeap(), 0, dwDataLen
);
666 memcpy(lpbDup
, lpbData
, dwDataLen
);
667 strm
= IStream_Create(NULL
, lpbDup
, dwDataLen
);
670 HeapFree(GetProcessHeap(), 0, lpbDup
);
672 return &strm
->IStream_iface
;
675 /*************************************************************************
676 * SHCreateStreamWrapper [SHLWAPI.@]
678 * Create an IStream object on a block of memory.
681 * lpbData [I] Memory block to create the IStream object on
682 * dwDataLen [I] Length of data block
683 * dwReserved [I] Reserved, Must be 0.
684 * lppStream [O] Destination for IStream object
687 * Success: S_OK. lppStream contains the new IStream object.
688 * Failure: E_INVALIDARG, if any parameters are invalid,
689 * E_OUTOFMEMORY if memory allocation fails.
692 * The stream assumes ownership of the memory passed to it.
694 HRESULT WINAPI
SHCreateStreamWrapper(LPBYTE lpbData
, DWORD dwDataLen
,
695 DWORD dwReserved
, IStream
**lppStream
)
702 if(dwReserved
|| !lppStream
)
705 strm
= IStream_Create(NULL
, lpbData
, dwDataLen
);
708 return E_OUTOFMEMORY
;
710 IStream_QueryInterface(&strm
->IStream_iface
, &IID_IStream
, (void**)lppStream
);
711 IStream_Release(&strm
->IStream_iface
);