2
votes

In a C# app, I'm referencing a native C static lib, that I wrapped in a C++/CLI DLL. I chose a static lib versus a DLL because I have other constraints related to the release process of the app to the user. Among the many topics available on this forum I found the following implementation.

Main :

{
  MyCLRDLL test = new MyCLRDLL();
  if(test.go()) Console.WriteLine("Hello, wrld");
}

In the DLL project, the file MyCLRDLL.hpp

#include "MyNativeLib.h"
#pragma comment(lib, "MyNativeLib.lib")
namespace InteropTest {
public ref class MyCLRDLL
{
  CMyNativeLib* mInt ;
  public:
    MyCLRDLL()  { mInt = CMyNativeLib_New() ;} ;
    ~MyCLRDLL() { CMyNativeLib_Delete(mInt) ;} ;
    bool go() { return mInt->areYouThere()  ;} ;
};
}

And in the native project, MyNativeLib.h

namespace InteropTest
{
class __declspec(dllexport) CMyNativeLib
{
public:
  CMyNativeLib() {};
    ~CMyNativeLib(){};
  bool areYouThere() ;
} ;
extern "C" __declspec(dllexport) CMyNativeLib* CMyNativeLib_New();
extern "C" __declspec(dllexport) void CMyNativeLib_Delete(CMyNativeLib* pLib);
}

and MyNativeLib.cpp

#include "MyNativeLib.h"
namespace InteropTest {
extern "C" __declspec(dllexport) CMyNativeLib* CMyNativeLib_New(){return new CMyNativeLib() ;}
extern "C" __declspec(dllexport) void CMyNativeLib_Delete(CMyNativeLib* pLib){delete pLib;}
bool CMyNativeLib::areYouThere() { return true ; }
}

The debugger is not stepping into mInt->areYouThere() function. Why is that so? Breakpoints in the native part are not caught either.

Symbols are loaded for MyCLRDLL, "Enable unmanaged code debugging" is active on C# project, "Debugging/Debugger Type" is set to Mixed for both C projects and in the general options, most relevant things seem checked.

Does it mean that the MyCLRDLL.pdb file does not contain the debug information from the lib code? How to check?

(possible same question as unanswered Debugging an unmanaged C++ static lib included in a CLR .dll)

1
This is by design, you cannot single-step from managed code into unmanaged code. You must set a breakpoint on the native code. And of course don't forget to enable unmanaged code debugging.Hans Passant
OK, but when I set a breakpoint on the native code (@ areYouThere function in the example), it does not stop, even though unmanaged code debugging is enabled. Have you tried this specific example?user3463671
Actually you can step into unmanaged code dll (C++) from a managed exe (C#) at least in VS2013, so I wonder of the static lib is the prb.Fernando Gonzalez Sanchez
Finally switched to VS2013. I can know debug mixed-mode, with Options>Debugging>General>Compatibility mode switched on.user3463671

1 Answers

-1
votes

I tried the same setup that you have. C# exe project, with a reference to a .dll project that has CLR support which has a reference to a .lib with pure Native C++ code.

First of all, in the executable project properties you must enable Debugging in Native code unde Debug tab. Next, you won't be able to run with debugger attached since it won't load the symbols for the native part of the CLR. I succeded by running without debugger attached, then setting the CLR project to as Startup Project. After that you must attach to the process with CTRL+ALT+P, with Native code in "Attach to:". That for me worked.