创建进程作为用户 - 错误 1305
CreateProcessAsUser - Error 1305
我有服务,我需要使用此服务的当前用户权限运行 gui 应用程序。这是我的代码,它总是返回带有 1305 CreateProcessAsUser 功能的 GetLastError。我如何修复它或可能是我的代码不正确,您可以建议我一些有用的东西。感谢。
void ConnectionManager::LaunchDialer ()
{
HANDLE currentToken;
HANDLE primaryToken;
int dwSessionId = 0;
PHANDLE hUserToken = 0;
PHANDLE hTokenDup = 0;
PWTS_SESSION_INFO pSessionInfo = 0;
DWORD dwCount = 0;
// Get the list of all terminal sessions
WTSEnumerateSessions (WTS_CURRENT_SERVER_HANDLE, 0, 1,
&pSessionInfo, &dwCount);
int dataSize = sizeof (WTS_SESSION_INFO);
// look over obtained list in search of the active session
for (DWORD i = 0; i < dwCount; ++i)
{
WTS_SESSION_INFO si = pSessionInfo [i];
if (WTSActive == si.State)
{
// If the current session is active – store its ID
dwSessionId = si.SessionId;
break;
}
}
WTSFreeMemory (pSessionInfo);
// Get token of the logged in user by the active session ID
BOOL bRet = WTSQueryUserToken (dwSessionId, ¤tToken);
if (!bRet)
{
ModemDetectorService::instance ()->logMessage (QString ("WTSQueryUserToken: %1")
.arg (GetLastError ()));
return;
}
bRet = DuplicateTokenEx (currentToken,
TOKEN_ASSIGN_PRIMARY | TOKEN_ALL_ACCESS,
0,
SecurityImpersonation,
TokenPrimary,
&primaryToken);
if (!bRet)
{
ModemDetectorService::instance ()->logMessage (QString ("DuplicateTokenEx: %1")
.arg (GetLastError ()));
return;
}
if (!primaryToken)
{
ModemDetectorService::instance ()->logMessage ("Invalid user token");
return;
}
STARTUPINFO StartupInfo;
PROCESS_INFORMATION processInfo;
ZeroMemory(&StartupInfo, sizeof(STARTUPINFO));
StartupInfo.cb= sizeof(STARTUPINFO);
StartupInfo.lpDesktop = TEXT("winsta0\default");
SECURITY_ATTRIBUTES Security1;
SECURITY_ATTRIBUTES Security2;
QSettings settings ("HKEY_LOCAL_MACHINE\Software\Olive\OliveDialer",
QSettings::NativeFormat);
const QString path = QDir::toNativeSeparators (settings.value ("InstallationDirectory").toString ());
QByteArray command = (""" + path + "\" +
ApplicationInfo::Olive::ShortApplicationName + ".exe" + """).toUtf8 ();
void* lpEnvironment = NULL;
// Get all necessary environment variables of logged in user
// to pass them to the process
BOOL resultEnv = CreateEnvironmentBlock (&lpEnvironment,
primaryToken,
FALSE);
if (!resultEnv)
{
long nError = GetLastError ();
ModemDetectorService::instance ()->logMessage (QString ("CreateEnvironmentBlock failed with: %1")
.arg (nError));
}
// Start the process on behalf of the current user
BOOL result = CreateProcessAsUser (primaryToken, 0,
(LPSTR)(command.data ()),
&Security1,
&Security2,
FALSE,
NORMAL_PRIORITY_CLASS | CREATE_UNICODE_ENVIRONMENT,
lpEnvironment,
NULL,
&StartupInfo,
&processInfo);
if (!result)
{
DWORD errorCode = GetLastError ();
ModemDetectorService::instance ()->logMessage (QString ("Application start failed: %1 %2")
.arg (errorCode)
.arg (command.data ()));
}
else
ModemDetectorService::instance ()->logMessage ("Application started successfully");
DestroyEnvironmentBlock (lpEnvironment);
CloseHandle (primaryToken);
}
错误
1305 ERROR_UNKNOWN_REVISION
("修订级别未知"),通常是指安全对象。 实际上,您正在传递两个从未初始化过的SECURITY_ATTRIBUTES
结构(Security1
和Security2
)。
您需要传递NULL
而不是&Security1
和&Security2
,或者正确初始化结构。
尝试将 lpCurrentDirectory 设置为类似 C:\Windows
的内容或使用 LoadUserProfile 加载用户配置文件
相关文章:
- 警告处理为错误这里有什么问题
- "error: no matching function for call to"构造函数错误
- boost::进程间消息队列引发错误
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- QT在错误的班级中寻找空位
- vector.resize()中的分配错误
- 代码在main()中运行,但在函数中出现错误
- 释放错误后堆使用
- (C++)分析树以计算返回错误值的简单算术表达式
- Project Euler问题4的错误解决方案
- 我的字符计数代码计算错误.为什么
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- CMake项目Boost库错误:Boost/config/compiler/gcc.hpp:165:10:致命错误:cs
- 在某些循环内使用vector.push_back时出现分段错误
- MSVC多行宏编译器错误
- 静态数据成员的问题-修复链接错误会导致编译器错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 创建进程作为用户 - 错误 1305