Friday, April 20, 2007

Debugging VC++ dlls from .Net

In my current project we have to use a legacy VC++ dll to be called from our .Net assemblies. In addition I needed to be able to debug the C++ code from my .Net assembly project.

I found out that there are certain simple steps to carry out in order to achieve this.

1. Make the necessary functions in C++ exportable.

2. Make your C++ dll debuggable. You have to build the project on DEBUG configuration in order to that. There will be debug symbol files like .pdb being generated.

3. Now declare the calling method signature in your .Net code as follows inside the class.

[DllImport ../VCPlusProject/bin/Debug/VCPlus.dll", EntryPoint = "VCPlusMethodToCall", SetLastError = true)]
public static extern bool IsMethodCalled(int param)

4. The implemented method will be a normal .Net method as follows.

bool sucess = VCPlusMethodToCall(10);

Now everything is ready to let the cat out of the bag!

5. Start the VS.Net project in debug mode and try to debug in to the "VCPlusMethodToCall" method. The VC++ code will be debuggable inside VS.Net.


Post a Comment