views:

2205

answers:

4

Hi all,

I have a wrapper around a C++ function call which I call from C# code. How do I attach a debugger in Visual Studio to step into the native C++ code?

This is the wrapper that I have which calls GetData() defined in a C++ file:

    [DllImport("Unmanaged.dll", CallingConvention=CallingConvention.Cdecl, 
               EntryPoint = "GetData", BestFitMapping = false)]
        public static extern String GetData(String url);

The code is crashing and I want to investigate the root cause.

Thanks, Nikhil

+12  A: 

Check the Debug tab on your project's properties page. There should be an "Enable unmanaged code debugging" checkbox. This worked for me when we developed a new .NET UI for our old c++ DLLs.

If your unmanaged DLL is being built from another project (for a while ours were being built using VS6) just make sure you have the DLL's pdb file handy for the debugging.

The other approach is to use the C# exe as the target exe to run from the DLL project, you can then debug your DLL normally.

Lou
+2  A: 

in addition to Lou's advise for starting the debugger, you can select which debug engines are used when attaching to an existing process by clicking on 'Select...' in the 'attach to process' dialog and choosing both 'managed code' and 'native code'.

Debugging in this way is called mixed mode debugging. See this blog post for some tips.

I believe this isn't supported for 64 bit processes ... though would love to be wrong on that point.

Rob Walker
Thanks for the follow-up. I don't tend to use "attach to process" so I forget that it's another option. cheers.
Lou
+2  A: 

To anyone using WinDbg:

1>Setup symbols

Look at these commands. (Help: in console .hh < command> )

.sympath
.sympath+ 
.symfix

2>Set up source path

.srcpath

3>Load SOS extention to debug managed / mixed mode programs.

(Make sure you have extention path setup correctly)

Add Microsoft.NET\Framework\v2.0.50727 for x86 using-

.extpath

Set a breakpoint for the clr to load.

sxe ld:mscorwks

(F5 / g) (Wait for ModLoad BP on mscorwks.dll)

Make sure you dont have a duplicate sos extention already loaded. See:

.chain

Now we're ready to load the sos extention. :)

.loadby sos mscorwks

4> Reload all the symbols..

.reload

Now you're all set :)

(YMMV)

Kapil Kapre
+2  A: 

Mixed debugging is not supported in 64bit mode (as of Visual Studio 2008).

Andrei
Could you post any articles that you may have on this subject?
Zenox