appwiz.cpl: Use the fancy new icon.
[wine/multimedia.git] / dlls / shlwapi / regstream.c
blob4dc15ad38dc8473356c006112bd22cf130a8cbd7
1 /*
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
22 #include <stdarg.h>
23 #include <string.h>
25 #define COBJMACROS
27 #include "winerror.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "objbase.h"
31 #include "winreg.h"
32 #include "shlwapi.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shell);
38 typedef struct
40 const IStreamVtbl *lpVtbl;
41 LONG ref;
42 HKEY hKey;
43 LPBYTE pbBuffer;
44 DWORD dwLength;
45 DWORD dwPos;
46 DWORD dwMode;
47 union {
48 LPSTR keyNameA;
49 LPWSTR keyNameW;
50 }u;
51 BOOL bUnicode;
52 } ISHRegStream;
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);
63 *ppvObj = NULL;
65 if(IsEqualIID(riid, &IID_IUnknown)) /*IUnknown*/
66 *ppvObj = This;
67 else if(IsEqualIID(riid, &IID_IStream)) /*IStream*/
68 *ppvObj = This;
70 if(*ppvObj)
72 IStream_AddRef((IStream*)*ppvObj);
73 TRACE("-- Interface: (%p)->(%p)\n",ppvObj,*ppvObj);
74 return S_OK;
76 TRACE("-- Interface: E_NOINTERFACE\n");
77 return E_NOINTERFACE;
80 /**************************************************************************
81 * IStream_fnAddRef
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);
90 return refCount;
93 /**************************************************************************
94 * IStream_fnRelease
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);
103 if (!refCount)
105 TRACE(" destroying SHReg IStream (%p)\n",This);
107 if (This->hKey)
109 /* write back data in REG_BINARY */
110 if (This->dwMode == STGM_READWRITE || This->dwMode == STGM_WRITE)
112 if (This->dwLength)
114 if (This->bUnicode)
115 RegSetValueExW(This->hKey, This->u.keyNameW, 0, REG_BINARY,
116 (const BYTE *) This->pbBuffer, This->dwLength);
117 else
118 RegSetValueExA(This->hKey, This->u.keyNameA, 0, REG_BINARY,
119 (const BYTE *) This->pbBuffer, This->dwLength);
121 else
123 if (This->bUnicode)
124 RegDeleteValueW(This->hKey, This->u.keyNameW);
125 else
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);
136 return 0;
139 return refCount;
142 /**************************************************************************
143 * IStream_fnRead
145 static HRESULT WINAPI IStream_fnRead (IStream * iface, void* pv, ULONG cb, ULONG* pcbRead)
147 ISHRegStream *This = (ISHRegStream *)iface;
148 DWORD dwBytesToRead;
150 TRACE("(%p)->(%p,0x%08x,%p)\n",This, pv, cb, pcbRead);
152 if (This->dwPos >= This->dwLength)
153 dwBytesToRead = 0;
154 else
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 */
164 if (pcbRead)
165 *pcbRead = dwBytesToRead;
167 return S_OK;
170 /**************************************************************************
171 * IStream_fnWrite
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);
186 if (!newBuf)
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 */
195 if (pcbWritten)
196 *pcbWritten = cb;
198 return S_OK;
201 /**************************************************************************
202 * IStream_fnSeek
204 static HRESULT WINAPI IStream_fnSeek (IStream * iface, LARGE_INTEGER dlibMove, DWORD dwOrigin, ULARGE_INTEGER* plibNewPosition)
206 ISHRegStream *This = (ISHRegStream *)iface;
207 LARGE_INTEGER tmp;
208 TRACE("(%p, %s, %d %p)\n", This,
209 wine_dbgstr_longlong(dlibMove.QuadPart), dwOrigin, plibNewPosition);
211 if (dwOrigin == STREAM_SEEK_SET)
212 tmp = dlibMove;
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;
217 else
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;
226 if (plibNewPosition)
227 plibNewPosition->QuadPart = This->dwPos;
228 return S_OK;
231 /**************************************************************************
232 * IStream_fnSetSize
234 static HRESULT WINAPI IStream_fnSetSize (IStream * iface, ULARGE_INTEGER libNewSize)
236 ISHRegStream *This = (ISHRegStream *)iface;
237 DWORD newLen;
238 LPBYTE newBuf;
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);
245 if (!newBuf)
246 return STG_E_INSUFFICIENTMEMORY;
248 This->pbBuffer = newBuf;
249 This->dwLength = newLen;
251 return S_OK;
254 /**************************************************************************
255 * IStream_fnCopyTo
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);
262 if (pcbRead)
263 pcbRead->QuadPart = 0;
264 if (pcbWritten)
265 pcbWritten->QuadPart = 0;
267 /* TODO implement */
268 return E_NOTIMPL;
271 /**************************************************************************
272 * IStream_fnCommit
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 */
281 return E_NOTIMPL;
284 /**************************************************************************
285 * IStream_fnRevert
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 */
294 return E_NOTIMPL;
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 */
307 return E_NOTIMPL;
310 /*************************************************************************
311 * IStream_fnStat
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;
334 return S_OK;
337 /*************************************************************************
338 * IStream_fnClone
340 static HRESULT WINAPI IStream_fnClone (IStream * iface, IStream** ppstm)
342 ISHRegStream *This = (ISHRegStream *)iface;
344 TRACE("(%p)\n",This);
345 *ppstm = NULL;
347 /* clone not supported by this stream */
348 return E_NOTIMPL;
351 static const IStreamVtbl rstvt =
353 IStream_fnQueryInterface,
354 IStream_fnAddRef,
355 IStream_fnRelease,
356 IStream_fnRead,
357 IStream_fnWrite,
358 IStream_fnSeek,
359 IStream_fnSetSize,
360 IStream_fnCopyTo,
361 IStream_fnCommit,
362 IStream_fnRevert,
363 IStream_fnLockUnlockRegion,
364 IStream_fnLockUnlockRegion,
365 IStream_fnStat,
366 IStream_fnClone
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);
378 return 2;
381 /**************************************************************************
382 * IStream_fnReleaseDummy
384 static ULONG WINAPI IStream_fnReleaseDummy(IStream *iface)
386 ISHRegStream *This = (ISHRegStream *)iface;
387 TRACE("(%p)\n", This);
388 return 1;
391 /**************************************************************************
392 * IStream_fnReadDummy
394 static HRESULT WINAPI IStream_fnReadDummy(IStream *iface, LPVOID pv, ULONG cb, ULONG* pcbRead)
396 if (pcbRead)
397 *pcbRead = 0;
398 return E_NOTIMPL;
401 static const IStreamVtbl DummyRegStreamVTable =
403 IStream_fnQueryInterface,
404 IStream_fnAddRefDummy, /* Overridden */
405 IStream_fnReleaseDummy, /* Overridden */
406 IStream_fnReadDummy, /* Overridden */
407 IStream_fnWrite,
408 IStream_fnSeek,
409 IStream_fnSetSize,
410 IStream_fnCopyTo,
411 IStream_fnCommit,
412 IStream_fnRevert,
413 IStream_fnLockUnlockRegion,
414 IStream_fnLockUnlockRegion,
415 IStream_fnStat,
416 IStream_fnClone
419 /* Dummy registry stream object */
420 static ISHRegStream rsDummyRegStream =
422 &DummyRegStreamVTable,
424 NULL,
425 NULL,
428 STGM_READWRITE,
429 {NULL},
430 FALSE
433 /**************************************************************************
434 * IStream_Create
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));
444 if (regStream)
446 regStream->lpVtbl = &rstvt;
447 regStream->ref = 1;
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);
457 return regStream;
460 /*************************************************************************
461 * SHOpenRegStream2A [SHLWAPI.@]
463 * Create a stream to read binary registry data.
465 * PARAMS
466 * hKey [I] Registry handle
467 * pszSubkey [I] The sub key name
468 * pszValue [I] The value name under the sub key
469 * dwMode [I] Unused
471 * RETURNS
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)
478 ISHRegStream *tmp;
479 HKEY hStrKey = NULL;
480 LPBYTE lpBuff = NULL;
481 DWORD dwLength = 0;
482 LONG ret;
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);
504 if (!dwLength)
505 lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength);
507 tmp = IStream_Create(hStrKey, lpBuff, dwLength);
508 if(tmp)
510 if(pszValue)
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);
524 if (hStrKey)
525 RegCloseKey(hStrKey);
526 return NULL;
529 /*************************************************************************
530 * SHOpenRegStream2W [SHLWAPI.@]
532 * See SHOpenRegStream2A.
534 IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey,
535 LPCWSTR pszValue, DWORD dwMode)
537 ISHRegStream *tmp;
538 HKEY hStrKey = NULL;
539 LPBYTE lpBuff = NULL;
540 DWORD dwLength = 0;
541 LONG ret;
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);
564 if (!dwLength)
565 lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength);
567 tmp = IStream_Create(hStrKey, lpBuff, dwLength);
568 if(tmp)
570 if(pszValue)
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);
584 if (hStrKey)
585 RegCloseKey(hStrKey);
586 return NULL;
589 /*************************************************************************
590 * SHOpenRegStreamA [SHLWAPI.@]
592 * Create a stream to read binary registry data.
594 * PARAMS
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
600 * RETURNS
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)
608 IStream *iStream;
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)
624 IStream *iStream;
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 /*************************************************************************
633 * @ [SHLWAPI.12]
635 * Create an IStream object on a block of memory.
637 * PARAMS
638 * lpbData [I] Memory block to create the IStream object on
639 * dwDataLen [I] Length of data block
641 * RETURNS
642 * Success: A pointer to the IStream object.
643 * Failure: NULL, if any parameters are invalid or an error occurs.
645 * NOTES
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;
652 LPBYTE lpbDup;
654 TRACE("(%p,%d)\n", lpbData, dwDataLen);
656 if (!lpbData)
657 dwDataLen = 0;
659 lpbDup = HeapAlloc(GetProcessHeap(), 0, dwDataLen);
661 if (lpbDup)
663 memcpy(lpbDup, lpbData, dwDataLen);
664 iStrmRet = (IStream *)IStream_Create(NULL, lpbDup, dwDataLen);
666 if (!iStrmRet)
667 HeapFree(GetProcessHeap(), 0, lpbDup);
669 return iStrmRet;
672 /*************************************************************************
673 * SHCreateStreamWrapper [SHLWAPI.@]
675 * Create an IStream object on a block of memory.
677 * PARAMS
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
683 * RETURNS
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.
688 * NOTES
689 * The stream assumes ownership of the memory passed to it.
691 HRESULT WINAPI SHCreateStreamWrapper(LPBYTE lpbData, DWORD dwDataLen,
692 DWORD dwReserved, IStream **lppStream)
694 IStream* lpStream;
696 if (lppStream)
697 *lppStream = NULL;
699 if(dwReserved || !lppStream)
700 return E_INVALIDARG;
702 lpStream = (IStream *)IStream_Create(NULL, lpbData, dwDataLen);
704 if(!lpStream)
705 return E_OUTOFMEMORY;
707 IStream_QueryInterface(lpStream, &IID_IStream, (void**)lppStream);
708 IStream_Release(lpStream);
709 return S_OK;