繁体   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