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#).
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.
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.
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.
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.