views:

44

answers:

1

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 (i.e. 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.

A: 

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.

Kragen
We could be onto a winner here. The modules list shows a red exclamation mark and warns me that the binary was not built with debug information.
Duncan Jones
Whoops, posted too early. Anyway, I've attempted to load the symbols file and it warns me that it doesn't match. I've got the scent now so I'm sure I'll be onto a solution any time. I'll mark you as the answerer as you've sent me in the right direction. Will comment back with the full answer shortly.
Duncan Jones
@Duncan - Sounds like you need to check the debug info build settings for your C++ project.
Kragen
@Kragen - Yup, you solved it. Thanks to your tip, I realised my DLL was not being correctly copied into my GUI project output directory. Instead, I was using a stale version of the DLL from before I had correctly embedded debug symbols in it. Thank you!
Duncan Jones
What a nightmare. After thinking I had solved this problem, I now face exactly the same dilemma. Now my DLL is not even showing in the Modules windows, however it's certainly working (my application functions just fine). Can't figure out what's going wrong now - I've still got my DLL (and pdb, for good measure) getting copied into my C# GUI bin/Debug directory. Why would it not show in the Module window?
Duncan Jones
@Myself - Well, there's three hours I won't see again. For some reason I'd managed to uncheck the "Enable unmanaged code debugging" checkbox. Problem solved again.
Duncan Jones