2014-04-16 2 views
0

다음 코드는 MBCS 용으로 컴파일됩니다.레지스트리 값 32 비트를 읽으시겠습니까?

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 비트 컴퓨터도 코드가 작동 할 수
      ?
    • 어떻게 리턴을 간단한 문자열에 넣을 수 있습니까? 전의; 문자열 namevalue = GetRegistry (_T ("name"));
  • 답변

    1

    당신은 KEY_WOW64_32KEY 플래그 RegOpenKeyEx()를 사용하고, 당신을 위해 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은 64 비트 시스템에서 SOFTWARE\Wow6432Node\PAX으로 해결됩니다.

    +0

    하위 키 위치는 어디에 있습니까? – user3513035