Get a registry info

Hello,

I'm kinda new to C++ and need help.
Can anyone tell me please, how can I get the value of port number of remote desktop which is in: 'HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp: PortNumber'
I would like to know a DEC number of port.
I've done something like that:

1
2
3
4
5
6
7
8
9
10
11
12
HKEY hKey;				
DWORD buffersize = 1024;	
int* hkey = new int[buffersize];
RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp",NULL,KEY_READ,&hKey);
	
RegQueryValueEx(hKey,"PortNumber",NULL,NULL,(LPBYTE) hkey,&buffersize);
	
std::cout << "Port number = " << hkey << "\n\n";
RegCloseKey (hKey);
system("Pause");
delete hkey;

but it's not what I want of course...and not correct at all =D
And I'd like to have function to change the port as well.
Thanks
Last edited on
regedit tells me that PortNumber is a REG_DWORD, but your type parameter to RegQueryValueEx() (parameter 4) is set to NULL.
hkey needs to be merely the size of a DWORD, why are you allocating sizeof(int)*1024 bytes? And buffersize is also wrong because it needs to be the size of the buffer in bytes, not in ints.
Rewrite it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
DWORD port;
//...
RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	REG_DWORD,
	(BYTE *)port,
	/*
	since DWORD has a fixed size, this could also be 4, but let's do it classy,
	shall we? sizeof(port) also works.
	*/
	sizeof(DWORD)
);
Well yes I know it's REG_DWORD but I didn't know how to set it. I've actaully found similar code on one website but it was made for string and then I just tried to modified for REG_DWORD but didn't know how to.

I still have a problem...
Is 'RegOpenKeyEx' same as I typed? Then 'RegQueryValueEx' is like u typed and what have I to modify also?

Could u please type the whole code? I'd be very grateful to u.

Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
HKEY hKey;				
DWORD port;
RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp",NULL,KEY_READ,&hKey);
RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	REG_DWORD,
	(BYTE *)port,
	/*
	since DWORD has a fixed size, this could also be 4, but let's do it classy,
	shall we? sizeof(port) also works.
	*/
	sizeof(DWORD)
);
std::cout << "Port number = " << port << "\n\n";
RegCloseKey (hKey);
Last edited on
Thanks but I see there is another problem because I've typed the same code but still get errors.

reginfo.cpp:16: error: invalid conversion from `int' to `DWORD*'
reginfo.cpp:16: error: invalid conversion from `unsigned int' to `DWORD*'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <windows.h>
#include <iostream>

int main(){
	HKEY hKey;				
	DWORD port;
	RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
	"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp",NULL,KEY_READ,&hKey);
	RegQueryValueEx(
		hKey,
		"PortNumber",
		NULL,
		REG_DWORD,
		(BYTE *)port,
		sizeof(DWORD)
	);
	std::cout << "Port number = " << port << "\n";
	RegCloseKey (hKey);
}


Should I declare anything as first as well?
Sorry if I nag you but I just don't know how to...
Last edited on
I misread.
1
2
3
4
5
6
7
8
9
DWORD size=sizeof(DWORD);
RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	REG_DWORD,
	(BYTE *)port,
	&size
);
Aww, there is still one error
error: invalid conversion from `int' to `DWORD*'


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

int main(){
	HKEY hKey;				
	DWORD port;
	RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
	"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp",NULL,KEY_READ,&hKey);
	DWORD size=sizeof(DWORD);
	RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	REG_DWORD,
	(BYTE *)port,
	&size);
	std::cout << "Port number = " << port << "\n";
	RegCloseKey (hKey);
}

(BYTE *)port,

Shout be

(BYTE *)&port,

Hmm, no difference...compiler still posts the same error (error: invalid conversion from `int' to `DWORD*')...
Sometimes I feel like punching myself.
Sorry. I was reading the documentation completely wrong.
1
2
3
4
5
6
7
8
RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	NULL,
	(BYTE *)port,
	&size
);
Well no errors now but it doesn't work correctly, it always prints different values, but value should be 3389 which is 3D3 in HEX by default. I'd like that, the program tells me, what's the number of the port in DEC.
Any ideas?
Last edited on
This isn't my day at all.
Do you see line 6 in my last post? Well, I forgot the ampersand again.
(BYTE *)&port,
Ooh yeahh :P thank you so much!

But there is still one thig I don't know. I want function to change the port, something like this code, but I don't know how to use and which function use to change the value:
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
#include <windows.h>
#include <iostream>
using namespace std;

int main(){
	char i;
	HKEY hKey;				
	DWORD port;
	RegOpenKeyEx (HKEY_LOCAL_MACHINE, 
	"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp", NULL,KEY_READ,&hKey);
	DWORD size=sizeof(DWORD);
	RegQueryValueEx(
	hKey,
	"PortNumber",
	NULL,
	NULL,
	(BYTE *)&port,
	&size
	);
	cout << "Port number = " << port << '\n';
	cout << "Type \'c\' to change the port: ";
	cin >> i;
	if(i=='c'){
		cout << "Type the port number: ";
		cin >> x;//what is the variable?
		RegReplaceKey // or 'RegSetKeyValue'?
		(HKEY_LOCAL_MACHINE, 
		"SYSTEM\\CurrentControlSet\\Control\\Terminal Server\\WinStations\\RDP-Tcp","PortNumber" /*...?*/);
		
		RegSaveKey( //where and how to use this one?
		HKEY hKey,
		"PortNumber", 
		NULL
		)
		cout << "Port has been changed to: "<<port<<'\n';
		system("pause");
	}
	RegCloseKey (hKey);
}

In case if anyone have anytime I beg to help me.
Last edited on
1
2
3
4
5
6
7
8
LONG WINAPI RegSetValueEx(
  __in          HKEY hKey, //key handle
  __in          LPCTSTR lpValueName, //name of the value
  DWORD Reserved,
  __in          DWORD dwType, //type of the value that will be written
  __in          const BYTE* lpData, //pointer to data
  __in          DWORD cbData //size of data
);
Last edited on
Yeah, that's what hav I found in msdn, but problem is because I'm not so advancet to do that. And I also know taht I'm annoying and bothering u and u r not here to program for others, but I'm trying to make it correctly, I googled everything posssible to get right.
So first of all I set variable for cin, for example let's say it's 'x'. Then after 'cin' facility is RegSetValueEx which I don't kno how to set:
1
2
3
4
5
6
7
8
RegSetValueEx(
		hKey,
		"PortNumber", //i think so...
		0, //must me 0, ain't it?
		REG_DWORD, // that, right?
		const BYTE* "PortNumber", //is the lpData "PortNumber" here?
		cbData //what do I use for size and how to set
		);


I see lpData is the data to be stored but idk how to set it
and cbData (The size of the information pointed to by the lpData parameter, in bytes) I also don't kno how to set correctly...

and after RegSetValueEx, do I need to save the value with RegSaveKey or just print 'port' or again call function RegQueryValueEx after cout?
Last edited on
Not quite.
lpData is a pointer to the data that you want to write to that value, and cdData is the size of that data.
In this case, you want to write a DWORD, which are always 32-bits long, so as cbData, you'll just pass a 4, but, in case the size of DWORD ever changed, for whatever reason, you'll pass size, instead, which you already have from the previous example I gave you a few posts ago.
Now, let's suppose you've written the new port number in the variable port. You pass a pointer to port, casted to a 'BYTE *'.
The resulting line would be:
1
2
3
4
5
6
7
8
RegSetValueEx(
	hKey,
	TEXT("PortNumber"), //This was right, but it's better to add the TEXT macro.
	0, //OK
	REG_DWORD, // OK
	(BYTE*)&port,
	size //This had been set to sizeof(DWORD), remember?
);


The TEXT macro:
Some compilers define UNICODE by default, so if you were to pass the string literal just like that, you'd get a compiler error (wchar_t string literals are written L"like this", with an upper case L at the beginning).
I forgot to mention this before. Add it to the previous API calls.
O, thanks for this explanation.
Ok, variable for new input for port is sets to 'port' (cin >> port).
But should I do anything else after RegSetValueEx because it doesn't save the value. And of course if I print 'cout << port' after RegSetValueEx, I get the value I've just inputed because it's just variable but if I run prog. again then I see that port is still same as before.
Btw, I just need this program, otherwise I'm learning C++ from a book and I'm not that far to know such things :)
Compare the return value to ERROR_SUCCESS. If it's not equal, it means it failed.
It would be odd that it failed if opening the key didn't, though.
Topic archived. No new replies allowed.