簡體   English   中英

在C ++中的注冊表中寫入/讀取字符串

[英]Writing/Reading string in Registry in C++

如何在C ++中的Windows注冊表中寫入/讀取字符串?

我可以使用以下代碼在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;
}

代碼的問題在於,您天真地將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));
  • 您天真地將data投射到LPBYTE
  • sizeof(DWORD)是錯誤的,因為您需要提供字符串的長度

這是您在GetVal的代碼:

    string data;
    DWORD size = sizeof(data);
    DWORD type = REG_SZ;
    LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
  • datastring時, sizeof(data)沒有任何意義。 它不是字符串的長度,那時候無論如何都是0。
  • 您再次在調用RegQueryValueEx時將天真stringLPBYTE

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM