RegOpenKeyEx giving error 2 or error 161, fails bo

2020-03-26 11:29发布

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条回答
Rolldiameter
2楼-- · 2020-03-26 12:15

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()).

查看更多
登录 后发表回答