简体   繁体   English

即使从系统服务使用RegOpenCurrentUser也无法获取用户级别的注册表值

[英]Cannot get user level registry value even using RegOpenCurrentUser from system service

I wrote a system service, in which I want to get the active user's one registry value under HKEY_CURRENT_USER. 我编写了一个系统服务,其中我想在HKEY_CURRENT_USER下获取活动用户的一个注册表值。 I wrote the code as below. 我写的代码如下。 However, it seems it can only get the system level registry value, cannot get the active user's registry value. 但是,似乎只能获取系统级别的注册表值,而不能获取活动用户的注册表值。 See the code below. 请参见下面的代码。 Where is the problem? 问题出在哪儿? Something missing? 缺少什么?

void GetUserRegistryFromSystemService()
{
#ifdef Q_OS_WIN

    DWORD sessionId = WTSGetActiveConsoleSessionId();
    qInfo() << "Session ID = " << sessionId;

    wchar_t * ppUserName[100];
    DWORD sizeOfUserName;
    WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE, sessionId, WTSUserName, ppUserName, &sizeOfUserName);
    qInfo() << "Windows User Name = " << QString::fromWCharArray(*ppUserName);

    std::wstring strValueOfBinDir = L"Unknown Value";
    LONG regOpenResult = ERROR_SUCCESS;

    HANDLE hUserToken = NULL;
    HANDLE hFakeToken = NULL;

    if (WTSQueryUserToken(sessionId, &hUserToken))
    {
         if (DuplicateTokenEx(hUserToken, TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS, 0, SecurityImpersonation, TokenPrimary, &hFakeToken) == TRUE)
         {
            qInfo() << "Before ImpersonateLoggedOnUser()......";
            if (ImpersonateLoggedOnUser(hFakeToken))
            {
                HKEY hKey;

                regOpenResult = RegOpenCurrentUser(KEY_READ, &hKey);
                if (regOpenResult != ERROR_SUCCESS)
                {
                    qCritical() << "Failed to call RegOpenCurrentUser(), Error is " << regOpenResult;
                }

                // Fails to get this hive, will get the default value "Unkown"
                RegOpenKeyEx(HKEY_CURRENT_USER,
                             TEXT("Software\\Baidu\\BaiduYunGuanjia"),
                             0,
                             KEY_READ,
                             &hKey);
                GetStringRegKey(hKey, TEXT("installDir"), strValueOfBinDir, TEXT("Unknown"));

                // It can get the following hive successfully
                // RegOpenKeyEx(HKEY_LOCAL_MACHINE,
                //              TEXT("Software\\GitForWindows"),
                //              0,
                //              KEY_READ,
                //              &hKey);
                // GetStringRegKey(hKey, TEXT("InstallPath"), strValueOfBinDir, TEXT("Unknown"));

                RevertToSelf();
            }
            else
            {
                qCritical() << "Failed to ImpersonateLoggedOnUser...";
            }
            CloseHandle(hFakeToken);
        }
        else
        {
            qCritical() << "Failed to call DuplicateTokenEx...";
        }
        CloseHandle(hUserToken);
    }
    else
    {
        qCritical() << "Failed to get the user token of session " << sessionId;
    }

    qInfo() << "The value of Registry is " << QString::fromWCharArray( strValueOfBinDir.c_str() );

#endif
}

You should use HKEY handle received from RegOpenCurrentUser in the RegOpenKeyEx instead of HKEY_CURRENT_USER : 您应该在RegOpenKeyEx使用从RegOpenCurrentUser收到的HKEY句柄,而不是HKEY_CURRENT_USER

regOpenResult = RegOpenCurrentUser(KEY_READ, &hKey);
if (regOpenResult != ERROR_SUCCESS)
{
    qCritical() << "Failed to call RegOpenCurrentUser(), Error is " << regOpenResult;
}
HKEY hSubKey; 
// Fails to get this hive, will get the default value "Unkown"
RegOpenKeyEx(hKey, TEXT("Software\\Baidu\\BaiduYunGuanjia"), 0, KEY_READ, &hSubKey);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM