You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

为何RegOpenKeyExA打开含空格的注册表键路径失败且返回错误码2?

Answers to Your RegOpenKeyExA Questions

1. Does RegOpenKeyExA Fail with Spaces in Registry Paths?

Nope, that's a common misconception. RegOpenKeyExA handles spaces in registry key names perfectly fine. Spaces are valid characters in registry keys, and as long as your path string is properly escaped (which your code does with double backslashes), the function won't choke on them. The error you're seeing isn't related to the space in "Windows Defender" at all.

2. Why Am I Getting Error Code 2 ("Path Not Found") for That Specific Key?

Error code 2 (ERROR_FILE_NOT_FOUND) translates directly to: the registry key you're trying to open doesn't exist on your system. Here's the breakdown:

  • The SOFTWARE\Policies\Microsoft\Windows Defender key isn't present by default. It only gets created if you've configured group policy settings for Windows Defender, or if you manually added it yourself.
  • Admin rights don't magically make non-existent keys appear. They let you modify existing keys or create new ones, but you can't open a key that isn't there.

Quick Notes on Your Test Code

Looking at your snippet:

int res; HKEY hKey; 
res = RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\Policies\\Microsoft\\Windows Defender", 0, KEY_QUERY_VALUE|KEY_WRITE|KEY_READ|KEY_SET_VALUE, &hKey); 
std::cout << " Error code ["+res+"]"<<std::endl;
  • The line "+res+" will cause undefined behavior because you're trying to concatenate an integer (res) with strings. Replace that with std::to_string(res) to properly convert the error code to a string.
  • If you're targeting machine-wide policies instead of user-specific ones, you might want to use HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER — but even then, the key still won't exist unless policies are set up.

How to Fix the Error

If you need that key to exist, you have two straightforward options:

  • Manually create it: Open Registry Editor (regedit.exe), navigate to HKEY_CURRENT_USER\SOFTWARE\Policies\Microsoft, right-click > New > Key, and name it "Windows Defender".
  • Create it programmatically: Swap RegOpenKeyExA with RegCreateKeyExA, which will create the key if it doesn't exist (and open it if it does). Here's a quick example:
LONG res; HKEY hKey;
res = RegCreateKeyExA(
    HKEY_CURRENT_USER,
    "SOFTWARE\\Policies\\Microsoft\\Windows Defender",
    0,
    NULL,
    REG_OPTION_NON_VOLATILE,
    KEY_QUERY_VALUE | KEY_WRITE | KEY_READ | KEY_SET_VALUE,
    NULL,
    &hKey,
    NULL
);

if (res == ERROR_SUCCESS || res == ERROR_ALREADY_EXISTS) {
    // Key is open and ready for use
    RegCloseKey(hKey);
} else {
    std::cout << " Error code [" << std::to_string(res) << "]" << std::endl;
}

That should resolve your error code 2 issue. And for what it's worth, you don't even need admin rights to create keys under HKEY_CURRENT_USER — those are user-specific. Admin rights are only required for system-wide keys under HKEY_LOCAL_MACHINE.

内容的提问来源于stack exchange,提问作者loaded_dypper

火山引擎 最新活动