简体   繁体   English

在C ++中读取Windows注册表项REG_BINARY

[英]Reading Windows Registry Key REG_BINARY in c++

I'm trying to read a registry key in c++, that's my function: 我正在尝试读取C ++中的注册表项,这是我的功能:

    DWORD regkey()
{
    HKEY hKey;
    DWORD dwDisp = REG_BINARY;
    DWORD dwSize = sizeof(dwDisp);
    DWORD dwValue = 0;
    DWORD dwReturn;
    DWORD dwBufSize = sizeof(dwDisp);

    if( RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HERE\\IS\\THE\\REGKEY",0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
    {
        DWORD error = RegQueryValueEx(hKey,L"key",0,0, (LPBYTE)&dwReturn, &dwBufSize);
        if(error == ERROR_SUCCESS)
        {
            return dwReturn;
        }
    }

    RegCloseKey(hKey);

    return 0;
}

but it's returning nothing... please help me. 但它什么也没返回...请帮助我。

The registry functions will return a meaningful error code, and that can help you diagnose the problem. 注册表函数将返回有意义的错误代码,它可以帮助您诊断问题。 Try holding on to that code: 尝试保留该代码:

{
    HKEY hKey;
    DWORD dwDisp = REG_BINARY;
    DWORD dwSize = sizeof(dwDisp);
    DWORD dwValue = 0;
    DWORD dwReturn;
    DWORD dwBufSize = sizeof(dwReturn);

    DWORD dwError = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"HERE\\IS\\THE\\REGKEY",0, KEY_READ, &hKey) ;
    if( dwError == ERROR_SUCCESS)
    {
        dwError = RegQueryValueEx(hKey,L"key",0,0, (LPBYTE)&dwReturn, &dwBufSize);
        if(error == ERROR_SUCCESS)
        {
            // it worked!
        }
        else
        {
            // it failed to read, check dwError for the error code
            dwResult = 0;
        }

        RegCloseKey(hKey);
    }
    else
    {
        // it failed to open, check dwError for the error code
        dwResult = 0;
    }


    return 0;
}

If you're using Visual Studio, you can break on any of the failure points and evaluate dwError,hr in your watch window. 如果使用的是Visual Studio,则可以中断任何故障点并在监视窗口中评估dwError,hr The ,hr format specifier causes the debugger to look up the error code for you and present a meaningful string that describes the problem. ,hr格式说明符使调试器为您查找错误代码,并提供描述问题的有意义的字符串。 That should lead you to an understanding of what went wrong. 那应该使您了解出了什么问题。

If you can tell us which function is failing and which code you're getting back from that function, we might be able to provide more detailed help. 如果您可以告诉我们哪个功能失败了,以及您从该功能找回了哪些代码,我们也许可以提供更详细的帮助。 As it stands now, you've presented us with a bit of a guessing game. 就目前而言,您已经向我们展示了一些猜谜游戏。 Maybe you've misspelled your registry key name or given an incorrect path. 也许您拼错了注册表项名称或给出了错误的路径。 Your code seems to imply you're passing the registry key RegQueryValueEx() , but you're meant to pass a value name, not a key name, to that function. 您的代码似乎暗示您正在传递注册表项RegQueryValueEx() ,但您要向该函数传递值名称,而不是键名称。 Maybe you have a problem with access privileges because you're looking at a protected part of the registry and not running as an account with enough rights to read that key. 可能您的访问权限有问题,因为您正在查看注册表的受保护部分,而没有作为具有足够权限读取该密钥的帐户运行。 (And so, you should pass KEY_READ instead of KEY_ALL_ACCESS .) (因此,您应该传递KEY_READ而不是KEY_ALL_ACCESS 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM