簡體   English   中英

在C ++中將字符串(REG_SZ)值寫入注冊表

[英]Writing string (REG_SZ) values to the registry in C++

我已經獲得了大部分用於向Windows注冊表寫入值的代碼,但是當我將路徑更改為虛擬鍵和我為測試設置的值時,它失敗了。 我的代碼如下:

    HKEY hKey;
    LPCTSTR sk = TEXT("SOFTWARE\TestSoftware");

    LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS , &hKey);

    if (openRes==ERROR_SUCCESS) {
        printf("Success opening key.");
    } else {
        printf("Error opening key.");
    }

    LPCTSTR value = TEXT("TestSoftwareKey");
    LPCTSTR data = "TestData\0";

    LONG setRes = RegSetValueEx (hKey, value, 0, REG_SZ, (LPBYTE)data, strlen(data)+1);

    if (setRes == ERROR_SUCCESS) {
        printf("Success writing to Registry.");
    } else {
        printf("Error writing to Registry.");
    }

    LONG closeOut = RegCloseKey(hKey);

    if (closeOut == ERROR_SUCCESS) {
        printf("Success closing key.");
    } else {
        printf("Error closing key.");
    }

所有三個測試都會產生錯誤狀態。

令我困惑的部分是我能夠在將其指向注冊表的其他部分時運行此代碼。 有任何想法嗎?

謝謝,布賴恩

我覺得很傻。 解決方案是需要正確地轉義字符串中的斜杠,如下所示:

LPCTSTR sk = TEXT("SOFTWARE\\TestSoftware");

希望有人發現這有用......

HKEY OpenKey(HKEY hRootKey, char* strKey)
{
  HKEY hKey;
  LONG nError = RegOpenKeyEx(hRootKey, strKey, NULL, KEY_ALL_ACCESS, &hKey);

  if (nError==ERROR_FILE_NOT_FOUND)
  {
    cout << "Creating registry key: " << strKey << endl;
    nError = RegCreateKeyEx(hRootKey, strKey, NULL, NULL,     REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL, &hKey, NULL);
  }

  if (nError)
    cout << "Error: " << nError << " Could not find or create " <<      strKey << endl;

  return hKey;
}

void SetintVal(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 GetintVal(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; 
  SetVal() is called.
  else if (nError)
    cout << "Error: " << nError << " Could not get registry value " << (char*)lpValue << endl;

  return data;
}

BOOL SetcharVal(HKEY Key,char* subkey,char* StringName,char* Stringdata)
{
  HKEY hKey = OpenKey(Key,subkey);

  LONG openRes = RegOpenKeyEx(Key, subkey, 0, KEY_ALL_ACCESS , &hKey);

  if (openRes==ERROR_SUCCESS) {

  } else {
    printf("Error opening key.");
  }

  LONG setRes = RegSetValueEx (hKey, StringName, 0, REG_SZ, (LPBYTE)Stringdata, strlen(Stringdata)+1);

  if (setRes == ERROR_SUCCESS) {

  } else {
    printf("Error writing to Registry.");
  }

  LONG closeOut = RegCloseKey(hKey);

  if (closeOut == ERROR_SUCCESS) {

  } else {
    printf("Error closing key.");
  }

}

char* GetCharVal(HKEY Key,char* subkey,char* StringName)
{
  DWORD dwType = REG_SZ;
  HKEY hKey = 0;
  char value[1024];
  DWORD value_length = 1024;
  RegOpenKey(HKEY_LOCAL_MACHINE,subkey,&hKey);
  RegQueryValueEx(hKey, StringName, NULL, &dwType, (LPBYTE)&value,     &value_length);
  RegCloseKey(hKey);
  return value;
}

我正在使用此代碼。

對於UNICODE環境:

//Writing to registry
HKEY hKey;
LPCTSTR sk = TEXT("SOFTWARE\\Microsoft\\Windows");

LONG openRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE, sk, 0, KEY_ALL_ACCESS, &hKey);

if (openRes == ERROR_SUCCESS) {
    printf("Success opening key.");
}
else {
    printf("Error opening key.");
}

LPCTSTR value = TEXT("Sample");
WCHAR path[80] = TEXT("C:\\samples\\app.exe");

LONG setRes = RegSetValueEx(hKey, value, 0, REG_SZ, (LPBYTE)path, sizeof(path));

if (setRes == ERROR_SUCCESS) {
    printf("Success writing to Registry.");
}
else {
    printf("Error writing to Registry.");
}

LONG closeOut = RegCloseKey(hKey);

if (closeOut == ERROR_SUCCESS) {
    printf("Success closing key.");
}
else {
    printf("Error closing key.");
}

暫無
暫無

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

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