Search This Blog

Wednesday, December 9, 2009

Debugging VB6 COM DLLs via .Net

I am sure there are other ways of doing this but this post is just a simple step by step guide on one of them.

More often than not we have to use legacy COM components while writing new applications, let it be for database access, business logic or to simply avoid duplicating code.  Every time I end up using a COM object in my .Net application I lose the ability to debug inside that COM object and mostly have to assume that whatever happens within that call is logically correct.  Sometimes I am not convinced so I need to see proof of it first hand, so what do I do, I debug the COM call.

Adding a COM object to .Net cannot be easier, simply add a reference to the COM object which is registered on your computer and .Net automatically creates what we call a CCW (COM Callable Wrapper).  This is widely documented here.

In order to demonstrate how to debug a COM call let us first create a very simple VB6 DLL called Project1.dll.  In this COM DLL I have a single class called Class1.cls which contains a very simple Add method as shown below:

Option Explicit

Public Sub Add(ByVal a As Integer, ByVal b As Integer, ByRef result As Integer)
result = a + b
Exit Sub
End Sub

After creating this method, I built the DLL to a location using the default build options within the VB6 IDE by going to File>Make Project1.dll.


After this was done I created a new ConsoleApplication solution in C# to reference this COM DLL.  Added a reference to Project1 which allowed me to do this:


Project1.Class1Class class1 = new Project1.Class1Class();
short res = 0;
class1.Add(1, 2, ref res);

Great, I can now use the add method which I wrote in the COM object, say if the COM method returns the wrong value and I want to step into that code, I simply go Debug>Step Into (F11).  But what happens is that the debugger simply steps over that code and gets the result.  What has happened is that none of the symbols for that Project1.dll were loaded into my IDE:

image


This can be achieved simply by building the VB6 DLL using symbolic information.  You can do this by right clicking on the VB6 project in your IDE and going to Properties>Compile and check the boxes which say “Create Symbolic Debug Info” and also select “No Optimization”.


image

After the above is done, rebuild the Project1.dll.  You’  Simply doing the above step will not allow you to step into the “Add” function because the .Net IDE still hasn’t loaded the symbols.  To allow for this we have to enabled “Unmanaged Debugging” for our project, this can be done by going into the Project Properties>Debug and checking the box “Enable unmanaged code debugging” as shown below.

image


Doing the above process will now load the symbols:

image


Doing the above steps will still not allow us to step into the VB6 code, for this we have to do a manual step.  Open up the code file which has the “Add” function defined and put a breakpoint at the desired line of code.  Now when you step into the “Add” method call you will hit the breakpoint on the VB6 side as shown below:


image


Voila! :)

2 comments:

  1. Thanks Dude. It works and you saved me a lot of time. Keep the good work.

    ReplyDelete
  2. Debugging DLL via .NET is interesting. This is more useful for me.

    VB6 to C#

    ReplyDelete