views:

1142

answers:

3

I'm developing a C# assembly which is to be called via COM from a Delphi 7 (iow, native win32, not .net) application.

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.

So that's great, but soon I'm going to really want to use Visual Studio to debug the C# code while it's running. Set breakpoints, step through code, all that stuff.

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.

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.

Just please don't tell me I have to make do with MessageBox.Show etc.

+3  A: 

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.

Will
That is a really neat trick!
Blorgbeard
+1  A: 

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.

I put a Afx MsgBox when the object is created to stop the application's flow and then attach the debugger.

Nick
+4  A: 

In the VS2008 project properties page, on the Debug tab, there's an option to set a different Start Action.

This can be used to run an external program (e.g. your Delphi app) when you press F5.

Mark Pattison
Wow, it works! Thanks.
Blorgbeard
It is pretty handy. I discovered it when writing a C# assembly which was being called from Excel.
Mark Pattison