Click to See Complete Forum and Search --> : Reading a registry value


Korupt
October 13th, 2008, 11:20 AM
I'm just trying to read the system root value from the registry but for some reason it's not working. Here's my code:

#include <windows.h>
#include <stdio.h>

int main()
{
char lszValue[255];
HKEY hKey;
LONG returnStatus;
DWORD dwType=REG_SZ;
DWORD dwSize=255;
returnStatus = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", NULL, KEY_ALL_ACCESS, &hKey);
if (returnStatus == ERROR_SUCCESS)
{
returnStatus = RegQueryValueEx(hKey, TEXT("SystemRoot"), NULL, &dwType,(LPBYTE)&lszValue, &dwSize);
if (returnStatus == ERROR_SUCCESS)
{
printf("Value Read is %s\n", lszValue);
}
}
RegCloseKey(hKey);
system("pause");

I'm not getting an error but it's not printing the value of SystemRoot. Can anyone please explain to me why and how I can fix it. Thanks.

0xC0000005
October 13th, 2008, 12:09 PM
Check the return value?!

I'll bet it has something to do with security. Why are you using KEY_ALL_ACCESS when you really only need KEY_QUERY_VALUE?

golanshahar
October 13th, 2008, 05:15 PM
Just a comment if you doing all this code to get windows directory why not calling ::GetWindowsDirectory() (http://msdn.microsoft.com/en-us/library/ms724454(VS.85).aspx) instead?

Cheers