2009-03-29 6 views
2

レジストリで特定のキー/値をチェックするVisual C++ 2005ルーチンをコード化しようとしています。 私はC#を使ってコードを書いても問題はありませんが、C++でそれが必要です。 誰もがvs2005でC++を使ってこれを行う方法を知っています。レジストリ値が存在するかどうかの確認Visual C++ 2005

感謝 トニー

答えて

6

は以下を取得するには、いくつかの擬似コードです:

  1. レジストリキーがデフォルトの値はそのレジストリの文字列値が
  2. は何
  3. キーは何
  4. が存在する場合
  5. DWORD値とは何か

コード例:

は、ライブラリの依存関係を含める:先頭にこれらのラッパー関数を入れ

HKEY hKey; 
LONG lRes = RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Perl", 0, KEY_READ, &hKey); 
bool bExistsAndSuccess (lRes == ERROR_SUCCESS); 
bool bDoesNotExistsSpecifically (lres == ERROR_FILE_NOT_FOUND); 
std::wstring strValueOfBinDir; 
std::wstring strKeyDefaultValue; 
GetStringRegKey(hKey, L"BinDir", strValueOfBinDir, L"bad"); 
GetStringRegKey(hKey, L"", strKeyDefaultValue, L"bad"); 

:Advapi32.lib

はあなたのメインの中で次のように置くか、値を読みたい場所あなたのコード:

LONG GetDWORDRegKey(HKEY hKey, const std::wstring &strValueName, DWORD &nValue, DWORD nDefaultValue) 
{ 
    nValue = nDefaultValue; 
    DWORD dwBufferSize(sizeof(DWORD)); 
    DWORD nResult(0); 
    LONG nError = ::RegQueryValueExW(hKey, 
     strValueName.c_str(), 
     0, 
     NULL, 
     reinterpret_cast<LPBYTE>(&nResult), 
     &dwBufferSize); 
    if (ERROR_SUCCESS == nError) 
    { 
     nValue = nResult; 
    } 
    return nError; 
} 


LONG GetBoolRegKey(HKEY hKey, const std::wstring &strValueName, bool &bValue, bool bDefaultValue) 
{ 
    DWORD nDefValue((bDefaultValue) ? 1 : 0); 
    DWORD nResult(nDefValue); 
    LONG nError = GetDWORDRegKey(hKey, strValueName.c_str(), nResult, nDefValue); 
    if (ERROR_SUCCESS == nError) 
    { 
     bValue = (nResult != 0) ? true : false; 
    } 
    return nError; 
} 


LONG GetStringRegKey(HKEY hKey, const std::wstring &strValueName, std::wstring &strValue, const std::wstring &strDefaultValue) 
{ 
    strValue = strDefaultValue; 
    WCHAR szBuffer[512]; 
    DWORD dwBufferSize = sizeof(szBuffer); 
    ULONG nError; 
    nError = RegQueryValueExW(hKey, strValueName.c_str(), 0, NULL, (LPBYTE)szBuffer, &dwBufferSize); 
    if (ERROR_SUCCESS == nError) 
    { 
     strValue = szBuffer; 
    } 
    return nError; 
} 
関連する問題