Click to See Complete Forum and Search --> : RegQueryValueEx


Feered
June 17th, 2010, 12:58 AM
Okay, for some reason when I call RegQueryValueEx, my buffer pads every other byte with a space. So the output is something like "1 0 0 1 u s w e s t" and so on... I've tried a few different methods but can't seem to nail this one...

On top of that, when I cout the data, it only prints the first byte...


int main ()
{

HKEY hKey;
DWORD cType;
char lpData[1024] = {0};
DWORD buffersize = sizeof(lpData);

if(RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Blizzard Entertainment\\Warcraft III"),NULL,KEY_READ,&hKey) == ERROR_SUCCESS)
{
cout << "Opened successfully";
}else{
cout << "Failed to open reg key: " << GetLastError() << "\n";
}

RegQueryValueEx(hKey,TEXT("Battle.net Gateways"),NULL,&cType,(LPBYTE) lpData,&buffersize);

cout << "Registry Key Open: mem key location=" << hKey << "\n\n";
cout << "Data: " << lpData;

RegCloseKey (hKey);

system("Pause");

}

VictorN
June 17th, 2010, 01:29 AM
Okay, for some reason when I call RegQueryValueEx, my buffer pads every other byte with a space. So the output is something like "1 0 0 1 u s w e s t" and so on... Is it a UNICODE string?

Feered
June 17th, 2010, 07:05 AM
I fixed the space padding by going to the compilers settings and turning UNICODE to Not Set. However, now when I cout the buffer, it's only printing the first word.


--edit
I'm sorry, just realized it's not a space padding. It's a null padding after each word.

ovidiucucu
June 23rd, 2010, 06:52 AM
Your code contains few mistakes.
Take a look at next example and guess which are they. ;)

#include <windows.h>
#include <tchar.h>
#include <iostream>

#ifdef UNICODE
#define t_cout wcout
#else
#define t_cout cout
#endif

int _tmain()
{
HKEY hKey = NULL;
DWORD cType = REG_NONE;
TCHAR lpData[1024] = {0};
DWORD size = sizeof(lpData);

LONG lRet = RegOpenKeyEx(HKEY_CURRENT_USER,
_T("Software\\Blizzard Entertainment\\Warcraft III"),
NULL, KEY_QUERY_VALUE, &hKey);
if(ERROR_SUCCESS != lRet)
{
std::t_cout << _T("RegOpenKeyEx failed. Error: ") << lRet << std::endl;;
return lRet;
}

lRet = RegQueryValueEx(hKey, _T("Battle.net Gateways"), NULL, &cType, (LPBYTE)lpData, &size);
RegCloseKey(hKey);

if(ERROR_SUCCESS != lRet)
{
std::t_cout << _T("RegQueryValueEx failed. Error: ") << lRet << std::endl;;
return lRet;
}

if(REG_SZ != cType)
{
std::t_cout << _T("Value has not expected type REG_SZ") << std::endl;
return E_FAIL;
}

std::t_cout << "Data: " << lpData << std::endl;
system("Pause");
return ERROR_SUCCESS;
}

Maejie
June 26th, 2010, 04:06 PM
Hello, ovidiucucu

I'd like to get the Type of the data, (REG_SZ,REG_DWORD etc), :confused:

Thank you

VictorN
June 27th, 2010, 04:18 AM
RegQueryValueEx has a parameter:lpType
[out] Pointer to a variable that receives a code indicating the type of data stored in the specified value. For a list of the possible type codes, see Registry Value Types. The lpType parameter can be NULL if the type code is not required.