I am trying to read three REG_SZ values (strings) from the registry using C, but I am having no success. The function RegOpenKeyEx runs correctly, but after I then run RegQueryValueExW, I sometimes get error 2 (ERROR_FILE_NOT_FOUND), or sometimes error 127 (ERROR_PROC_NOT_FOUND). Any help would be greatly appreciated, as I've been working on getting this to work for a few days now and it has been very frustrating! Thank you so much for taking the time to assist me!
The following code gives me error 127:
TCHAR date[11];
TCHAR regSerialKey[12];
TCHAR strAN[15];
HKEY hkey;
unsigned long datalen = sizeof(char) * 11;
if (RegOpenKeyEx(HKEY_CURRENT_USER, L"\\Software\\VB and VBA Program Settings\\App Version\\Align", 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS) { printf("RegOpenKeyEx failed with error: %d\n", GetLastError()); }
else
{
printf("RegOpenKeyEx was successful\n.");
if (RegQueryValueExW(hkey, TEXT("Date"), NULL, NULL, (LPBYTE)date, &datalen) != ERROR_SUCCESS) { printf("RegQueryValueExW run 1 failed with error: %d\n", GetLastError()); }
datalen = sizeof(char) * 12; if (RegQueryValueExW(hkey, TEXT("strSerNum"), NULL, NULL, (LPBYTE)regSerialKey, &datalen) != ERROR_SUCCESS)
{ printf("RegQueryValueExW run 2 failed with error: %d\n", GetLastError()); }
datalen = sizeof(char) * 12;
if (RegQueryValueExW(hkey, TEXT("strAN"), NULL, NULL, (LPBYTE)strAN, &datalen) != ERROR_SUCCESS)
{ printf("RegQueryValueExW run 3 failed with error: %d\n", GetLastError()); }
RegCloseKey(hkey);
}
Edit: I'm really new to the whole idea of the Windows Registry (I'm predominantly a Mac user), and I've never used these registry read functions before. Could someone please post code samples that should replace mine? Thanks!
Edit 2: Yes, my app is Unicode.
It does compile when I use L"address goes here", but when I use a const char * variable and use it as the parameter for the RegOpenKey function, that function returns error 127 (it used to be error 2, so there must be a few things wrong with my code). I've noticed that when I use L"address goes here", the compiler gives me a warning, "Suspicious pointer conversion," and also gives me the same warning for the RegQueryValue functions if I use L"Date" or L"strSerNum". If I use TEXT(), I don't get these warnings.
Edit 3: "In this case it can easily happen that GetLastError() returns something that is unrelated to your RegQueryValueEx call, for example the last_error of a previously failed function call."
You're right. I changed my code so that the a variable stores the return value of the function calls, and they keep giving me error 2. Error 127 must be from something unrelated, as you predicted.