简体   繁体   English

仅当使用Process.Start()在C#中作为外部程序运行时,C控制台程序才会在退出(访问冲突)时崩溃。

[英]C console program crashes on exit (Access Violation) only when run as an external program in C# using Process.Start()

I have a console program written in C that uses an unmanaged library (dll) to perform actions with a USB device. 我有一个用C编写的控制台程序,该程序使用非托管库(dll)对USB设备执行操作。 It reads and uses the parameters passed to the main function. 它读取并使用传递给主函数的参数。 This program works and exits OK when compiled and executed standalone (in a command line). 该程序可以正常运行,并且可以独立(在命令行中)编译和执行时退出OK。

Then I have a second program written in C# that should call the first program with some parameters, using the following code: 然后,我有了用C#编写的第二个程序,该程序应使用以下代码调用带有某些参数的第一个程序:

Process runProg = new Process(); 
runProg.StartInfo.FileName = @"C:\Path\to\my\program.exe";
runProg.StartInfo.Arguments = @"hello123 testing123@test.com";
runProg.Start();
runProg.WaitForExit();

It runs the first program which does what it was supposed to do (I checked what the USB device did), but when returning from the program it crashes and shows me the "This program has stopped working" window. 它运行第一个程序,该程序执行了应做的工作(我检查了USB设备的工作),但是从该程序返回时,它崩溃并显示“此程序已停止工作”窗口。

Debugger says: 调试器说:

"Unhandled exception at 0x77962EE5 (ntdll.dll) in program.exe: 0xC0000005: Access violation reading location 0x6E650254."

What would be different when running the first program from the C# program as opposed to the windows command line that would cause this? 从C#程序运行第一个程序与导致此问题的Windows命令行有什么不同? Have I forgotten some part? 我忘记了什么吗?

Is there another way to run the first program? 还有另一种方法可以运行第一个程序吗? Maybe a way to tell Windows to execute the first program with cmd.exe, not try whatever it is trying to do now that causes this crash? 也许是一种告诉Windows使用cmd.exe执行第一个程序的方法,而不是尝试立即尝试导致该崩溃的任何方法? A way to make it functionally the same as if I execute the first program exe manually, which works OK? 一种使其在功能上与手动执行第一个程序exe相同的方法,可以正常工作吗?

This is kind of a shot in the dark, but have to tried changing UseShellExecute to false? 这是一个黑暗的镜头,但是必须尝试将UseShellExecute更改为false吗?

MSDN Process Docs MSDN流程文档

I solved the issue. 我解决了这个问题。 Michael's suggestion to find the crash in program.exe made me look at the output in the IDE for program.exe, and I noticed the errors were happening there too, it just wasn't crashing. 迈克尔的建议是在program.exe中发现崩溃,这使我着眼于IDE中针对program.exe的输出,并且我注意到那里也发生了错误,但并不是崩溃。

Turns out I had a couple of malloc 'd variables that I forgot to free . 原来,我有几个忘记释放malloc变量。 Fixing that fixed the errors and the crashing in the C# application. 此修复程序修复了错误以及C#应用程序中的崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM