2017-08-02 60 views
0

C++のWindowsレジストリに文字列を書き込む/読み込む方法は?C++のレジストリに文字列を書き込んだり読んだりする

次のコードを使用して、WindowsレジストリにDWORD(数値)を書き込み/読み取ることができます。しかし、レジストリに中国語のような文字として格納されるので、文字列値を書き込み/読み込めません。

void SetVal(HKEY hKey, LPCTSTR lpValue, DWORD data) 
{ 
    LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); 

    if (nError) 
     cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl; 
} 

DWORD GetVal(HKEY hKey, LPCTSTR lpValue) 
{ 
    DWORD data;  DWORD size = sizeof(data); DWORD type = REG_DWORD; 
    LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size); 

    if (nError==ERROR_FILE_NOT_FOUND) 
     data = 0; // The value will be created and set to data next time SetVal() is called. 
    else if (nError) 
     cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl; 

    return data; 
} 

コードは(レジストリ内の文字のような中国人として格納)/読み込み、文字列値を書き込むために使用:

void SetVal(HKEY hKey, LPCTSTR lpValue, string data) 
{ 
    LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_SZ, (LPBYTE)&data, sizeof(data)); 

    if (nError) 
     cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl; 
} 

string GetVal(HKEY hKey, LPCTSTR lpValue) 
{ 
    string data;  DWORD size = sizeof(data); DWORD type = REG_SZ; 
    LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size); 

    if (nError==ERROR_FILE_NOT_FOUND) 
     data = "0"; // The value will be created and set to data next time SetVal() is called. 
    else if (nError) 
     cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl; 

    return data; 
} 
+0

を呼び出すときに

  • あなたは再びstringLPBYTEに単純にキャストされています。 –

  • +0

    ヌル終了文字列を扱うときに 'sizeof(DWORD)'が間違っています。文字列の長さではなく、DWORD(ポインタ?)のサイズを返します。あなたが非常に短い文字列を書いていない限り、値は非常に小さいです。あなたもあなたの文字列を終了するnullではありません。 –

    +0

    失敗したコードの読み込み/書き込み文字列値ではなく、DWORD値の読み取り/書き込み用のコードを投稿しました。 –

    答えて

    0

    あなたのコードの問題は、あなたが単純LPBYTEstring型変数をキャストしていることです。まだ改善の余地がある

    void SetVal(HKEY hKey, LPCTSTR lpValue, string data) 
    { 
        const char *x = data.c_str(); 
        LONG nError = RegSetValueEx(hKey, lpValue, NULL, REG_SZ, (LPBYTE)data.c_str(), data.size()); 
    
        if (nError) 
        cout << "Error: " << nError << " Could not set registry value: " << (char*)lpValue << endl; 
    } 
    
    string GetVal(HKEY hKey, LPCTSTR lpValue) 
    { 
        string data; 
    #define MAXLENGTH 100 
    
        char buffer[100]; 
        DWORD size = sizeof(buffer); 
        DWORD type = REG_SZ; 
    
        LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)buffer, &size); 
    
        if (nError == ERROR_FILE_NOT_FOUND) 
        { 
        data = "0"; // The value will be created and set to data next time SetVal() is called. 
        return data; 
        } 
        else if (nError) 
        cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl; 
    
        data = buffer; 
        return data; 
    } 
    

    、例えば文字列の最大長は100に制限され、エラー処理が改善されなければならない:

    これは修正して、あなたのコードです。

    説明:

    これはあなたのコードである

    RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); 
    
    • SetValであなたは単純にLPBYTEdataをキャストしています。

      string data; 
          DWORD size = sizeof(data); 
          DWORD type = REG_SZ; 
          LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size); 
      
      • sizeof(data)dataがあるときstringはいずれも行いません。あなたは、文字列

      の長さを提供する必要があるため

    • sizeof(DWORD)これはGetValであなたのコードで、間違っていますセンス。それはストリングの長さではなく、その瞬間には0である。あなたは[MCVE]を投稿することでしたRegQueryValueEx
    +1

    それはまだ私のために働いていない。まだレジストリに漢字のセットを取得しています。 –

    関連する問題