如何在 C++ 中将 32 位 D-Word 写入 Windows 注册表
How to write a 32 Bit D-Word to Windows registry in C++
我正在尝试使用 C++ Win32API 应用程序禁用 Windows Defender。
为此,我需要在注册表中写入一个 D 字 (DisableAntiSpyware = 1)。
我总是在安装新的 Windows 后手动执行此操作。
所以这是我的代码,但它不起作用。
也许有人可以告诉我为什么或有什么问题。谢谢!
好的,我已经稍微更改了代码,但仍然无法正常工作...
case 1:
//::MessageBeep(MB_ICONERROR);
::MessageBox(hWnd, L"Button was Pressed",L"Button was clicked?",MB_OK);
LONG
SetRegValue
(
const wchar_t* path
, const wchar_t *name
, const BYTE *value
);
{
LONG status;
HKEY hKey;
DWORD value = 0x00000001;
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"\SOFTWARE\Policies\Microsoft\Windows Defender", 0, KEY_ALL_ACCESS, &hKey);
if ((status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx(hKey, L"test", 0, REG_DWORD, (const BYTE*)&value,sizeof(value));
RegCloseKey(hKey);
}
return status;
::MessageBeep(MB_ICONERROR);
}
}
}
break;
您无法写入 HKEY_LOCAL_MACHINE
下的任何键,除非该程序是 运行 提升权限,即管理员模式。调用 RegOpenKeyEx
或 RegSetValueEx
将失败。
打开注册表项时,您应该只请求您实际需要的权限。因此,请将 KEY_ALL_ACCESS
替换为 KEY_SET_VALUE
,因为您所做的只是写入一个值。但即便如此,您可能仍然需要 运行 您的应用程序具有更高的权限才能写入 HKEY_LOCAL_MAHCINE
,除非您事先授予您的用户帐户对 Windows Defender 密钥的写入权限。
此外,如果您的代码在 64 位系统上编译为 32 位和 运行s,并且需要写入 64 位注册表,那么您必须包含 KEY_WOW64_64KEY
标志,否则您可能受制于 Registry Reflection/Registry Redirection.
尝试更像这样的东西:
case 1:
{
::MessageBox(hWnd, L"Button was Pressed", L"Button was clicked?", MB_OK);
DWORD value = 1;
DWORD flags = KEY_SET_VALUE;
#if !defined(_WIN64)
BOOL bIsWow64Process = FALSE;
if (IsWow64Process(GetCurrentProcess(), &bIsWow64Process) && bIsWow64Process)
flags |= KEY_WOW64_64KEY;
#endif
HKEY hKey;
LONG status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"\SOFTWARE\Policies\Microsoft\Windows Defender", 0, flags, &hKey);
if ((status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx(hKey, L"DisableAntiSpyware", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
RegCloseKey(hKey);
}
::MessageBeep(MB_ICONERROR);
}
break;
我正在尝试使用 C++ Win32API 应用程序禁用 Windows Defender。 为此,我需要在注册表中写入一个 D 字 (DisableAntiSpyware = 1)。 我总是在安装新的 Windows 后手动执行此操作。 所以这是我的代码,但它不起作用。 也许有人可以告诉我为什么或有什么问题。谢谢!
好的,我已经稍微更改了代码,但仍然无法正常工作...
case 1:
//::MessageBeep(MB_ICONERROR);
::MessageBox(hWnd, L"Button was Pressed",L"Button was clicked?",MB_OK);
LONG
SetRegValue
(
const wchar_t* path
, const wchar_t *name
, const BYTE *value
);
{
LONG status;
HKEY hKey;
DWORD value = 0x00000001;
status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"\SOFTWARE\Policies\Microsoft\Windows Defender", 0, KEY_ALL_ACCESS, &hKey);
if ((status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx(hKey, L"test", 0, REG_DWORD, (const BYTE*)&value,sizeof(value));
RegCloseKey(hKey);
}
return status;
::MessageBeep(MB_ICONERROR);
}
}
}
break;
您无法写入 HKEY_LOCAL_MACHINE
下的任何键,除非该程序是 运行 提升权限,即管理员模式。调用 RegOpenKeyEx
或 RegSetValueEx
将失败。
打开注册表项时,您应该只请求您实际需要的权限。因此,请将 KEY_ALL_ACCESS
替换为 KEY_SET_VALUE
,因为您所做的只是写入一个值。但即便如此,您可能仍然需要 运行 您的应用程序具有更高的权限才能写入 HKEY_LOCAL_MAHCINE
,除非您事先授予您的用户帐户对 Windows Defender 密钥的写入权限。
此外,如果您的代码在 64 位系统上编译为 32 位和 运行s,并且需要写入 64 位注册表,那么您必须包含 KEY_WOW64_64KEY
标志,否则您可能受制于 Registry Reflection/Registry Redirection.
尝试更像这样的东西:
case 1:
{
::MessageBox(hWnd, L"Button was Pressed", L"Button was clicked?", MB_OK);
DWORD value = 1;
DWORD flags = KEY_SET_VALUE;
#if !defined(_WIN64)
BOOL bIsWow64Process = FALSE;
if (IsWow64Process(GetCurrentProcess(), &bIsWow64Process) && bIsWow64Process)
flags |= KEY_WOW64_64KEY;
#endif
HKEY hKey;
LONG status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"\SOFTWARE\Policies\Microsoft\Windows Defender", 0, flags, &hKey);
if ((status == ERROR_SUCCESS) && (hKey != NULL))
{
status = RegSetValueEx(hKey, L"DisableAntiSpyware", 0, REG_DWORD, (const BYTE*)&value, sizeof(value));
RegCloseKey(hKey);
}
::MessageBeep(MB_ICONERROR);
}
break;