john-charles
john-charles

Reputation: 1469

RegLoadAppKey parameter is incorrect

I am attempting to open the the registry hive of the windows default user. I get the error "The Parameter is invalid." My code is as follows:

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;

}

My main is basically just a call to the preceding method. Here is the msdn article for RegLoadAppKey.

Upvotes: 0

Views: 905

Answers (1)

Roger Lipscombe
Roger Lipscombe

Reputation: 91855

Your phkResult is wrong. It's clearer if you read it as pointer-to-HKEY. What you need is this:

HKEY temporaryHKEY;

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

Upvotes: 1

Related Questions