尝试更改 ActivePowerScheme:RegOpenKeyEx 失败,错误 0

发布于 2024-12-07 16:06:42 字数 1112 浏览 0 评论 0 原文

我需要通过在注册表项 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes 中更改 ActivePowerScheme 来设置它。 所以我尝试使用 winapi 函数 RegOpenKeyEx 和 RegSetValueEx 来执行此操作,

wchar_t *PowerScheme=TEXT("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
HKEY hRootKey = HKEY_LOCAL_MACHINE;
PWCHAR sKey = TEXT("SYSTEM\\CurrentControlSet\\Control\\Power\\User\\PowerSchemes");
PWCHAR sActivePowerS = TEXT("ActivePowerScheme"); 
HKEY hKeyResult = NULL; 
//open  
if (RegOpenKeyEx(hRootKey,sKey,0,KEY_ALL_ACCESS,&hKeyResult)!=ERROR_SUCCESS) {
      //it is always failing with error 0 !
      DWORD dw = GetLastError();  
}

但是 RegOpenKeyEx() 总是失败并显示错误 0,这意味着“操作成功完成”。 RegSetValueEx() 返回相同的值。

if(RegSetValueEx(hKeyResult,sActivePowerS,0,REG_SZ,
         (BYTE *)PowerScheme,wcslen(PowerScheme))!=ERROR_SUCCESS) {
                //it is always failing with error 0
                DWORD dw = GetLastError();  
            }

当然,当前的电源方案不会改变价值。但根据msdn: “如果函数成功,则返回值为 ERROR_SUCCESS。 如果函数失败,返回值是一个非零错误代码”。

我将不胜感激您的回答。PS

它在Windows 7中编译并以管理员权限执行

I need to set ActivePowerScheme by changing it in registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Power\User\PowerSchemes.
So I try to do it with winapi functions RegOpenKeyEx and RegSetValueEx

wchar_t *PowerScheme=TEXT("8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c");
HKEY hRootKey = HKEY_LOCAL_MACHINE;
PWCHAR sKey = TEXT("SYSTEM\\CurrentControlSet\\Control\\Power\\User\\PowerSchemes");
PWCHAR sActivePowerS = TEXT("ActivePowerScheme"); 
HKEY hKeyResult = NULL; 
//open  
if (RegOpenKeyEx(hRootKey,sKey,0,KEY_ALL_ACCESS,&hKeyResult)!=ERROR_SUCCESS) {
      //it is always failing with error 0 !
      DWORD dw = GetLastError();  
}

But RegOpenKeyEx() is always failing with error 0, that means "Operation completed successfully". And RegSetValueEx() returns same value.

if(RegSetValueEx(hKeyResult,sActivePowerS,0,REG_SZ,
         (BYTE *)PowerScheme,wcslen(PowerScheme))!=ERROR_SUCCESS) {
                //it is always failing with error 0
                DWORD dw = GetLastError();  
            }

And of course current power scheme doesn't change value. But according to msdn:
"If the function succeeds, the return value is ERROR_SUCCESS.
If the function fails, the return value is a nonzero error code".

I will be grateful to any your answers.

P.S. it compiled in Windows 7 and executed with rights of admin

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

李不 2024-12-14 16:06:42

你的处理方式是错误的。您很少需要自己更改注册表中的内容。

阅读电源方案管理 在 MSDN 站点上了解正确的操作方法。

You are going about this the wrong way. You RARELY need to change stuff in the registry yourself.

Read Power Scheme Management on the MSDN site for the proper way of doing it.

梦过后 2024-12-14 16:06:42

正如文档所述RegOpenKeyEx不会更新GetLastError,返回值是错误代码本身。您介意检查一下吗?

我敢打赌您在这里遇到了 ERROR_ACCESS_DENIED 错误。

UPD:虽然这可能回答了您的问题,但您应该考虑使用 RedX 建议的 API 来更新电源管理设置。该注册表项的权限设置方式(出于某种原因!)甚至管理员也只有读取权限,而没有写入权限。

As documentation states, RegOpenKeyEx does not update GetLastError, and return value is the error code itself. Would you mind checking it?

I'd bet you have ERROR_ACCESS_DENIED error here.

UPD: While this perhaps answers your question, you should consider using API suggested by RedX in order to update power management settings. Permissions on this registry key are set (for a reason!) in a way that even Administrators have only read permissions, and not write.

春花秋月 2024-12-14 16:06:42

在评论中,您声明 RegOpenKeyEx 返回 ERROR_ACCESS_DENIED。这是因为您请求对某个密钥进行写访问,但由于 UAC,您没有足够的权限。您将需要运行提升的进程才能写入此密钥。

正如其他人正确指出的那样,您不应该调用 GetLastError 因为 RegOpenKeyEx 不会设置最后一个错误值,而是直接返回错误代码。更重要的是,您应该使用电源管理 API,而不是破解注册表。

即使您切换到电源管理 API,您仍然需要管理员权限。您可以通过在 requestedExecutionLevel 设置为 requireAdministrator 来安排此操作rel="nofollow">应用程序清单

在 Visual Studio 中,您可以在链接器 | 下的项目配置中进行此更改。清单文件 | UAC 执行级别。

In the comments you state that RegOpenKeyEx returns ERROR_ACCESS_DENIED. This is because you request write access to a key to which you do not have sufficient rights because of UAC. You will need to run your process elevated to write to this key.

As others have correctly pointed out, you should not call GetLastError since RegOpenKeyEx does not set the last error value and instead returns the error code directly. More importantly you should be using the power management API rather than hacking the registry.

Even when you switch to the power management API you will still require administrator rights. You can arrange this by setting requestedExecutionLevel to requireAdministrator in your application manifest.

In Visual Studio you can make this change in the project configuration under Linker | Manifest File | UAC Execution Level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文