windows 当从本机 win32 应用程序调用 C# COM 程序集时,如何调试它?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/126868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I debug a C# COM assembly when it's being called from a native win32 application?
提问by Blorgbeard is out
I'm developing a C# assembly which is to be called via COM from a Delphi 7 (iow, native win32, not .net) application.
我正在开发一个 C# 程序集,它将从 Delphi 7(iow,本机 win32,而不是 .net)应用程序通过 COM 调用。
So far, it seems to work. I've exported a TLB file, imported that into my Delphi project, and I can create my C# object and call its functions.
到目前为止,它似乎有效。我已经导出了一个 TLB 文件,将它导入到我的 Delphi 项目中,然后我可以创建我的 C# 对象并调用它的函数。
So that's great, but soon I'm going to reallywant to use Visual Studio to debug the C# code while it's running. Set breakpoints, step through code, all that stuff.
这很好,但很快我就真的想使用 Visual Studio 在运行时调试 C# 代码。设置断点,单步执行代码,所有这些。
I've tried breaking in the Delphi code after the COM object is created, then looking for a process for VS to attach to, but I can't find one.
我已经尝试在创建 COM 对象后打破 Delphi 代码,然后寻找一个让 VS 附加到的进程,但我找不到一个。
Is there a way to set VS2008 up to do this? I'd prefer to just be able to hit f5 and have VS start the Delphi executable, wait for the C# code to be called, and then attach itself to it.. But I could live with manually attaching to a process, I suppose.
有没有办法设置 VS2008 来做到这一点?我更希望能够点击 f5 并让 VS 启动 Delphi 可执行文件,等待 C# 代码被调用,然后将其自身附加到它..但我想我可以接受手动附加到进程。
Just please don't tell me I have to make do with MessageBox.Show etc.
请不要告诉我我必须使用 MessageBox.Show 等。
回答by Mark Pattison
In the VS2008 project properties page, on the Debug tab, there's an option to set a different Start Action.
在 VS2008 项目属性页面的调试选项卡上,有一个选项可以设置不同的启动操作。
This can be used to run an external program (e.g. your Delphi app) when you press F5.
当您按 F5 时,这可用于运行外部程序(例如您的 Delphi 应用程序)。
回答by Mark Pattison
Place the following in the method you wish to debug:
将以下内容放入您要调试的方法中:
#if DEBUG
if (!System.Diagnostics.Debugger.IsAttached)
Debugger.Launch();
#endif
When you want to debug, build a debug version and use that in your application. When this code runs, a dialog pops up asking if you want to attach a debugger.
当您想要调试时,构建一个调试版本并在您的应用程序中使用它。当此代码运行时,会弹出一个对话框,询问您是否要附加调试器。
回答by Nick
You can just attach to the native application and see breakpoint, view stacks, watches etc. normally. You'll need to attach after the COM object is created.
您可以只附加到本机应用程序并正常查看断点、查看堆栈、监视等。创建 COM 对象后,您需要附加。
I put a Afx MsgBox when the object is created to stop the application's flow and then attach the debugger.
我在创建对象时放置了一个 Afx MsgBox 以停止应用程序的流程,然后附加调试器。