How to check if a registry key exists C++ windows API


How to check if a registry key exists C++ windows API


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

//Assume hKey contains a valid HKEY obtained from calling RegOpenKeyEx(...)

LONG result = RegQueryValueEx(hKey, "somevaluename", NULL, NULL, NULL, NULL);
	
if (result == ERROR_SUCCESS) {
	printf("Found\r\n");
}
else if (result == ERROR_FILE_NOT_FOUND) {
	printf("Not found\r\n");
}
else {
	printf("There was an error\r\n");
}
Last edited on
Do you have a problem with this code? Or are yoy just presenting a useful fact for future use?

Andy

PS Note that (a) RegQueryValueEx returns a LONG value, and (b) RegQueryValueEx maps to either RegQueryValueExA or RegQueryValueExW depending on whether or not UNICODE is #defined. So the ANSI version would be better written as:

1
2
3
4
5
6
7
8
9
10
11
12
13
//Assume hKey contains a valid HKEY obtained from calling RegOpenKeyEx(...)

LONG result = RegQueryValueExA(hKey, "somevaluename", NULL, NULL, NULL, NULL);
	
if (result == ERROR_SUCCESS) {
	printf("Found\r\n");
}
else if (result == ERROR_FILE_NOT_FOUND) {
	printf("Not found\r\n");
}
else {
	printf("There was an error\r\n");
}
Useful fact
hmmm.... Good Work, although this should be posted as a article.

You can easily implement this into a Anti-Virus.
Topic archived. No new replies allowed.