C# 为什么在使用控制台应用程序测试时,在DLL文件中启动的进程可以工作,而在被另一个DLL文件调用时却不能工作?

C# 为什么在使用控制台应用程序测试时,在DLL文件中启动的进程可以工作,而在被另一个DLL文件调用时却不能工作?,c#,dll,process,redirectstandardoutput,C#,Dll,Process,Redirectstandardoutput,上周,当我试图找出一个问题的原因时,我发布了这个问题。然而,我一直无法找到我出错的原因,所以我想我会直接就我遇到的问题进行跟进 我正在处理DLL文件中的方法,我必须在其中启动一个进程。用于执行此操作的代码是: ProcessStartInfo psi = new ProcessStartInfo(); psi.UseShellExecute = false; psi.ErrorDialog = false; psi.RedirectStandardError = true; psi.Redire

上周,当我试图找出一个问题的原因时,我发布了这个问题。然而,我一直无法找到我出错的原因,所以我想我会直接就我遇到的问题进行跟进

我正在处理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最终调用我的方法。当我以这种方式运行它时,我可以看到该进程是通过查看任务管理器创建的,但它很快退出,并且没有输出到事件日志,并且运行的可执行文件也没有创建输出文件

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

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

当我添加debugger 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    
我发现在系统事件查看器中出现此错误。直到现在我才注意到

“应用程序弹出窗口:XTandemAdapter.exe-应用程序错误:应用程序未能正确初始化(0xe06d7363)。单击“确定”终止应用程序。”


这有助于任何人理解问题吗?我应该注意到,可执行文件确实需要使用一些DLL文件,不过根据dependency walker的说法,这些文件都可以找到。

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

我将从一个“Hello,World!!”控制台应用程序开始,并与实际应用程序分开调试进程内容


这可能有助于确定返回代码(即调用代码或控制台应用程序)的原因。

当从控制台应用程序运行时,它会工作,但当从其他程序运行的DLL文件运行时,它会失败


对我来说,这听起来像是安全上下文的问题。您的控制台应用程序可能正在以比其他程序更高的权限运行。

因为这是第二次请求帮助,+1才能引起注意。我认为
XTandemAdapter.exe
决定在您的DLL中出于某种原因停止该程序。我们不知道它为什么会停止程序,但一些常见的原因可能是XTandemAdapter.exe没有写入ini文件的权限,或者ini文件已经存在,等等。实际上,它只是使用ini文件获取执行XTandemAdapter的设置。它定义了输入和输出文件,以及它所包装的程序的一些参数,XTandem!最后,我发现这与进程无法找到它们所依赖的DLL有关。出于某种原因,在使用测试应用程序时这不是问题。但是我把目录添加到路径中,它就工作了。@Travis:你介意把最后的评论放在一个答案上并接受它吗?当有人在寻找未回答的问题时,一路浏览这个问题,却在隐藏的评论中发现问题已经解决了,这不是很有效。人们阅读这篇文章所花的时间可能会用于回答尚未解决的问题;)哦,我想我可能有点误导了你,或者我没有正确地阅读你的信息。我确实得到了一个事件日志条目,但根本没有输出,条目只会说:“ID:”此外,文档中说“如果要将RedirectStandardOutput设置为true,则必须将UseShellExecute设置为false。否则,从StandardOutput流读取会引发异常。”,所以我不认为我可以将shell execute设置为true,并且永远希望得到standard out.Fail。但我的第二点意见仍然有效。对不起,这是漫长的一天。没问题,我完全理解。我明天在实验室的时候会试试。