2

特定のキー/値のレジストリをチェックする Visual C++ 2005 ルーチンをコーディングしようとしています。C# を使用してコードを作成するのに問題はありませんが、C++ でコードが必要です。vs2005でc ++を使用してこれを行う方法を知っている人は誰でも。

どうもありがとうトニー

4

2 に答える 2

6

以下は、以下を取得するための疑似コードです。

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

コード例:

ライブラリの依存関係を含めます: Advapi32.lib

メインまたは値を読み取りたい場所に次を配置します。

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");

これらのラッパー関数をコードの先頭に配置します。

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;
}
于 2009-03-29T16:34:57.580 に答える
4

利用可能な Win32 API があります: MSDN for RegOpenKeyand friends (およびレジストリ関数全般) を参照してください。

「サブキーを含むキーの削除」の例を次に示します。

于 2009-03-29T16:29:22.320 に答える