Hello!
I am trying to read a value from the registry, but I can't get it to work.
Everything compiles, but RegQueryValueEx keeps producing an error.

this is the code:

#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	HKEY hKey = 0;
    unsigned char buf[20];
    DWORD dwBufSize = 20;

    if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Activision\\Call of Duty 4"), NULL, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
		MessageBox(0, TEXT("Can't open key."), NULL, MB_OK);

	if(RegQueryValueEx(hKey, TEXT("codkey"), NULL, NULL, (LPBYTE)buf, &dwBufSize) != ERROR_SUCCESS)
		MessageBox(0, TEXT("Can't query key."),NULL, MB_OK);
	
	cout << "key value is '" << buf << "'\n";

    RegCloseKey(hKey);
    cin.ignore();
 
    return 0;
}

I have read through tonnes of examples, and can't figure out why this wont work =/

anyone have any ideas?

Edited 7 Years Ago by VBNick: n/a

I don't have that registry key, so I used a different one. This works

int _tmain(int argc, _TCHAR* argv[])
{
HKEY hKey = 0;
TCHAR buf[255] = {0};
DWORD dwBufSize = sizeof(buf)/sizeof(TCHAR);

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\DirectX"), 
   NULL, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
{
        MessageBox(0, TEXT("Can't open key."), NULL, MB_OK);
}
else
if(RegQueryValueEx(hKey, TEXT("Version"), NULL, NULL, (LPBYTE)buf, &dwBufSize) != ERROR_SUCCESS)
{
    MessageBox(0, TEXT("Can't query key."),NULL, MB_OK);
}
else
    wcout << "key value is '" << buf << "'\n";

RegCloseKey(hKey);
cin.ignore();

return 0;
}

Ok! we are making progress.
I don't get the error on querying the value anymore, but it seems like when I call
cout << "key value is '" << buf << "'\n";
its outputting the address of "buf" instead of its value(this value differs every time the app is run) with my code and yours, it shoots out an 8 digit hex number =/

the changes I made to mine were using TCHAR like yours, and RegQueryValueEx returns the error I was getting when I use buf[20] but not when I use buf[255].

Edited 7 Years Ago by VBNick: n/a

If you are compiling your program for UNICODE then you need to use wcout, not cout. See my example which was compiled for UNICODE. cout doesn't know how to print wchar_t*, which is what TCHAR is converted to.

Sweet! mission successful...

funny part is though: I was trying to make a quick key changer so My roomie could play COD4 with me online, but as it turns out, COD4 knows every time you chang your key...So your online rank gets reset to Noob every time you do it...LoLz..
thankfully its only to hold him over till our pre-orders of MW2 come out next month =D

but on another note, Ancient, I remember asking for help on this forum about a cheapy little 3D app I was making. All it was was a floor and a ceiling drawn with Polygon() lol....but wasnt that you who was helping out with it?

This article has been dead for over six months. Start a new discussion instead.