Re: Windows SDK Registry: How can I read data from the registry?
Q: The RegQueryValueEx function returns an error code 234 (ERROR_MORE_DATA) that means "more data is available". How many bytes must be allocated for value buffer to assure this error does not appear anymore?
A: To find the necessary buffer size, call 'RegQueryValueEx()' with a 'NULL' value in 'lpData' parameter. After return, the variable pointed by 'lpcbData' will contain the size of the data. Allocate the buffer 'lpData' then call 'RegQueryValueEx()' again:
Code:
HKEY hKey = NULL;
DWORD dwSize = 0;
DWORD dwDataType = 0;
LPBYTE lpValue = NULL;
LPCTSTR const lpValueName = _T("Directory");
LONG lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE,
_T("System\\CurrentControlSet\\Control\\Windows"),
0,
KEY_QUERY_VALUE,
&hKey);
if(ERROR_SUCCESS != lRet)
{
// Error handling (see this FAQ)
return;
}
// Call once RegQueryValueEx to retrieve the necessary buffer size
::RegQueryValueEx(hKey,
lpValueName,
0,
&dwDataType,
lpValue, // NULL
&dwSize); // will contain the data size
// Alloc the buffer
lpValue = (LPBYTE)malloc(dwSize);
// Call twice RegQueryValueEx to get the value
lRet = ::RegQueryValueEx(hKey,
lpValueName,
0,
&dwDataType,
lpValue,
&dwSize);
::RegCloseKey(hKey);
if(ERROR_SUCCESS != lRet)
{
// Error handling
return;
}
// Enjoy of lpValue...
// free the buffer when no more necessary
free(lpValue);
Last edited by Andreas Masur; July 25th, 2005 at 03:12 PM.
Bookmarks