Can't read registry key c++

hello i have this code to read a registry key that exist (i've created it before) but i get some errors
here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <windows.h>

using namespace std;

string path;

int main() {
      HKEY key;

    if(RegOpenKey(HKEY_CURRENT_USER,TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion"),&key)!=ERROR_SUCCESS){

            cout<<"Error 2x01\n";
        }
        DWORD value_lenght=SIZE;
        char valore[SIZE];
        RegQueryValueEx(key,"path",NULL,REG_SZ,(LPBYTE)&valore,&value_lenght);
        cout<<"path is "<<valore<<endl;


    ShellExecute(NULL,"open",valore.c_str(),NULL,NULL,SW_SHOWMINIMIZED);


}
i think that this code is right but i get a lot of error starting from the char value[SIZE];
why?
Last edited on
and what is "SIZE"?
yeah, i tought that it must be declared frist but in this website http://www.dreamincode.net/forums/topic/171917-how-to-setread-registry-key-in-c/
(where i'm learning registry operations) isn't declared...
so just a int SIZE before?
I don't know where you got that code from but ...

Never use RegOpenKey, that's a call from OLE1 from Win16. You should be using RegOpenKeyEx.

Start looking up a registry value that actually exists. Run up regedit to find one.

Don't use ShellExecute to try to run any old crap on your computer, especially if you're using a local admin account. That's the sort of thing that gives Windows a bad name.

To get you started, here's a working program that calls the relevant SDK functions to read the registry properly.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <windows.h>
#include <tchar.h>
#include <iostream>

int _tmain()
{
	DWORD dwRet;
	HKEY hKey;

	dwRet = RegOpenKeyEx(
				HKEY_CURRENT_USER,
				_T("Control Panel\\Desktop"),
				NULL,
				KEY_QUERY_VALUE,
				&hKey);
	if (dwRet != ERROR_SUCCESS)
	{
		std::clog
			<< "Error: RegOpenKeyEx:"
			<< " dwRet=" << dwRet
			<< " GetLastError=" << GetLastError() << "\n";
		return 1;
	}

	const DWORD SIZE = 1024;
	TCHAR szValue[SIZE] = _T("");
	DWORD dwValue = SIZE;
	DWORD dwType = 0;
	dwRet = RegQueryValueEx(
				hKey,
				_T("OriginalWallpaper"),
				NULL,
				&dwType,
				(LPBYTE)&szValue,
				&dwValue);
	if (dwRet != ERROR_SUCCESS)
	{
		std::clog
			<< "Error: RegQueryValueEx:"
			<< " dwRet=" << dwRet
			<< " GetLastError=" << GetLastError() << "\n";
		return 1;
	}
	if (dwType != REG_SZ)
	{
		std::clog << "Value not a string\n";
		return 1;
	}

#ifdef _UNICODE
	std::wcout << "szValue=" << szValue << "\n";
#else
	std::cout << "szValue=" << szValue << "\n";
#endif

	RegCloseKey(hKey);
	hKey = NULL;
}
Last edited on
Topic archived. No new replies allowed.