【问题标题】:My winreg function is not being recognized我的 winreg 功能未被识别
【发布时间】:2021-05-02 19:54:41
【问题描述】:

错误:

error: no matching function for call to 'RegCreateKeyExW'

我包括什么:

#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <string>
#include <winreg.h>

我的代码:

        HKEY hKey;
        LONG result = 0;
        char *path = "SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001";

        if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, path, 0, NULL,
                REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL) == ERROR_SUCCESS) {
            printf("2. success \n");
        } else {
            printf("fail\n");
        }

我已经尝试了一切,但这个错误不会消失,如果有人可以帮助我,我会很感激!

【问题讨论】:

    标签: c++ registry regedit


    【解决方案1】:

    您正在调用RegCreateKeyEx()TCHAR 版本。从错误消息中可以清楚地看出RegCreateKeyEx() 正在解析为Unicode 版本RegCreateKeyExW()(因为UNICODE 在您的构建配置中定义)。该版本将宽 wchar_t 字符串作为输入,但您传递的是窄 char 字符串。

    您可以:

    1. 使用TCHAR 字符串来匹配您的代码正在调用的TCHAR 函数:
    const TCHAR* path = TEXT("SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001");
    
    1. 使用 Unicode 字符串,以匹配运行时实际调用的 Unicode 函数:
    const wchar_t *path = L"SYSTEM\\CurrentControlSet\\Control\\IDConfigDB\\Hardware Profiles\\0001";
    
    1. 使用函数的 ANSI 版本 RegCreateKeyExA() 来匹配您的原始字符串:
    if (RegCreateKeyExA(...) == ERROR_SUCCESS) {
    

    【讨论】:

    • 嘿!谢谢,这解决了错误,但是现在找不到通过该路径的密钥,您知道可能出了什么问题吗?
    • @El_Sapo_Pepe 您是否使用 RegEdit 验证该密钥确实存在?您是在为 32 位还是 64 位编译您的应用程序,32bit or 64bit area of the Registry 中是否存在密钥?
    • 我需要在管理员模式下运行程序才能执行此操作吗?
    • @El_Sapo_Pepe 可能,因为 HKLM 的大部分内容通常仅限于非管理员的只读权限,并且您可以打开密钥进行写作。 RegCreateKeyEx() 如果无法打开/创建密钥,即由于权限不足,则返回error code,但您没有在printf() 中打印出该错误代码,例如:LONG lRet = RegCreateKeyEx(...); if (lRet == ERROR_SUCCESS) { ... } else { printf("fail, error %d\n", lRet); }
    • 这个最好用powershell脚本
    猜你喜欢
    • 1970-01-01
    • 2013-08-04
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-28
    • 2023-03-23
    相关资源
    最近更新 更多