RegQueryValueEx problem

August 16, 2010 at 14:18:28
Specs: Windows XP Pro
strange issue reading from reg, possibly a typecasting mistake? I dunno.

[code]

char AppDir[255];
char homedrive[255];
char homepath[255];

HKEY hKey;
LONG returnStatus;
DWORD dwType=REG_SZ;
DWORD dwSize=255;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\thirdpartyappnameremoved", 0L, KEY_ALL_ACCESS, &hKey);
RegQueryValueEx(hKey, "InstallLocation", NULL, &dwType,(LPBYTE)&AppDir, &dwSize);
RegCloseKey(hKey);
//for debug
cout << "AppDir: " << AppDir << endl;

RegOpenKeyEx(HKEY_CURRENT_USER, "Volatile Environment", 0L, KEY_ALL_ACCESS, &hKey);
RegQueryValueEx(hKey, "HOMEDRIVE", NULL, &dwType,(LPBYTE)&homedrive, &dwSize);
RegCloseKey(hKey);
//for debug
cout << "HomeDrive: " << homedrive << endl;

RegOpenKeyEx(HKEY_CURRENT_USER, "Volatile Environment", 0L, KEY_ALL_ACCESS, &hKey);
RegQueryValueEx(hKey, "HOMEPATH", NULL, &dwType,(LPBYTE)&homepath, &dwSize);
RegCloseKey(hKey);
//for debug
cout << "HomePath: " << homepath << endl;

cout << "merging..." << endl;
std::string userpath;
userpath = homedrive;
userpath.append(homepath);
cout << "result: " << userpath << endl;

system("pause");

[/code]

everything works except returning homepath, which returns a question mark "?"

I really don't understand why the other two registry calls work fine but not that one.

Any Assistance would be greatly appreciated.


See More: RegQueryValueEx problem

Report •


#1
August 16, 2010 at 15:17:13
Try rewriting the code to open the key and read the value and return the error code. The return error code with give you an indication of what went wrong. Result is a long integer.

Result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, hKey, 0#, KEY_READ, phkResult)
If Result = 0 Then


cbData = 0
Do
lpBuffer = String$(cbData, " ")
Result = RegQueryValueEx(phkResult, Key, 0#, REG_SZ, ByVal lpBuffer, cbData)
Loop While Result = ERROR_MORE_DATA

End If

Using this method you can use the same code to read any string value regardless of length, This is in VB syntax but should be able to adapt it to C++

ERROR_MORE_DATA is a Constant with the value of 234
No errors Returns 0

http://msdn.microsoft.com/en-us/lib...

Stuart


Report •

#2
August 16, 2010 at 18:18:32
Question: What's the value of dwSize after you look up the homedrive?

Report •

#3
August 17, 2010 at 04:16:10
VB syntax is fairly useless here O.o; It's written in c++. I do get the general idea though-- but it's something i've already tried to no avail.

RegOpenKeyEx(HKEY_CURRENT_USER, "Volatile Environment", 0L, KEY_ALL_ACCESS, &hKey);
returnStatus = RegQueryValueEx(hKey, "HOMEPATH", NULL, &dwType,(LPBYTE)&homepath, &dwSize);
RegCloseKey(hKey);
if (returnStatus == ERROR_SUCCESS) {
cout << "succeess" << endl;
cout << returnStatus << endl;
} else {
cout << "Failure" << endl;
cout << returnStatus << endl;
}

returns failure on homepath with a returnStatus value of 234

the other two succeed without issue. It's not opening volatile environment that fails, but the querying of homepath's value (which I verified is present, has a value, and is named correctly)

after AppDir dwSize is 27
after homedrive dwSize is 3
after homepath dwSize is 37

This value is the same regardless of whether or not dwSize is redefined as 255 prior to each use in function.

Still scratching my head... any ideas?
also, is there a getlasterror() function i can call here (in c++ please) in liu of matching long returnStatus to ERROR_SUCCESS?


Report •

Related Solutions

#4
August 17, 2010 at 05:04:22
Listen to Razor2.3 he is the most knowledgeable member you can get on this forum.


If you read the msdn reference for RegQueryValueEx you will find that the return value is the same value you can expect from GetLastError() and that after calling the function your dwSize will contain(because you pass a pointer) the number of bytes of buffer needed.

returns failure on homepath with a returnStatus value of 234

If you look up error 234 you will find the buffer you pass is insufficient to hold the data your requesting.

Also it wouldn't hurt to make your buffers one byte larger than needed so you can NULL terminate them if needed.

It might not be a bad idea to call this function with zero bytes first then use new to create the buffer(don't forget to call delete when finished).


Report •

#5
August 17, 2010 at 05:05:30
returns failure on homepath with a returnStatus value of 234

There is the problem. For some reason It is not reading the data complete the first time RegQueryValue is called.

You need to call RegQueryValue again with the receiving buffer set to the value of dwsize as I do inside a do loop.

I'm pretty sure C++ has a Do Loop construction of some kind,

I am pretty sure that setting all sizes to 255 is where your problem lies.

Stuart


Report •

#6
August 17, 2010 at 11:16:02
Here's Microsoft's method of reading the registry.

http://support.microsoft.com/kb/258556

You will notice the it call RegQueryValue twice. The first time to get the length of the string being read and the second time with the length of the receiving buffer correctly set.

Stuart


Report •

#7
August 17, 2010 at 12:11:30
someguysomewhere: after homedrive dwSize is 3
RegQueryValueEx(hKey, "HOMEPATH", NULL, &dwType,(LPBYTE)&homepath, &dwSize);

So let's see what's going on here:
You: Windows, give me the key type of "HOMEPATH", and its value. Oh, by the way, you only have three bytes to store that value.
Windows: Yeah, not going to happen. You need to give me a buffer of at least 37 bytes.


Report •

Ask Question