加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 为什么在使用控制台应用程序测试时,在dll中启动进程,而在

发布时间:2020-12-15 07:49:56 所属栏目:百科 来源:网络整理
导读:上周我发布了这个问题: How can I obtain console application output when running it as a process in a C# dll 因为我试图找出问题的原因.但是,我一直无法找到我收到错误的原因,所以我想我会要求跟进直接解决我遇到的问题. 我正在研究DLL中的方法,我必须
上周我发布了这个问题: 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 FilesOpenMS-1.6XTandemAdapter.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.

编辑:当我添加调试器中断语句时,我最终逐步执行该方法并观察过程对象的值,无论是在使用控制台测试应用程序还是使用现实世界时.我发现了差异,但我不知道该怎么做.

工作控制台测试应用:

-       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这些都可以找到.

解决方法

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读