读取注册表值32位?

时间:2014-04-16 01:08:12

标签: c++ mbcs

我有以下代码编译MBCS。

CString GetRegistry(LPCTSTR pszValueName)
{
    // Try open registry key
    HKEY hKey = NULL;
    LPCTSTR pszSubkey = _T("SOFTWARE\\Wow6432Node\\PAX");
    if ( RegOpenKey(HKEY_LOCAL_MACHINE, pszSubkey, &hKey) != ERROR_SUCCESS )
    {
        // Error:
        // throw an exception or something...
        //
        // (In production code a custom C++ exception 
        // derived from std::runtime_error could be used)
        AtlThrowLastWin32();
    }

    // Buffer to store string read from registry
    TCHAR szValue[1024];
    DWORD cbValueLength = sizeof(szValue);

    // Query string value
    if ( RegQueryValueEx(
            hKey,
            pszValueName, 
            NULL, 
            NULL, 
            reinterpret_cast<LPBYTE>(&szValue), 
            &cbValueLength) 
         != ERROR_SUCCESS )
    {
        // Error
        // throw an exception or something...
        AtlThrowLastWin32();
    }

    // Create a CString from the value buffer
    return CString(szValue);
}
  • 如何使代码也适用于32位计算机?
  • 如何将返回值放在一个简单的字符串中?前; string namevalue = GetRegistry(_T(“name”));

1 个答案:

答案 0 :(得分:1)

您需要将RegOpenKeyEx()KEY_WOW64_32KEY标志一起使用,并让它为您处理Wow6432节点,例如:

CString GetRegistry(LPCTSTR pszValueName)
{
    // WOW64 access
    REGSAM Wow64Flag;
    #ifdef _WIN64
    Wow64Flag = KEY_WOW64_32KEY;
    #else
    Wow64Flag = 0;
    #endif

    // Try open registry key
    HKEY hKey = NULL;

    LONG lResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\PAX"), 0, KEY_QUERY_VALUE | Wow64Flag, &hKey);
    if ( lResult != ERROR_SUCCESS )
    {
        // Error:
        // throw an exception or something...
        //
        // (In production code a custom C++ exception 
        // derived from std::runtime_error could be used)
        SetLastError(lResult);
        AtlThrowLastWin32();
    }

    DWORD cbValueLength;

    // Query string value size
    lResult = RegQueryValueEx(
            hKey,
            pszValueName, 
            NULL, 
            NULL, 
            NULL, 
            &cbValueLength) 
         != ERROR_SUCCESS )
    {
        // Error
        RegCloseKey(hKey);
        // throw an exception or something...
        SetLastError(lResult);
        AtlThrowLastWin32();
    }

    // Buffer to return string read from registry
    CString sValue;

    if ( cbValueLength > 0 )
    {
        // Buffer to store string read from registry
        std::vector<TCHAR> szValue((cbValueLength / sizeof(TCHAR))+1);

        lResult = RegQueryValueEx(
                hKey,
                pszValueName, 
                NULL, 
                NULL, 
                reinterpret_cast<LPBYTE>(&szValue[0]), 
                &cbValueLength) 
             != ERROR_SUCCESS )
        {
            // Error
            RegCloseKey(hKey);
            // throw an exception or something...
            SetLastError(lResult);
            AtlThrowLastWin32();
        }

        szValue[cbValueLength / sizeof(TCHAR)] = 0;
        sValue = &szValue[0];
    }

    RegCloseKey(hKey);

    return sValue;
}

当64位进程指定KEY_WOW64_32KEY或32位进程未指定任何WOW64标志时,它将访问32位注册表,因此SOFTWARE\PAX将解析为SOFTWARE\Wow6432Node\PAX 64位系统。