gpt4 book ai didi

c# - 无法以不同用户身份运行 C# 进程

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:26 36 4
gpt4 key购买 nike

我正在尝试使用不同的用户(不是服务用户)从 C# 服务中使用 System.Diagnostics.Process 运行 djoin.exe 工具。

该过程返回代码 -1073741502

在事件日志中我可以看到:

Application popup: djoin.exe - Application Error : The application was unable to start correctly (0xc0000142). Click OK to close the application.

没有标准错误或标准输出。

这是我使用的进程配置:

ProcessStartInfo startInfo = new ProcessStartInfo
{
Arguments = "/Provision /Domain domain.com /Machine PC12 /SaveFile NUL /printblob",
WorkingDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
FileName = "djoin.exe"
UseShellExecute = false,
RedirectStandardError = true,
RedirectStandardInput = true,
RedirectStandardOutput = true,
CreateNoWindow =true,
Domain = "domain.com",
UserName = "other-user",
Password = "***"
};

if (username!=null)
{
startInfo.Domain = domain;
startInfo.UserName = username;
startInfo.Password = ToSecureString(password);
}

p = new Process { StartInfo = startInfo };
p.Start();

当使用 RUNAS 命令时,一切正常。

问题是什么?

最佳答案

好像是权限问题。这可以在 exe 所在的文件夹级别,也可以与运行该进程的用户有关。

要诊断此问题,您可以先转到 exe 所在的文件夹。然后右键单击并将权限设置为“所有人”并具有完全控制权。然后尝试再次运行,看看是否收到相同的消息。

此外,当您运行 Visual Studio 时,在开始时,右键单击并以管理员身份运行。我从您的评论中得知这可以正常工作,这让我相信它实际上与许可相关。例如不同的用户是否在同一个域中?计算出应用程序所在文件夹的权限后,创建一个具有该文件夹权限的帐户,然后让任何进程安排/运行 exe 在该帐户下执行。

更新 - 上面的评论提示了另一个想法,您可以使用 system.diagnostics 在代码的每个点写入事件日志条目,以帮助确定出了什么问题。另一个可能有用的工具,如果 WinDBG 可以获取有关引发该异常的原因的更多信息。

关于c# - 无法以不同用户身份运行 C# 进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26086898/

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