|
C++ can't get RegQueryValueEx to work
I normally work in C#.NET, however, though it would be easier, the current environment I'm working in will not install .NET run times- don't ask me why. Anyway, this isn't the final result, but basically I need to check and change registry values. Not a problem in the language I know. With the history in C# I can fumble through C++ decently enough... however even though I have the functions calls set up as the windows help files say they should be (parameter, addresses, and such) the program crashes at run time if I try to read the value in dData at run-time - otherwise it runs error free; there are no compiler or linker errors. I'm interested both in a fix and why the syntax obviously has a problem yet matches with the Win32Api help file... thanks.
Brian
Code:
#include <iostream.h>
#include <windows.h>
void bjOpenKey(HKEY root, LPCTSTR subkey, HKEY *retKey, bool WriteAccess);
void bjCloseKey(HKEY key);
void bjShowMessage();
int main()
{
/* Open HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion */
HKEY local, software, microsoft, windows, currentVersion, policies;
LPBYTE dataSize;
LPDWORD dataType;
LONG ret;
bjOpenKey(HKEY_LOCAL_MACHINE, NULL, &local, false);
bjOpenKey(local, "SOFTWARE", &software, false);
bjOpenKey(software, "Microsoft", µsoft, false);
bjOpenKey(microsoft, "Windows", &windows, false);
bjOpenKey(windows, "CurrentVersion", ¤tVersion, false);
bjOpenKey(currentVersion, "policies", &policies, true);
ret = RegQueryValueEx(policies, "Test", 0, (LPDWORD) &dataType, (LPBYTE) &dData, (LPDWORD) &dataSize);
//Close Keys
bjCloseKey(policies);
bjCloseKey(currentVersion);
bjCloseKey(windows);
bjCloseKey(microsoft);
bjCloseKey(software);
bjCloseKey(local);
return 0;
}
void bjOpenKey(HKEY root, LPCTSTR subkey, HKEY *retKey, bool WriteAccess)
{
REGSAM thisAccess;
if(WriteAccess)
{
thisAccess = KEY_ALL_ACCESS;
}
else
{
thisAccess = KEY_READ;
}
LONG ret = RegOpenKeyEx(root, subkey, 0, thisAccess, retKey);
}
void bjCloseKey(HKEY key)
{
LONG ret = RegCloseKey(key);
}
void bjShowMessage()
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
cout << (LPCTSTR) lpMsgBuf << endl;
}
|