0

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.

Rupesh Yadav
  • 12,096
  • 4
  • 53
  • 70
john-charles
  • 1,417
  • 4
  • 17
  • 30

1 Answers1

1

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
);
Roger Lipscombe
  • 89,048
  • 55
  • 235
  • 380
  • Ok, I tried that, and I now get `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` – john-charles Jun 16 '12 at 14:47
  • Oh, I ok, I looked fast and didn't realize you had changed PHKEY to HKEY. That worked, now I get access denied, but I'll try and execute it with elevated rights. Which should work. – john-charles Jun 16 '12 at 15:00