Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

c++ - RegOpenKeyEx fails on HKEY_LOCAL_MACHINE

Hi I'm trying to read a registry value that gives me the path to firefox.exe. This is stored under

HKEY_LOCAL_MACHINESOFTWAREMozillaMozilla Firefox 3.0.10in

(the version number can be found somewhere else)

But I cant seem to get RegOpenKeyEx to return ERROR_SUCCESS for anything under

HKEY_LOCAL_MACHINE

so this test fails:

if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("\SOFTWARE"),0,KEY_QUERY_VALUE,&keyHandle) == ERROR_SUCCESS)

while this test passes:

if(RegOpenKeyEx(HKEY_CLASSES_ROOT,TEXT("\Shell"),0,KEY_QUERY_VALUE,&keyHandle) == ERROR_SUCCESS)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The following code failed on my machine with the error code 161, which means "bad path" (look it up in winerror.h):

long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE"),
                      0,KEY_QUERY_VALUE, &hk );

I then changed the call to RegOpenKeyEx to use "SOFTWARE" (note no leading slashes) and it worked:

#include <windows.h>
#include <iostream>
using namespace std; 

int main() {
    HKEY hk;

    // Notice that it's SOFTWARE instead of \SOFTWARE:
    long n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE"),
                      0,KEY_QUERY_VALUE, &hk );
    if ( n == ERROR_SUCCESS ) {
        cout << "OK" << endl;
    }
    else {
        cout << "Failed with value " << n << endl;
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...