简体   繁体   English

在C ++中的注册表中写入/读取字符串

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

How to write/read a string in windows registry in C++ ? 如何在C ++中的Windows注册表中写入/读取字符串?

I am able to write/read DWORD (number) in windows registry using the following code. 我可以使用以下代码在Windows注册表中写入/读取DWORD(数字)。 But, unable to write/read a string value, as it gets stored as chinese like characters in the registry. 但是,无法写入/读取字符串值,因为它像汉字一样存储在注册表中。

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;
}

Code used to write/read string value (Stored as chinese like characters in the registry): 用于写入/读取字符串值的代码(在注册表中以中文之类的字符存储):

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;
}

The problem with your code is that you are naively casting string type variables to LPBYTE . 代码的问题在于,您天真地将string类型的变量转换为LPBYTE

This is your code with corrections: 这是经过更正的代码:

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;
}

There is still room for improvement, for example the maximum string length is limited to 100 and the error handling should be improved. 仍有改进的空间,例如,最大字符串长度限制为100,并且应改进错误处理。

Explanations: 说明:

This is your code in SetVal 这是您在SetVal的代码

RegSetValueEx(hKey, lpValue, NULL, REG_DWORD, (LPBYTE)&data, sizeof(DWORD));
  • you are naively casting data to LPBYTE . 您天真地将data投射到LPBYTE
  • sizeof(DWORD) is wrong, because you need to provide the length of the string sizeof(DWORD)是错误的,因为您需要提供字符串的长度

This is your code in GetVal : 这是您在GetVal的代码:

    string data;
    DWORD size = sizeof(data);
    DWORD type = REG_SZ;
    LONG nError = RegQueryValueEx(hKey, lpValue, NULL, &type, (LPBYTE)&data, &size);
  • sizeof(data) when data is a string doesn't make any sense. datastring时, sizeof(data)没有任何意义。 It is not the length of the string, which at that moment is 0 anyway. 它不是字符串的长度,那时候无论如何都是0。
  • you are again casting naively string to LPBYTE when calling RegQueryValueEx 您再次在调用RegQueryValueEx时将天真stringLPBYTE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM