RegOpenKeyEx giving error 2 or error 161, fails both ways

孤人 提交于 2019-12-01 11:59:00

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!