为什么在使用控制台应用程序测试时,dll中的进程是否正常工作,但是当被另一个dll调用时,是

时间:2011-09-26 01:30:10

标签: c# dll process redirectstandardoutput

上周我发布了这个问题:How can I obtain console application output when running it as a process in a C# dll 因为我试图找出问题的原因。但是,我一直无法找到我收到错误的原因,所以我想我会要求跟进直接解决我遇到的问题。

我正在研究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通常作为控制台应用程序运行,FileName和Arguments用于重现此格式:     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    

另一个编辑: 我发现我在系统事件查看器中收到此错误。直到现在我才注意到它。 “应用程序弹出:XTandemAdapter.exe - 应用程序错误:应用程序无法正确初始化(0xe06d7363)。单击”确定“终止应用程序。”

这有助于任何人理解这个问题。我应该注意,可执行文件确实需要使用一些dll,但根据依赖walker这些都可以找到。

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

我会从“Hello,world!”开始。控制台应用程序和调试过程的东西分开你的真实应用程序。这可能会帮助您确定导致返回代码的原因(即您的调用代码或控制台应用程序)。

答案 2 :(得分:0)

从控制台应用程序运行时有效,但从运行不同程序的DLL运行时失败。对我来说,这听起来像安全上下文的问题。您的控制台应用程序可能以比其他程序更高的权限运行。