gpt4 book ai didi

c# - 为什么在 DLL 文件中启动的进程在使用控制台应用程序进行测试时有效,但在被另一个 DLL 文件调用时却无效?

转载 作者:太空狗 更新时间:2023-10-29 20:42:12 24 4
gpt4 key购买 nike

上周我发布了问题 How can I obtain console application output when running it as a process in a C# DLL file? 因为我试图找出我遇到的问题的原因。但是,我无法找到我遇到错误的原因,所以我想我会询问直接与我遇到的问题相关的跟进。

我正在处理 DLL 文件中的方法,我必须在其中启动一个进程。用于执行此操作的代码是:

ProcessStartInfo psi = new ProcessStartInfo();
psi.UseShellExecute = false;
psi.ErrorDialog = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.CreateNoWindow = true;
psi.FileName = @"C:\Program Files\OpenMS-1.6\XTandemAdapter.exe";
psi.Arguments = @"-ini C:\XTandemAdapter.ini";

Process getIDs = new Process();
getIDs.StartInfo = psi;
getIDs.Start();
StreamWriter inputWriter = getIDs.StandardInput;
StreamReader outputReader = getIDs.StandardOutput;
StreamReader errorReader = getIDs.StandardError;
getIDs.WaitForExit();
System.Diagnostics.EventLog.WriteEntry("FMANWiff", "ID output: " + outputReader.ReadToEnd());
System.Diagnostics.EventLog.WriteEntry("FMANWiff", "ID error: " + errorReader.ReadToEnd());

应用程序 XTandemAdapter.exe 通常作为控制台应用程序运行,文件名和参数旨在重现此格式:

XtandemAdapter.exe -ini XTandemAdapter.ini

我有一个控制台测试应用程序调用此 DLL 文件中的方法。当我使用它时,我能够看到 standoutput 重定向的结果,并且我可以看到该过程成功执行(被调用的可执行文件还生成一个 XML 文件作为输出,我可以看到它已创建)。然而,正常的操作模式有一个应用程序调用 DLL 中的一个方法,该方法最终调用我的。当我以这种方式运行它时,我可以看到该进程是通过查看任务管理器创建的,但它很快退出并且没有输出到事件日志并且运行的可执行文件没有创建输出文件。

为什么在一种情况下它可以正常运行,而在另一种情况下却不能?通过控制台应用程序调用与通过 DLL 文件中的方法调用时,执行的操作是否不同?

我注意到进程返回的 Exitcode 是 Exitcode: -529697949 所以我猜进程中出了点问题。我将查看 xtandemadapter 的代码并尝试找出它的来源。当我从控制台应用程序运行它时,它返回 0。

当我添加调试器 break 语句时,我最终单步执行该方法并观察进程对象的值,无论是在使用控制台测试应用程序还是在实际使用中。我发现了差异,但我不确定如何理解它们。

工作控制台测试应用程序:

-       getIDs  {System.Diagnostics.Process (XTandemAdapter)}   System.Diagnostics.Process
+ base {System.Diagnostics.Process (XTandemAdapter)} System.ComponentModel.Component {System.Diagnostics.Process}
BasePriority 8 int
EnableRaisingEvents false bool
ExitCode 9 int
+ ExitTime {10/4/2011 1:21:33 AM} System.DateTime
+ Handle 1036 System.IntPtr
HandleCount 53 int
HasExited true bool
Id 2732 int
MachineName "." string
+ MainModule 'getIDs.MainModule' threw an exception of type 'System.ComponentModel.Win32Exception' System.Diagnostics.ProcessModule {System.ComponentModel.Win32Exception}
+ MainWindowHandle 0 System.IntPtr
MainWindowTitle "" string
+ MaxWorkingSet 'getIDs.MaxWorkingSet' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ MinWorkingSet 'getIDs.MinWorkingSet' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ Modules 'getIDs.Modules' threw an exception of type 'System.ComponentModel.Win32Exception' System.Diagnostics.ProcessModuleCollection {System.ComponentModel.Win32Exception}
NonpagedSystemMemorySize 3240 int
NonpagedSystemMemorySize64 3240 long
PagedMemorySize 3010560 int
PagedMemorySize64 3010560 long
PagedSystemMemorySize 120196 int
PagedSystemMemorySize64 120196 long
PeakPagedMemorySize 3010560 int
PeakPagedMemorySize64 3010560 long
PeakVirtualMemorySize 137424896 int
PeakVirtualMemorySize64 137424896 long
PeakWorkingSet 9064448 int
PeakWorkingSet64 9064448 long
+ PriorityBoostEnabled 'getIDs.PriorityBoostEnabled' threw an exception of type 'System.InvalidOperationException' bool {System.InvalidOperationException}
+ PriorityClass 'getIDs.PriorityClass' threw an exception of type 'System.InvalidOperationException' System.Diagnostics.ProcessPriorityClass {System.InvalidOperationException}
PrivateMemorySize 3010560 int
PrivateMemorySize64 3010560 long
+ PrivilegedProcessorTime {00:00:00.0937500} System.TimeSpan
ProcessName "XTandemAdapter" string
+ ProcessorAffinity 'getIDs.ProcessorAffinity' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
Responding true bool
SessionId 0 int
+ StandardError {System.IO.StreamReader} System.IO.StreamReader
+ StandardInput {System.IO.StreamWriter} System.IO.StreamWriter
+ StandardOutput {System.IO.StreamReader} System.IO.StreamReader
+ StartInfo {System.Diagnostics.ProcessStartInfo} System.Diagnostics.ProcessStartInfo
+ StartTime {10/4/2011 1:21:32 AM} System.DateTime
SynchronizingObject null System.ComponentModel.ISynchronizeInvoke
+ Threads {System.Diagnostics.ProcessThreadCollection} System.Diagnostics.ProcessThreadCollection
+ TotalProcessorTime {00:00:00.8125000} System.TimeSpan
+ UserProcessorTime {00:00:00.7187500} System.TimeSpan
VirtualMemorySize 132001792 int
VirtualMemorySize64 132001792 long
WorkingSet 9064448 int
WorkingSet64 9064448 long
+ Static members
+ Non-Public members

当我按预期调用 dll 时以及它不起作用时:

-       getIDs  {System.Diagnostics.Process}    System.Diagnostics.Process
+ base {System.Diagnostics.Process} System.ComponentModel.Component {System.Diagnostics.Process}
+ BasePriority 'getIDs.BasePriority' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
EnableRaisingEvents false bool
ExitCode -529697949 int
+ ExitTime {10/4/2011 1:03:09 AM} System.DateTime
+ Handle 4176 System.IntPtr
+ HandleCount 'getIDs.HandleCount' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
HasExited true bool
Id 596 int
MachineName "." string
- MainModule 'getIDs.MainModule' threw an exception of type 'System.ComponentModel.Win32Exception' System.Diagnostics.ProcessModule {System.ComponentModel.Win32Exception}
+ base {"Access is denied"} System.Runtime.InteropServices.ExternalException {System.ComponentModel.Win32Exception}
NativeErrorCode 5 int
+ Non-Public members
- MainWindowHandle 'getIDs.MainWindowHandle' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ base {"Process has exited, so the requested information is not available."} System.SystemException {System.InvalidOperationException}
+ MainWindowTitle 'getIDs.MainWindowTitle' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
+ MaxWorkingSet 'getIDs.MaxWorkingSet' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ MinWorkingSet 'getIDs.MinWorkingSet' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ Modules 'getIDs.Modules' threw an exception of type 'System.ComponentModel.Win32Exception' System.Diagnostics.ProcessModuleCollection {System.ComponentModel.Win32Exception}
+ NonpagedSystemMemorySize 'getIDs.NonpagedSystemMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ NonpagedSystemMemorySize64 'getIDs.NonpagedSystemMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PagedMemorySize 'getIDs.PagedMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PagedMemorySize64 'getIDs.PagedMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PagedSystemMemorySize 'getIDs.PagedSystemMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PagedSystemMemorySize64 'getIDs.PagedSystemMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PeakPagedMemorySize 'getIDs.PeakPagedMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PeakPagedMemorySize64 'getIDs.PeakPagedMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PeakVirtualMemorySize 'getIDs.PeakVirtualMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PeakVirtualMemorySize64 'getIDs.PeakVirtualMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PeakWorkingSet 'getIDs.PeakWorkingSet' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PeakWorkingSet64 'getIDs.PeakWorkingSet64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PriorityBoostEnabled 'getIDs.PriorityBoostEnabled' threw an exception of type 'System.InvalidOperationException' bool {System.InvalidOperationException}
+ PriorityClass 'getIDs.PriorityClass' threw an exception of type 'System.InvalidOperationException' System.Diagnostics.ProcessPriorityClass {System.InvalidOperationException}
+ PrivateMemorySize 'getIDs.PrivateMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ PrivateMemorySize64 'getIDs.PrivateMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ PrivilegedProcessorTime {00:00:00.0468750} System.TimeSpan
+ ProcessName 'getIDs.ProcessName' threw an exception of type 'System.InvalidOperationException' string {System.InvalidOperationException}
+ ProcessorAffinity 'getIDs.ProcessorAffinity' threw an exception of type 'System.InvalidOperationException' System.IntPtr {System.InvalidOperationException}
+ Responding 'getIDs.Responding' threw an exception of type 'System.InvalidOperationException' bool {System.InvalidOperationException}
+ SessionId 'getIDs.SessionId' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ StandardError {System.IO.StreamReader} System.IO.StreamReader
+ StandardInput {System.IO.StreamWriter} System.IO.StreamWriter
+ StandardOutput {System.IO.StreamReader} System.IO.StreamReader
+ StartInfo {System.Diagnostics.ProcessStartInfo} System.Diagnostics.ProcessStartInfo
+ StartTime {10/4/2011 1:03:09 AM} System.DateTime
SynchronizingObject null System.ComponentModel.ISynchronizeInvoke
+ Threads 'getIDs.Threads' threw an exception of type 'System.InvalidOperationException' System.Diagnostics.ProcessThreadCollection {System.InvalidOperationException}
+ TotalProcessorTime {00:00:00.0781250} System.TimeSpan
+ UserProcessorTime {00:00:00.0312500} System.TimeSpan
+ VirtualMemorySize 'getIDs.VirtualMemorySize' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ VirtualMemorySize64 'getIDs.VirtualMemorySize64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ WorkingSet 'getIDs.WorkingSet' threw an exception of type 'System.InvalidOperationException' int {System.InvalidOperationException}
+ WorkingSet64 'getIDs.WorkingSet64' threw an exception of type 'System.InvalidOperationException' long {System.InvalidOperationException}
+ Static members System.Diagnostics.Process System.Diagnostics.Process
+ Non-Public members {System.Diagnostics.Process} System.Diagnostics.Process
'getIDs.MainModule' threw an exception of type 'System.ComponentModel.Win32Exception' Too many characters in character literal

我发现我在系统事件查看器中遇到了这个错误。直到现在我才注意到它。

"Application popup: XTandemAdapter.exe - Application Error : The application failed to initialize properly (0xe06d7363). Click on OK to terminate the application."

这是否有助于任何人理解问题?我应该注意,可执行文件确实需要使用一些 DLL 文件,但根据 dependency walker,这些文件都已找到。

最佳答案

为什么没有记录事件更有趣。我建议您将 System.Diagnostics.Debugger.Break(); 放在应用程序的第一行。这将允许您在调试器被调用后附加它,然后您可以观察行为。

关于c# - 为什么在 DLL 文件中启动的进程在使用控制台应用程序进行测试时有效,但在被另一个 DLL 文件调用时却无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7549852/

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