添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
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 ; // 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()); } 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.
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.
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).
For samples use google, you will find plenty of them.
1. You havent changed the size of your buffers (for example to something between 0x40 and 0x100).
2. You havent checked the actual return value of RegQueryValueExW, it can be something else than ERROR_SUCCESS - for example the value I previously mentioned that alone tells you what happend without GetLastError(). Use google and search for the documentation of RegQueryValueEx. Just by searching for the name of a windows function google finds the right page immediately.
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.
  • Read the question carefully.
  • Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  • If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  • Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question. Let's work to help developers, not make them feel stupid.
  •