Search code examples
c#c++c++-cli

Debugging C++ static lib from C#


This question is similar to this unanswered question: Debugger does not step into native code when debugging a static lib wrapped in a C++/CLI DLL

The setup is the same. I have a pure C++ static library that is linked to a C++/CLI DLL which is then used by a C# executable. Depending on the settings, I can either debug the C# layer, or both C# and C++/CLI. No matter what I try, I can't debug the C++ layer. I'm using Visual Studio 2019.

Here's what I have tried and the results. For all scenarios described below, I have breakpoints set on the C++/CLI and C++ functions (not the C#).

  1. C# with native debugging, C++ and C++/CLI with auto debugging: The debugger stops at the calls in C# that would invoke the C++/CLI functions, but I cannot setp into them. Visual Studio has no messages for the breakpoints on the C++/CLI side (they are active but can't be hit). On the C++ side, it says:
This breakpoint will not currently be hit. Breakpoints in module clr.dll are not alowed. This module contains the implementation of the underlying runtime you are trying to debug.
  1. C# without native debugging, C++ and C++/CLI with mixed debugging: breakpoints in C++/CLI are hit and active. C++ breakpoints either disappear, or have the following message:
This breakpoint will not currently be hit. No executable code of the debugger's target code is associated with this line. Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the current debugger code type.
  1. C# with native debugging, C++ and C++/CLI with mixed debugging: see the first point, the behaviour is identical.
  2. C# without native debugging, C++ with native debugging, and C++/CLI with mixed debugging: same as point 2.

The code I have is the following:

C++ Native.hpp:

#pragma once
namespace native
{
   class Native
   {
   public:
      Native();

      bool here() const;
   };
}

C++ Native.cpp:

#include "Native.hpp"

#include <iostream>

namespace native
{
   Native::Native()
   {
      std::cout << "Created native entity!" << std::endl; // breakpoint here
   }

   bool Native::here() const
   {
      std::cout << "Native is here!" << std::endl; // breakpoint here
      return true;
   }
}

C++/CLI Wrapper.h:

#pragma once

#include "../cpp/Native.hpp"

using namespace System;

namespace clr
{
    public ref class Wrapper
    {
   public:
      Wrapper() 
      { 
         Console::WriteLine("Building wrapper for native"); // breakpoint here
         mNative = new native::Native(); 
      }
      ~Wrapper() { delete mNative; }
      bool go() 
      { 
         Console::WriteLine("In wrapper to find native..."); // breakpoint here
         return mNative->here(); 
      }

   private:
      native::Native* mNative;

    };
}

And C#:

using System;
using clr;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Wrapper w = new Wrapper();
            Console.WriteLine("Finding native through wrapper...");
            w.go();
            Console.WriteLine("Waiting...");
            Console.Read();
        }
    }
}

I have tried just about everything I can think of and I cannot debug the C++ side. I would really appreciate any help figuring this out.


Solution

  • It could be as simple as Enable native code debugging not being enabled in Visual Studio.

    learn.microsoft.com's Tutorial: Debug C# and C++ in the same debugging session contains the steps for a successful c#+c++ debugging session.