簡體   English   中英

如何使用 C++ 和 Windows 注冊表 API 在 Windows 注冊表編輯器鍵中添加字符串值/名稱數據對

[英]How to add a String Value/ Name Data pair in Windows Registry Editor key using C++ and Windows Registry API's

我想使用 C++ 代碼將字符串名稱及其值添加到 Windows 注冊表,以停止 Firefox 以外的瀏覽器停止運行。 我計划為所有瀏覽器循環編輯 Windows 注冊表,但目前我只為 Chrome 實現它。

我下面的當前代碼是向Default字符串添加一個值,但它不起作用,但我想創建一個Debugger字符串並將其設置為chrome.exe子項中的"ntsd -cq" ,如下圖所示。

圖片

這是我的代碼,它為Default字符串添加"ntsd -cq" ,而不是創建新的Debugger字符串。 我無法從微軟的文檔中清楚地了解如何使用 C/C++ 實現這一點。 我見過一些通過命令行完成的解決方案,但同事們非常有道德,他們不喜歡這種方式。

#include <Windows.h>
#include <iostream>

int main()
{
    HKEY hkey;
    LSTATUS ReturnValue = 0;
    LPCTSTR Directory = TEXT("SOFTWARE\\Microsoft\\Windows\ NT\\CurrentVersion\\Image\ File\ Execution\ Options\\chrome.exe");
    ReturnValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Directory, 0, KEY_ALL_ACCESS, &hkey);
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegOpenKeyEx failed\n");
        printf("%d\n", GetLastError());
        return -1;
    }
    //LPCSTR value = "Debugger";
    //ReturnValue = RegQueryValueEx(HKEY_LOCAL_MACHINE, Directory, NULL, NULL, value, sizeof(value));
    
    LPCSTR Value = "ntsd\ -c\ q\0";
    ReturnValue = RegSetValueA(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Windows\ NT\\CurrentVersion\\Image\ File\ Execution\ Options\\chrome.exe",REG_SZ, Value, sizeof(Value));
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegStatusValueA failed\n");
        printf("%d\n", GetLastError());
        return -1;
    }
    
    ReturnValue = RegCloseKey(hkey);
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegCloseKey failed\n");
        printf("%d\n", GetLastError());
        return -1;
    }
    return 0;
}

KEY_ALL_ACCESS需要管理員權限。 在這種情況下,您真正需要的只是KEY_SET_VALUE 不要請求比實際需要更多的權限。 但是,您仍然需要管理員權限才能開始寫入HKEY_LOCAL_MACHINE 因此,請確保您以提升的用戶身份運行代碼。

在任何情況下,您的字符串文字都有一些不屬於的\ 但更重要的是,您對RegSetValueA()的使用對於此任務是完全錯誤的。 這是一個已棄用的 API ,它只能寫入(Default)字符串而不能寫入其他任何內容(而且,你甚至沒有正確調用它,你需要strlen(Value)+1而不是sizeof(Value) - 這並不重要因為無論如何都會忽略該參數)。

為了創建Debugger字符串值,您需要改用RegSetValueEx() ,例如:

#include <Windows.h>
#include <iostream>

int main()
{
    HKEY hkey;
    LPCTSTR Directory = TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\chrome.exe");
    LSTATUS ReturnValue = RegOpenKeyEx(HKEY_LOCAL_MACHINE, Directory, 0, KEY_SET_VALUE, &hkey);
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegOpenKeyEx failed: %u\n", GetLastError());
        return -1;
    }
    LPCTSTR Value = TEXT("ntsd -c q");
    ReturnValue = RegSetValueEx(hkey, TEXT("Debugger"), 0, REG_SZ, (const BYTE*)Value, (lstrlen(Value)+1) * sizeof(TCHAR));
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegSetValueExA failed: %u\n", GetLastError());
        RegCloseKey(hkey);
        return -1;
    }
    
    ReturnValue = RegCloseKey(hkey);
    if (ERROR_SUCCESS != ReturnValue)
    {
        printf("RegCloseKey failed: %u\n", GetLastError());
        return -1;
    }
    return 0;
}

暫無
暫無

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

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