No that's not correct. Your main misunderstanding is how pointers work in C++. It's not enough to supply NULL for a pointer argument, you must supply a pointer to a variable so that the RegOpenKeyEx
or RegValueQueryEx
routine can return a value to that variable. You also seem to be misunderstanding how to assign to a CString
(no need for new
). Finally although it's not an error you don't need to do 'setup' you just pass the values directly to the function.
First open the key
HKEY key;
RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\blah", 0, 0, &key);
then get the value
DWORD type, size;
char data[99];
size = sizeof(data);
RegQueryValueEx(key, "SetupPath", 0, &type, (BYTE*)data, &size);
then assign the value to your CString
CString regval(data);
finally close the key
RegCloseKey(key);
No error checking in that code, you should add it. Also I'm assuming that any value you could get will fit in 99 bytes, that might not be true.
Note how I pass a pointer to the key
variable, so that RegOpenKeyEx
can return the key. I then use that key in the call to RegValueQueryEx
and RegCloseKey
. Same for the type
and size
variables. Also note that I've split the path between the calls to RegOpenKeyEx
and RegValueQueryEx
. I think that is correct.
Not 100% sure that is correct, I haven't tested it but should be quite a bit closer.