A couple of helper functions that I use:
Code:
// =====================================================================================
HRESULT RegGetString(HKEY hKey, LPCTSTR szValueName, LPTSTR * lpszResult) {
// Given a HKEY and value name returns a string from the registry.
// Upon successful return the string should be freed using free()
// eg. RegGetString(hKey, TEXT("my value"), &szString);
DWORD dwType=0, dwDataSize=0, dwBufSize=0;
LONG lResult;
// Incase we fail set the return string to null...
if (lpszResult != NULL) *lpszResult = NULL;
// Check input parameters...
if (hKey == NULL || lpszResult == NULL) return E_INVALIDARG;
// Get the length of the string in bytes (placed in dwDataSize)...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, NULL, &dwDataSize );
// Check result and make sure the registry value is a string(REG_SZ)...
if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
else if (dwType != REG_SZ) return DISP_E_TYPEMISMATCH;
// Allocate memory for string - We add space for a null terminating character...
dwBufSize = dwDataSize + (1 * sizeof(TCHAR));
*lpszResult = malloc(dwBufSize);
if (*lpszResult == NULL) return E_OUTOFMEMORY;
// Now get the actual string from the registry...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) *lpszResult, &dwDataSize );
// Check result and type again.
// If we fail here we must free the memory we allocated...
if (lResult != ERROR_SUCCESS) { free(*lpszResult); return HRESULT_FROM_WIN32(lResult); }
else if (dwType != REG_SZ) { free(*lpszResult); return DISP_E_TYPEMISMATCH; }
// We are not guaranteed a null terminated string from RegQueryValueEx.
// Explicitly null terminate the returned string...
(*lpszResult)[(dwBufSize / sizeof(TCHAR)) - 1] = TEXT('\0');
return NOERROR;
}
// =====================================================================================
HRESULT RegGetDWord(HKEY hKey, LPCTSTR szValueName, DWORD * lpdwResult) {
// Given a value name and an hKey returns a DWORD from the registry.
// eg. RegGetDWord(hKey, TEXT("my dword"), &dwMyValue);
LONG lResult;
DWORD dwDataSize = sizeof(DWORD);
DWORD dwType = 0;
// Check input parameters...
if (hKey == NULL || lpdwResult == NULL) return E_INVALIDARG;
// Get dword value from the registry...
lResult = RegQueryValueEx(hKey, szValueName, 0, &dwType, (LPBYTE) lpdwResult, &dwDataSize );
// Check result and make sure the registry value is a DWORD(REG_DWORD)...
if (lResult != ERROR_SUCCESS) return HRESULT_FROM_WIN32(lResult);
else if (dwType != REG_DWORD) return DISP_E_TYPEMISMATCH;
return NOERROR;
}
Usage:
Code:
HKEY hKey = NULL;
LONG lResult;
LPTSTR szVal;
DWORD dwVal;
// Open the registry key...
lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company\\Program", 0, KEY_QUERY_VALUE, &hKey);
if (lResult != ERROR_SUCCESS) return;
// Get the registry values...
hr = RegGetString(hKey, TEXT("szVal"), &szVal);
if (FAILED(hr)) return;
hr = RegGetDWord(hKey, TEXT("dwVal"), &dwVal);
if (FAILED(hr)) return;
// Print the results...
printf("The values: %s, %d", szVal, dwVal);