问题
I am trying to read a registry key from a Windows server, and I can't seem to get it to work either with or without leading slashes. If I try:
lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "\\SOFTWARE\\Company\\Product\\ServerName", 0, KEY_QUERY_VALUE, &hDomainKey);
It gives me error 161, which is ERROR_BAD_PATHNAME. (The specified path is invalid.)
Okay, so trying it this way:
lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Company\\Product\\ServerName", 0, KEY_QUERY_VALUE, &hDomainKey);
I get error 2, ERROR_FILE_NOT_FOUND. (The system cannot find the file specified.)
I can open regedit
and see the value I want to retrieve, with path My Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Company\Product
, name ServerName
, and type REG_SZ
. What am I missing here?
回答1:
Open the key, not the value:
lError = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
"SOFTWARE\\Company\\Product",
0,
KEY_QUERY_VALUE,
&hDomainKey);
and then read the value using RegQueryValueEx() (or RegGetValue()).
来源:https://stackoverflow.com/questions/13809003/regopenkeyex-giving-error-2-or-error-161-fails-both-ways