即使使用系统服务中的RegOpenCurrentUser,也无法获取用户级注册表值
Cannot get user level registry value even using RegOpenCurrentUser from system service
我写了一个系统服务,我想在其中获取HKEY_CURRENT_user下活动用户的一个注册表值。我写的代码如下。然而,它似乎只能获得系统级注册表值,而不能获得活动用户的注册表值。请参阅下面的代码。问题出在哪里?什么东西不见了?
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
}
您应该在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);
相关文章:
- 如何获取用户输入的算术表达式,如 5-8+7*4-8+9 或 1+5?
- C++如何使用表达式获取用户输入?
- 有没有办法获取用户操作系统的ANSI转义码?
- 当PSO细粒度策略对使用AdsGetObject MSDN API的Windows操作系统生效时,如何获取用户密码到期日
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 使用 Firebase 从C++获取用户的电话号码
- 从 R 控制台获取用户输入:Rcpp 和 std::cin
- 如何使用字符串获取用户控制台输入
- 循环以使用 cstring 数组 c++ 获取用户输入
- 尝试获取用户输入和cout消息期望,当我输入一个有效名称时,它将所有这些命名
- 获取用户输入以形成链表
- 尝试编写进程内存并获取用户定义的文字运算符未找到
- 如何使用 C++17 std::文件系统获取用户的主目录?
- C |获取用户输入而无需给出值
- 如何在消息文件中获取用户定义的字段
- 从Windows上的系统工作的服务中获取用户文档文件夹.C++
- 如何阻止 cin.getline() 导致控制台在找不到分隔符时重复获取用户输入?
- C++:获取用户通过cin输入的数据类型
- 如何在C 中使用我的服务来获取用户的SID和用户名
- 结构数组:无需致电并获取用户输入的匹配函数