[Solved] Any example to use RegLoadKey()


Thanks a lot for your time. Going to share the code that I used; It may help someone else:

#include <windows.h>
#include <stdio.h>


BOOL SetPrivilege(

    HANDLE hToken,              // access token handle
    LPCWSTR nameOfPrivilege,   // name of privilege to enable/disable
    BOOL bEnablePrivilege     // to enable or disable privilege
)
{
    TOKEN_PRIVILEGES tp;
    LUID luid;

    if (!LookupPrivilegeValue(
        NULL,               // lookup privilege on local system
        nameOfPrivilege,   // privilege to lookup 
        &luid))           // receives LUID of privilege
    {
        printf("LookupPrivilegeValue error: %u\n", GetLastError());
        return FALSE;
    }

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    if (bEnablePrivilege)
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else
        tp.Privileges[0].Attributes = 0;

    // Enable the privilege or disable all privileges.

    if (!AdjustTokenPrivileges(
        hToken,
        FALSE,
        &tp,
        sizeof(TOKEN_PRIVILEGES),
        (PTOKEN_PRIVILEGES)NULL,
        (PDWORD)NULL))
    {
        printf("AdjustTokenPrivileges error: %u\n", GetLastError());
        return FALSE;
    }

    if (GetLastError() == ERROR_NOT_ALL_ASSIGNED)

    {
        printf("The token does not have the specified privilege. \n");
        return FALSE;
    }

    return TRUE;
}


int wmain(int argc, WCHAR *argv[])
{
    HANDLE proccessHandle = GetCurrentProcess();     // get the handle to the current proccess
    DWORD typeOfAccess = TOKEN_ADJUST_PRIVILEGES;   //  requiered to enable or disable the privilege
    HANDLE tokenHandle;                             //  handle to the opened access token

    HKEY hKey = HKEY_LOCAL_MACHINE;
    LPCWSTR subKeyName = L"Debu";
    LPCWSTR pHive = L"C:\\Users\\Default\\NTUSER.DAT";

    if (OpenProcessToken(proccessHandle, typeOfAccess, &tokenHandle))
    {
        // Enabling RESTORE and BACKUP privileges
        SetPrivilege(tokenHandle, SE_RESTORE_NAME, TRUE);
        SetPrivilege(tokenHandle, SE_BACKUP_NAME, TRUE);

    }
    else
    {
        wprintf(L"Error getting the access token.\n");
    }

    // Loading the HIVE into HKLM\Debu subkey

    LONG loadKey = RegLoadKeyW(hKey, subKeyName, pHive);

    if (loadKey != ERROR_SUCCESS)
    {
        wprintf(L"Error loading the key. Code: %li\n", loadKey);
    }
    else
    {
        wprintf(L"Hive file has been loaded.\n");

    }



    return 0;
}

1

solved Any example to use RegLoadKey()