Search code examples
c#visual-studiovisual-studio-2017visual-studio-debugging

Why isn't my C# assembly able to be debugged when loaded from a native C++ application?


I have an unmanaged C++ application which has a dependency on a C# assembly (interacted via COM).

When I run my native C++ application through the VS debugger, breakpoints in C# code are disabled with an error no symbols are loaded. From this question I found to look in the Modules window: How do I remedy the "The breakpoint will not currently be hit. No symbols have been loaded for this document." warning?

However, my C# assembly DLL is not listed in the module list. When the application starts up in the debugger, my C# assembly is not mentioned in the Output window either when it is loaded and used. The C++ is only a simple wrapper around the C# code so not being able to debug C# code is a problem. I can't attach-to-process because the C# code is in an assembly not a separate application.

This question seems to be on a similar topic, but the converse situation, so I don't see any solution there: "The breakpoint will not currently be hit" error while debugging a mixed mode application (c# and unmanaged c++)

Is there a way to debug my managed C# code when used by an unmanaged native C++ application?


Solution

  • Based on CodeCaster's comment:

    You need to be debugging managed code (Attach to -> Select), and you're probably not. Is your assembly already loaded and the CLR running when you attach the debugger

    I was able to find a solution. If I start the application through the debugger, symbols for the managed assembly are not loaded at startup; the assembly is only loaded when one of its types is instanced via COM.

    If I attach the debugger to an already-running process, the managed assembly is already loaded and now everything works as expected. It is not ideal if you want to set a breakpoint at startup, but it is a workable solution.