首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CreateProcessAsUser -错误1305

CreateProcessAsUser -错误1305
EN

Stack Overflow用户
提问于 2014-01-07 23:47:48
回答 2查看 1.7K关注 0票数 0

我有服务,我需要运行gui应用程序与当前用户从这个服务的好处。这是我的代码,它总是用1305 GetLastError函数返回CreateProcessAsUser。我如何修复它或者可能是我的代码是不对的,您可以给我一些有用的建议。谢谢。

代码语言:javascript
复制
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, &currentToken);
    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);
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-01-08 00:41:50

错误1305是ERROR_UNKNOWN_REVISION (“修订级别未知”),它通常指的是安全对象。实际上,您正在传递两个从未初始化过的SECURITY_ATTRIBUTES结构(Security1Security2)。

您需要传递NULL而不是&Security1&Security2,或者正确地初始化结构。

票数 2
EN

Stack Overflow用户

发布于 2014-01-08 00:08:08

尝试将lpCurrentDirectory设置为C:\Windows之类的东西

或者用LoadUserProfile加载用户配置文件

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20984356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档