如何使用 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
字符串并将其设置为 "ntsd -c q"
chrome.exe
子项,如下图所示。
这是我的代码,它为 Default
字符串添加 "ntsd -c q"
而不是创建新的 Debugger
字符串。我无法从 Microsoft 的文档中清楚地了解如何使用 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[=13=]";
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;
}
我想使用 C++ 代码将字符串名称及其值添加到 Windows 注册表,以停止 Firefox 以外的浏览器停止 运行。我计划为所有浏览器循环编辑 Windows 注册表,但现在我只针对 Chrome 实施它。
我下面的当前代码是向 Default
字符串添加一个值,但它不起作用,但我想创建一个 Debugger
字符串并将其设置为 "ntsd -c q"
chrome.exe
子项,如下图所示。
这是我的代码,它为 Default
字符串添加 "ntsd -c q"
而不是创建新的 Debugger
字符串。我无法从 Microsoft 的文档中清楚地了解如何使用 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[=13=]";
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;
}