使用RegLoadKey()的任何示例

时间:2016-10-28 17:39:09

标签: c winapi

我收到了这段代码:

int wmain(int argc, WCHAR *argv[])
{
    HKEY hKey = HKEY_LOCAL_MACHINE;
    LPCWSTR subKey = L"Example";
    LPCWSTR pFile = L"C:\\Users\\Default\\NTUSER.DAT";

    LONG loadKey = RegLoadKey(hKey, subKey, pFile);

    if (loadKey != ERROR_SUCCESS) {
        wprintf(L"Code: %li\n", loadKey);
    } else {
        wprintf(L"Mounted!\n");
    }

    return 0;
}

我知道我需要为我的调用流程启用SE_RESTORE_NAMESE_BACKUP_NAME,并且有example on MSDN,但无法理解。不知道在哪里放置我需要的权限。

有人能告诉我一个如何合并所有这些并让RegLoadKey()函数有效的例子吗?

MSDN文档没有解释很多变量,例如hToken等。这就是我需要帮助的原因。

1 个答案:

答案 0 :(得分:3)

非常感谢你的时间。要分享我用过的代码;它可能会帮助别人:

#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;
}