gpt4 book ai didi

vb.net - 使用 Windows 服务启动提升的进程

转载 作者:可可西里 更新时间:2023-11-01 11:16:45 25 4
gpt4 key购买 nike

我正在尝试从 Windows 服务运行软件更新。我的服务在 LocalSystem 下运行。

我认为在 CreateProcessAsUser 行之前一切正常,该行因 API 错误 3 (ERR_FILE_NOT_FOUND) 而失败。

我不确定如何继续调试我的代码。我用 ProcMon 看它是否真的找不到路径,但它确实找到了,所以我想我可能错过了其他东西。

有人看到可能的错误吗?

我正在使用 WTSEnumerateSessions 获取事件 session 。出于某种原因,它的 MachineName 成员是空的,但是 SessionID 不是 0,所以我想这还是可以的。

Public Function StartAppInSessionAsAdmin(ByVal uSessionID As String, ByVal uWinstationNameStrPtr As Long, ByVal uAppName As String) As Integer

'get SessionID token
Dim hToken&
Dim bRet As Boolean
bRet = WTSQueryUserToken(uSessionID, hToken)
WriteLog "wtsqueryusertoken: " & bRet & ", htoken: " & hToken

'we need to get the TokenLinked Token
Dim TLT As TOKEN_LINKED_TOKEN
Dim TLTSize&
TLTSize = Len(TLT.LinkedToken)

Dim hLinkedToken&
Dim iRetSize&
bRet = GetTokenInformation(hToken, TOKEN_INFORMATION_CLASS.TokenLinkedToken, hLinkedToken, TLTSize, iRetSize)
WriteLog "gettokeninformation: " & bRet & " linkedtoken: " & TOKEN_INFORMATION_CLASS.TokenLinkedToken & " linked2: " & hLinkedToken

'Use CreateEnvironment Block with the original token to create an environment for the new program with the USER Environment
Dim lpEB&
bRet = CreateEnvironmentBlock(lpEB, hToken, False)
WriteLog "Createenvblock: " & bRet

If bRet Then

Dim pi As PROCESS_INFORMATION
Dim si As STARTUPINFO
si.lpDesktop = uWinstationNameStrPtr ' '”Winsta0\default”
si.cb = Len(si)

Dim lRet&
lRet = CreateProcessAsUser( _
hLinkedToken, _
"", _
uAppName, _
0&, _
0&, _
0&, _
NORMAL_PRIORITY_CLASS, _
0&, _
0&, _
si, _
pi)

'Give user a feedback
If lRet <> 0 Then
WriteLog ":-) createprocessasuser succeeded!"
Else
WriteLog ":-( failed createprocessasuser! error: " & Err.LastDllError
End If
End If

WriteLog "pstartappinsessions}"

End Function

最佳答案

ERROR_FILE_NOT_FOUND 强烈提示找不到可执行文件。换句话说,问题出在 lpApplicationNamelpCommandLine 上。来自documentation :

The lpApplicationName parameter can be NULL. In that case, the module name must be the first white space–delimited token in the lpCommandLine string. If you are using a long file name that contains a space, use quoted strings to indicate where the file name ends and the arguments begin; otherwise, the file name is ambiguous. [...] If the file name does not contain an extension, .exe is appended.

如果 lpApplicationName 为 null,则 lpCommandLine 必须以您要运行的程序开头。如果程序路径包含空格,则路径必须用引号引起来。

如果 lpApplicationName 为 null,则可以通过将其值粘贴到命令提示符来测试 lpCommandLine 是否有效。

关于vb.net - 使用 Windows 服务启动提升的进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37467477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com