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);
39 const IStreamVtbl *lpVtbl;
47 /**************************************************************************
48 * IStream_fnQueryInterface
50 static HRESULT WINAPI IStream_fnQueryInterface(IStream *iface, REFIID riid, LPVOID *ppvObj)
52 ISHRegStream *This = (ISHRegStream *)iface;
54 TRACE("(%p)->(\n\tIID:\t%s,%p)\n",This,debugstr_guid(riid),ppvObj);
58 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
60 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
65 IStream_AddRef((IStream*)*ppvObj);
66 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
69 TRACE("-- Interface: E_NOINTERFACE\n");
73 /**************************************************************************
76 static ULONG WINAPI IStream_fnAddRef(IStream *iface)
78 ISHRegStream *This = (ISHRegStream *)iface;
79 ULONG refCount = InterlockedIncrement(&This->ref);
81 TRACE("(%p)->(ref before=%lu)\n",This, refCount - 1);
86 /**************************************************************************
89 static ULONG WINAPI IStream_fnRelease(IStream *iface)
91 ISHRegStream *This = (ISHRegStream *)iface;
92 ULONG refCount = InterlockedDecrement(&This->ref);
94 TRACE("(%p)->(ref before=%lu)\n",This, refCount + 1);
98 TRACE(" destroying SHReg IStream (%p)\n",This);
100 HeapFree(GetProcessHeap(),0,This->pbBuffer);
103 RegCloseKey(This->hKey);
105 HeapFree(GetProcessHeap(),0,This);
112 /**************************************************************************
115 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
117 ISHRegStream *This = (ISHRegStream *)iface;
119 DWORD dwBytesToRead, dwBytesLeft;
121 TRACE("(%p)->(%p,0x%08lx,%p)\n",This, pv, cb, pcbRead);
124 return STG_E_INVALIDPOINTER;
126 dwBytesLeft = This->dwLength - This->dwPos;
128 if ( 0 >= dwBytesLeft ) /* end of buffer */
131 dwBytesToRead = ( cb > dwBytesLeft) ? dwBytesLeft : cb;
133 memmove ( pv, (This->pbBuffer) + (This->dwPos), dwBytesToRead);
135 This->dwPos += dwBytesToRead; /* adjust pointer */
138 *pcbRead = dwBytesToRead;
143 /**************************************************************************
146 static HRESULT WINAPI IStream_fnWrite (IStream * iface, const void* pv, ULONG cb, ULONG* pcbWritten)
148 ISHRegStream *This = (ISHRegStream *)iface;
150 TRACE("(%p)\n",This);
158 /**************************************************************************
161 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
163 ISHRegStream *This = (ISHRegStream *)iface;
165 TRACE("(%p)\n",This);
168 plibNewPosition->QuadPart = 0;
172 /**************************************************************************
175 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
177 ISHRegStream *This = (ISHRegStream *)iface;
179 TRACE("(%p)\n",This);
183 /**************************************************************************
186 static HRESULT WINAPI IStream_fnCopyTo (IStream * iface, IStream* pstm, ULARGE_INTEGER cb, ULARGE_INTEGER* pcbRead, ULARGE_INTEGER* pcbWritten)
188 ISHRegStream *This = (ISHRegStream *)iface;
190 TRACE("(%p)\n",This);
192 pcbRead->QuadPart = 0;
194 pcbWritten->QuadPart = 0;
198 /**************************************************************************
201 static HRESULT WINAPI IStream_fnCommit (IStream * iface, DWORD grfCommitFlags)
203 ISHRegStream *This = (ISHRegStream *)iface;
205 TRACE("(%p)\n",This);
210 /**************************************************************************
213 static HRESULT WINAPI IStream_fnRevert (IStream * iface)
215 ISHRegStream *This = (ISHRegStream *)iface;
217 TRACE("(%p)\n",This);
222 /**************************************************************************
223 * IStream_fnLockUnlockRegion
225 static HRESULT WINAPI IStream_fnLockUnlockRegion (IStream * iface, ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
227 ISHRegStream *This = (ISHRegStream *)iface;
229 TRACE("(%p)\n",This);
234 /*************************************************************************
237 static HRESULT WINAPI IStream_fnStat (IStream * iface, STATSTG* pstatstg, DWORD grfStatFlag)
239 ISHRegStream *This = (ISHRegStream *)iface;
241 TRACE("(%p)\n",This);
246 /*************************************************************************
249 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
251 ISHRegStream *This = (ISHRegStream *)iface;
253 TRACE("(%p)\n",This);
259 static const IStreamVtbl rstvt =
261 IStream_fnQueryInterface,
271 IStream_fnLockUnlockRegion,
272 IStream_fnLockUnlockRegion,
277 /* Methods overridden by the dummy stream */
279 /**************************************************************************
280 * IStream_fnAddRefDummy
282 static ULONG WINAPI IStream_fnAddRefDummy(IStream *iface)
284 ISHRegStream *This = (ISHRegStream *)iface;
285 TRACE("(%p)\n", This);
289 /**************************************************************************
290 * IStream_fnReleaseDummy
292 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
294 ISHRegStream *This = (ISHRegStream *)iface;
295 TRACE("(%p)\n", This);
299 /**************************************************************************
300 * IStream_fnReadDummy
302 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
309 static const IStreamVtbl DummyRegStreamVTable =
311 IStream_fnQueryInterface,
312 IStream_fnAddRefDummy, /* Overridden */
313 IStream_fnReleaseDummy, /* Overridden */
314 IStream_fnReadDummy, /* Overridden */
321 IStream_fnLockUnlockRegion,
322 IStream_fnLockUnlockRegion,
327 /* Dummy registry stream object */
328 static ISHRegStream rsDummyRegStream =
330 &DummyRegStreamVTable,
338 /**************************************************************************
341 * Internal helper: Create and initialise a new registry stream object.
343 static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength)
345 ISHRegStream* regStream;
347 regStream = HeapAlloc(GetProcessHeap(), 0, sizeof(ISHRegStream));
351 regStream->lpVtbl = &rstvt;
353 regStream->hKey = hKey;
354 regStream->pbBuffer = pbBuffer;
355 regStream->dwLength = dwLength;
356 regStream->dwPos = 0;
358 TRACE ("Returning %p\n", regStream);
359 return (IStream *)regStream;
362 /*************************************************************************
363 * SHOpenRegStream2A [SHLWAPI.@]
365 * Create a stream to read binary registry data.
368 * hKey [I] Registry handle
369 * pszSubkey [I] The sub key name
370 * pszValue [I] The value name under the sub key
374 * Success: An IStream interface referring to the registry data
375 * Failure: NULL, if the registry key could not be opened or is not binary.
377 IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey,
378 LPCSTR pszValue,DWORD dwMode)
381 LPBYTE lpBuff = NULL;
382 DWORD dwLength, dwType;
384 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, pszSubkey, pszValue, dwMode);
386 /* Open the key, read in binary data and create stream */
387 if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
388 !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
389 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
390 !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
391 dwType == REG_BINARY)
392 return IStream_Create(hStrKey, lpBuff, dwLength);
394 HeapFree (GetProcessHeap(), 0, lpBuff);
396 RegCloseKey(hStrKey);
400 /*************************************************************************
401 * SHOpenRegStream2W [SHLWAPI.@]
403 * See SHOpenRegStream2A.
405 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
406 LPCWSTR pszValue, DWORD dwMode)
409 LPBYTE lpBuff = NULL;
410 DWORD dwLength, dwType;
412 TRACE("(%p,%s,%s,0x%08lx)\n", hKey, debugstr_w(pszSubkey),
413 debugstr_w(pszValue), dwMode);
415 /* Open the key, read in binary data and create stream */
416 if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) &&
417 !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) &&
418 (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) &&
419 !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) &&
420 dwType == REG_BINARY)
421 return IStream_Create(hStrKey, lpBuff, dwLength);
423 HeapFree (GetProcessHeap(), 0, lpBuff);
425 RegCloseKey(hStrKey);
429 /*************************************************************************
430 * SHOpenRegStreamA [SHLWAPI.@]
432 * Create a stream to read binary registry data.
435 * hKey [I] Registry handle
436 * pszSubkey [I] The sub key name
437 * pszValue [I] The value name under the sub key
438 * dwMode [I] STGM mode for opening the file
441 * Success: An IStream interface referring to the registry data
442 * Failure: If the registry key could not be opened or is not binary,
443 * A dummy (empty) IStream object is returned.
445 IStream * WINAPI SHOpenRegStreamA(HKEY hkey, LPCSTR pszSubkey,
446 LPCSTR pszValue, DWORD dwMode)
450 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, pszSubkey, pszValue, dwMode);
452 iStream = SHOpenRegStream2A(hkey, pszSubkey, pszValue, dwMode);
453 return iStream ? iStream : (IStream *)&rsDummyRegStream;
456 /*************************************************************************
457 * SHOpenRegStreamW [SHLWAPI.@]
459 * See SHOpenRegStreamA.
461 IStream * WINAPI SHOpenRegStreamW(HKEY hkey, LPCWSTR pszSubkey,
462 LPCWSTR pszValue, DWORD dwMode)
466 TRACE("(%p,%s,%s,0x%08lx)\n", hkey, debugstr_w(pszSubkey),
467 debugstr_w(pszValue), dwMode);
468 iStream = SHOpenRegStream2W(hkey, pszSubkey, pszValue, dwMode);
469 return iStream ? iStream : (IStream *)&rsDummyRegStream;
472 /*************************************************************************
475 * Create an IStream object on a block of memory.
478 * lpbData [I] Memory block to create the IStream object on
479 * dwDataLen [I] Length of data block
482 * Success: A pointer to the IStream object.
483 * Failure: NULL, if any parameters are invalid or an error occurs.
486 * A copy of the memory pointed to by lpbData is made, and is freed
487 * when the stream is released.
489 IStream * WINAPI SHCreateMemStream(LPBYTE lpbData, DWORD dwDataLen)
491 IStream *iStrmRet = NULL;
493 TRACE("(%p,%ld)\n", lpbData, dwDataLen);
497 LPBYTE lpbDup = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
501 memcpy(lpbDup, lpbData, dwDataLen);
502 iStrmRet = IStream_Create(NULL, lpbDup, dwDataLen);
505 HeapFree(GetProcessHeap(), 0, lpbDup);
511 /*************************************************************************
512 * SHCreateStreamWrapper [SHLWAPI.@]
514 * Create an IStream object on a block of memory.
517 * lpbData [I] Memory block to create the IStream object on
518 * dwDataLen [I] Length of data block
519 * dwReserved [I] Reserved, Must be 0.
520 * lppStream [O] Destination for IStream object
523 * Success: S_OK. lppStream contains the new IStream object.
524 * Failure: E_INVALIDARG, if any parameters are invalid,
525 * E_OUTOFMEMORY if memory allocation fails.
528 * The stream assumes ownership of the memory passed to it.
530 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
531 DWORD dwReserved, IStream **lppStream)
538 if(dwReserved || !lppStream)
541 lpStream = IStream_Create(NULL, lpbData, dwDataLen);
544 return E_OUTOFMEMORY;
546 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
547 IStream_Release(lpStream);