简体   繁体   中英

How to debug C DLL when called from a C# application in Visual Studio 2008

I have a C# windowed application that needs to make use of a third-party API, which is offered only in C. To solve this problem, I've created three projects within VS2008: two C# projects and an empty C++ project. One C# project is my "Wrapper" project, responsible only for producing managed code that calls the C DLL. The other C# project is the windowed application, which makes use of the Wrapper project; let's call this the GUI project.

Inside the C++ project, I've created several C files (*.c) that utilise the third-party API and export (dllexport) suitable functions. I can successfully compile this project into a DLL and have had no problems in calling these functions from my Wrapper project. In case it's relevant, my Wrapper project uses DllImport attributes to reference these functions.

My C++ project has a post-build event that copies the resulting DLL into the output directory of my GUI C# project so that it's picked up at execution time. This feels a bit grim, but it's the only way I've figured out how to do this. My GUI project has a dependency on my Wrapper project, which has a dependency on the C++ project.

What I'm struggling to do, however, is debug (ie step-through) my C project code. I've tried to set a breakpoint within the C code in the hope that it would be caught when my C# code executes the relevant function. Unfortunately, as soon as I run my C# application, the IDE warns me that the C breakpoints will never be executed: " No symbols have been loaded for this document ."

Any help with this would be greatly appreciated. Here are some things I've played with, but to no avail:

  • Ensuring the .pdb file has the same timestamp as the DLL file. This hint was followed after a random Google suggested the "No symbols" error could be caused by this.

  • I've selected "Enabled unmanaged code debugging" in both my C# project properties.

  • I've tried setting a breakpoint in my C# call just prior to an invocation of one of the DLL methods and attempted to step into the DLL. This didn't work either, it simply stepped over the function.

You should check in the modules list (Normally found in the Debug menu in Visual Studio) to make sure that:

  • The module is loaded
  • It is being loaded from the right place
  • The symbols have been correctly loaded

If the window lists the module, but indicates that symbols aren't loaded then you can force VS to load symbols by right clicking on the module and selecting "Load symbols". If it can't find them automatically then it will prompt you for a path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM