An HKEY
is not a string, rather it is an integer value. So, what happens is that your program treats hKey
as if it were a null-terminated array of char
and presumably that's what leads to the runtime failure.
You must use an appropriate format string for the type of data that you have. This is one of the pitfalls of printf
and is one of the reasons why, if you can, you should prefer the C++ stream output mechanism.
Note that HKEY
is pointer sized. So if you want your code to work for both 32 and 64 bit processes then you will need to allow for that in your format string. The easiest thing is to treat it as a pointer and use %p
.
You should also make sure that you perform error checking. You are ignoring the return value of RegOpenKeyEx
, and you must not do that. I would not be at all surprised if the real problem with your code was revealed simply by paying heed to the value returned by the function.
And don't use magic constants for the samDesired
parameter. Specify it by combining flags using the bitwise or operator. Or in your case, pass KEY_READ
.