C#C
C#4y ago
100 replies
Up

C++ Interop - writing a shim to use an existing c++ library [Answered]

So I'm currently trying to make a shim for a C++ library that I can then call from C#.
The original library uses CMake, not MSBuild, so I've set up Cmake, let it generate the .vcxproj files, made a 2nd C++ project for my shim classes and made it depend on the generated projects.
Then I've added that + the generated projects into my C# msbuild solution.

Now I am trying to call a hello world function that I made in my shim library form C#, but that immediately fails with no indicator as to what went wrong. all I get is System.Runtime.InteropServices.SEHException: 'External component has thrown an exception.'.

How I declared it on the C++ side:
extern "C" {
  void __declspec(dllexport) __stdcall CLIP_Hello();
}


How I declared it on the C# side:
[DllImport("ClipShim", EntryPoint = "CLIP_Hello", CallingConvention = CallingConvention.StdCall)]
public static extern void Hello();


any help appreciated!
Was this page helpful?