Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

C++
TCHAR date[11];
TCHAR regSerialKey[12];
TCHAR strAN[15];
HKEY hkey;
unsigned long datalen = sizeof(char) * 11; // stores length of data read, in bytes

if (RegOpenKeyEx(HKEY_CURRENT_USER, L"\\Software\\VB and VBA Program Settings\\App Version\\Align", 0, KEY_ALL_ACCESS, &hkey) != ERROR_SUCCESS)                                       // this part has always worked
{ 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)     // these have always failed
    {  printf("RegQueryValueExW run 1 failed with error: %d\n", GetLastError());  }
    datalen = sizeof(char) * 12; // must be reset after each call to RGVE function
    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.
Posted
Updated 15-Aug-13 7:35am
v4
Comments
pasztorpisti 15-Aug-13 11:41am    
EDIT: Its not the type parameter I previously mentioned.
ERROR_FILE_NOT_FOUND is a valid last_error that is mentioned by the function documentation, read it. This not found error can be the result of searching for an incorrect type. Use regedit while debugging to check out the actual state of the registry when you get into a breakpoint as a result of an error. Try to use a bigger buffer to receive the key value and use sizeof(date)/sizeof(date[0]) to fill up datalen. If your RegQueryValueEx call returns ERROR_MORE_DATA then the buffer is too small. 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.
Richard MacCutchan 15-Aug-13 11:49am    
Don't use the W suffix on your function calls. Let the compiler add it based on your project settings. Your code is mixing TCHAR and char types, and using wide function calls; a total mess that is bound to lead to tears before bedtime.
Jochen Arndt 15-Aug-13 11:57am    
Is your app a Unicode build or not?

You are mixing ANSI, Unicode and project depending function versions and strings:
Variables are TCHAR (project depending), RegQueryValue() is project defined while you are passing a wide string and RegQueryValueExW() is Unicode while you are passing project defined strings (TEXT macro).
pasztorpisti 15-Aug-13 11:58am    
It must be unicode as the W-less RegOpenKeyEx call receives a wide string.
Jochen Arndt 15-Aug-13 12:02pm    
With an ANSI build it would try to open the path "\" (the HKCU root here). I have not tested it, but I think that it succeeds.

you have one big problem: Understand that you work in ANSI-Mode but using Unicode function. It leads to strange string which cant be interpreted by the API. Look to what the used macro get resolved.

Use the functions without the "W" at the end.
 
Share this answer
 
Thanks for the help, everyone! I got it to work, thanks to your suggestions. I changed the string types to char instead of TCHAR, and removed the W from RegQueryKeyExW, and a few other things that were suggested by you. Again, thank you so much! You have no idea how glad I am.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900