From 90e800cf9b3c60b41f38ee85b30570cff9e8631e Mon Sep 17 00:00:00 2001 From: Peter Oberndorfer Date: Mon, 28 Sep 2009 22:24:12 +0200 Subject: [PATCH] shlwapi: Allow SHOpenRegStream2 A/W to work with not existing values. When mode is STGM_WRITE or STGM_READWRITE make sure key key exists. Only read data in STGM_READ or STGM_READWRITE mode. Do not depend on value type being REG_BINARY. Succeed when reading not existing values. --- dlls/shlwapi/regstream.c | 76 ++++++++++++++++++++++++++++++++++++------------ 1 file changed, 58 insertions(+), 18 deletions(-) diff --git a/dlls/shlwapi/regstream.c b/dlls/shlwapi/regstream.c index c16784369b9..275827dda5e 100644 --- a/dlls/shlwapi/regstream.c +++ b/dlls/shlwapi/regstream.c @@ -440,21 +440,41 @@ static IStream *IStream_Create(HKEY hKey, LPBYTE pbBuffer, DWORD dwLength) IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey, LPCSTR pszValue,DWORD dwMode) { + IStream *tmp; HKEY hStrKey = NULL; LPBYTE lpBuff = NULL; - DWORD dwLength, dwType; + DWORD dwLength = 0; + LONG ret; TRACE("(%p,%s,%s,0x%08x)\n", hKey, pszSubkey, pszValue, dwMode); - /* Open the key, read in binary data and create stream */ - if (!RegOpenKeyExA (hKey, pszSubkey, 0, KEY_READ, &hStrKey) && - !RegQueryValueExA (hStrKey, pszValue, 0, 0, 0, &dwLength) && - (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) && - !RegQueryValueExA (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) && - dwType == REG_BINARY) - return IStream_Create(hStrKey, lpBuff, dwLength); + if (dwMode == STGM_READ) + ret = RegOpenKeyExA(hKey, pszSubkey, 0, KEY_READ, &hStrKey); + else /* in write mode we make sure the subkey exits */ + ret = RegCreateKeyExA(hKey, pszSubkey, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hStrKey, NULL); - HeapFree (GetProcessHeap(), 0, lpBuff); + if (ret == ERROR_SUCCESS) + { + if (dwMode == STGM_READ || dwMode == STGM_READWRITE) + { + /* read initial data */ + ret = RegQueryValueExA(hStrKey, pszValue, 0, 0, 0, &dwLength); + if (ret == ERROR_SUCCESS && dwLength) + { + lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength); + RegQueryValueExA(hStrKey, pszValue, 0, 0, lpBuff, &dwLength); + } + } + + if (!dwLength) + lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength); + + tmp = IStream_Create(hStrKey, lpBuff, dwLength); + + return tmp; + } + + HeapFree(GetProcessHeap(), 0, lpBuff); if (hStrKey) RegCloseKey(hStrKey); return NULL; @@ -468,22 +488,42 @@ IStream * WINAPI SHOpenRegStream2A(HKEY hKey, LPCSTR pszSubkey, IStream * WINAPI SHOpenRegStream2W(HKEY hKey, LPCWSTR pszSubkey, LPCWSTR pszValue, DWORD dwMode) { + IStream *tmp; HKEY hStrKey = NULL; LPBYTE lpBuff = NULL; - DWORD dwLength, dwType; + DWORD dwLength = 0; + LONG ret; TRACE("(%p,%s,%s,0x%08x)\n", hKey, debugstr_w(pszSubkey), debugstr_w(pszValue), dwMode); - /* Open the key, read in binary data and create stream */ - if (!RegOpenKeyExW (hKey, pszSubkey, 0, KEY_READ, &hStrKey) && - !RegQueryValueExW (hStrKey, pszValue, 0, 0, 0, &dwLength) && - (lpBuff = HeapAlloc (GetProcessHeap(), 0, dwLength)) && - !RegQueryValueExW (hStrKey, pszValue, 0, &dwType, lpBuff, &dwLength) && - dwType == REG_BINARY) - return IStream_Create(hStrKey, lpBuff, dwLength); + if (dwMode == STGM_READ) + ret = RegOpenKeyExW(hKey, pszSubkey, 0, KEY_READ, &hStrKey); + else /* in write mode we make sure the subkey exits */ + ret = RegCreateKeyExW(hKey, pszSubkey, 0, NULL, 0, KEY_READ | KEY_WRITE, NULL, &hStrKey, NULL); + + if (ret == ERROR_SUCCESS) + { + if (dwMode == STGM_READ || dwMode == STGM_READWRITE) + { + /* read initial data */ + ret = RegQueryValueExW(hStrKey, pszValue, 0, 0, 0, &dwLength); + if (ret == ERROR_SUCCESS && dwLength) + { + lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength); + RegQueryValueExW(hStrKey, pszValue, 0, 0, lpBuff, &dwLength); + } + } + + if (!dwLength) + lpBuff = HeapAlloc(GetProcessHeap(), 0, dwLength); + + tmp = IStream_Create(hStrKey, lpBuff, dwLength); + + return tmp; + } - HeapFree (GetProcessHeap(), 0, lpBuff); + HeapFree(GetProcessHeap(), 0, lpBuff); if (hStrKey) RegCloseKey(hStrKey); return NULL; -- 2.11.4.GIT