簡體   English   中英

C ++將DWORD值寫入某個注冊表項的所有子項

[英]C++ Writing a DWORD value to all subkeys of a certain registry key

我很難使這段代碼對我有用。

現在 :在注冊表中的Interface文件夾(注冊表項)中添加2個DWORD值。

期望的 :我希望它將這兩個DWORD值添加到接口注冊表項(文件夾)的所有子項(子文件夾)中。

我有這個偽代碼:

  • 使用RegOpenKey或RegOpenKeyEx打開父鍵
  • 在循環中使用RegEnumKey或RegEnumKeyEx枚舉父級的所有子級鍵
  • 對於每個子鍵,使用RegSetValueEx設置所需的值
  • 使用RegCloseKey關閉父鍵

我將繼續嘗試進行排序,但也許有人可以提供幫助?

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <windows.h>
#include <iostream>

using std::cout;
using std::endl;

HKEY OpenKey(HKEY hRootKey, wchar_t* 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 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;
}

int main()
{
    static DWORD v1, v2;
    HKEY hKey = OpenKey(HKEY_LOCAL_MACHINE,L"SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces\\");
    v1 = GetVal(hKey, L"Registry Value1");
    v2 = GetVal(hKey, L"Registry Value2");
    v1 += 5;
    v2 += 2;
    SetVal(hKey, L"Registry Value1", v1);
    SetVal(hKey, L"Registry Value2", v2);
    RegCloseKey(hKey);
    return 0;
}

這是一個基本示例,沒有任何額外內容:

// open desired key whose subkeys shall be enumerated
HKEY hKey={0};
LPCTSTR path=TEXT("SYSTEM\\CurrentControlSet\\services\\Tcpip\\Parameters\\Interfaces");
if(RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_ENUMERATE_SUB_KEYS,&hKey) != ERROR_SUCCESS)
    return; // failed to open
DWORD index=0;           // enumeration index
TCHAR keyName[256]={0};  // buffer to store enumerated subkey name
DWORD keyLen=256;        // buffer length / number of TCHARs copied to keyName
// enumerate subkey names of hKey, result stored in keyName, keyLen set to strlen(keyName)
while(RegEnumKeyEx(hKey,index++,keyName,&keyLen,0,0,0,0) == ERROR_SUCCESS) {
    keyLen=256; // reset buffer length (RegEnumKeyEx changes this value)
    // open the subkey and set the desired value(s)
    HKEY hSubKey={0};
    if(RegOpenKeyEx(hKey,keyName,0,KEY_SET_VALUE,&hSubKey) == ERROR_SUCCESS) {
        // set desired value(s):
        DWORD myValue = 0xCAFEBABE;
        //RegSetValueEx(hSubKey,TEXT("MyValueName"),0,REG_DWORD,(LPBYTE)&myValue,sizeof(DWORD));
        RegCloseKey(hSubKey); // close sub key
    } 
    // else: failed to open subkey
}
// RegEnumKeyEx either returns ERROR_SUCCESS, ERROR_NO_MORE_ITEMS, or a system error code
RegCloseKey(hKey); // close key

請注意,此示例不評估錯誤代碼。 它僅演示枚舉子鍵和設置值的過程。 RegOpenKeyEx訪問權限設置為執行此任務所需的最低權限(將它們設置為您希望對打開的鍵進行的操作)。 while循環與ERROR_NO_MORE_ITEMS(一旦不再有要枚舉的子項)或實際錯誤沒有區別。 RegSetValueEx已出於安全考慮被注釋掉,其返回值將被忽略。

暫無
暫無

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

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