2017-08-02 178 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

時,你可以張貼[MCVE]你又天真地鑄造stringLPBYTE。 –

+0

當處理以空字符結尾的字符串時,'sizeof(DWORD)'是錯誤的。它返回一個DWORD(指針?)的大小,而不是字符串的長度。除非你寫一個很短的字符串,否則這個值太小了。你也不是null結束你的字符串。 –

+0

您發佈了讀取/寫入DWORD值的代碼,而不是讀取/寫入字符串值失敗的代碼。 –

回答

0

與您的代碼的問題是,你是天真鑄造string型變量LPBYTE

這是你的代碼更正:

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和錯誤處理應該改進。

說明:

這是你的代碼SetVal

RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD)); 
  • 你天真地鑄造dataLPBYTE
  • sizeof(DWORD)是錯誤的,因爲你需要提供的字符串

的長度這是GetVal代碼:

string data; 
    DWORD size = sizeof(data); 
    DWORD type = REG_SZ; 
    LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size); 
  • sizeof(data)datastring不作任何感。它不是字符串的長度,無論如何它在那一刻都是0。
  • 調用RegQueryValueEx
+1

它仍然不適合我。仍然在註冊表中獲得一組中文字符。 –