c++ - CreateProcessAsUser - 错误 1305

标签 c++ windows winapi createprocessasuser

我有服务,我需要使用此服务的当前用户权限运行 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, &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);
}

最佳答案

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

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

关于c++ - CreateProcessAsUser - 错误 1305,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20984356/

相关文章:

windows - 在 Windows 上从 Eclipse 复制粘贴到 Gmail 时保留文本颜色?

windows - 是否有用于 vista 的 API 来检测桌面是否全屏运行?

.net - .NET 的 SuppressKeypress 如何/为何工作?

c++ - 如何通过ifstream打开文件时共享文件删除权限

c++ - 我正在尝试使用队列实现 bfs。有人可以帮我找到错误吗?

c++ - 一副纸牌 : How do I make it so I randomly draw a card only once?

c++ - 在 C++ Builder 类中定义默认函数参数和参数太少错误

c++ - 从 Rcpp 调用 R 函数的替代方法

node.js - 运行 grunt 时出现错误 : spawn ./node_modules/.bin/grunt ENOENT

javascript - Components.interfaces.nsIProcess chop 包含 "&"的 URL 参数