【问题标题】:RegLoadAppKey parameter is incorrectRegLoadAppKey 参数不正确
【发布时间】:2012-06-19 07:53:29
【问题描述】:

我正在尝试打开 Windows 默认用户的注册表配置单元。我收到错误“参数无效”。我的代码如下:

PHKEY loadDefaultHiveAppKey(){

    PHKEY temporaryHKEY = 0;

    wchar_t * errorText = 0;
    //wchar_t * defaultProfileHiveFile = getDefaultUserProfileHive();
    /* For debugging purpouses use a hardcoded path */
    wchar_t * defaultProfileHiveFile = L"C:\\Users\\Default\\NTUSER.dat";

    long returnCode = 0;

    returnCode = RegLoadAppKey(
        defaultProfileHiveFile,
        temporaryHKEY,
        KEY_ALL_ACCESS,
        REG_PROCESS_APPKEY,
        0
    );

    //free(defaultProfileHiveFile);

    if(returnCode != ERROR_SUCCESS){



        // http://stackoverflow.com/questions/455434/how-should-i-use-formatmessage-properly-in-c
        FormatMessage(
           FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS,  
           NULL,
           returnCode,
           MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
           (LPTSTR)&errorText,
           0, 
           NULL
        );

        printf("Failed to open registry hive!\n");

        if(errorText != 0){
                    /* This prints "The Parameter is Incorrect" */
            printf("%ls\n", errorText);

            LocalFree(errorText);
            errorText = NULL;

        } else {

            printf("Unknown reason!\n");

        }



        return 0;

    }

    return temporaryHKEY;

}

我的 main 基本上只是对前面方法的调用。这是RegLoadAppKey的msdn文章。

【问题讨论】:

  • wchar_t * defaultProfileHiveFile如何指向一个非宽字符串(没有L前缀)?

标签: c winapi registry


【解决方案1】:

您的phkResult 错误。如果您将其读取为指向 HKEY 的指针,则会更清楚。你需要的是这样的:

HKEY temporaryHKEY;

returnCode = RegLoadAppKey(
    defaultProfileHiveFile,
    &temporaryHKEY,
    KEY_ALL_ACCESS,
    REG_PROCESS_APPKEY,
    0
);

【讨论】:

  • 好的,我试过了,现在我得到cpp(106): error C2664: 'RegLoadAppKeyW' : cannot convert parameter 2 from 'PHKEY *' to 'PHKEY' 1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
  • 哦,好吧,我看的很快,没有意识到您已将 PHKEY 更改为 HKEY。那行得通,现在我被拒绝访问,但我会尝试以提升的权限执行它。哪个应该有效。
猜你喜欢
  • 2011-06-23
  • 2019-05-23
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-25
  • 2020-03-13
相关资源
最近更新 更多