Create automatic DLL api binding from C++ to C#

Viewed 75

Suppose I have a C++ DLL and a header file containing all the API's.

extern "C" __declspec(dllexport) void* f1(const char* name);
extern "C" __declspec(dllexport) bool f2(const char* name, double* ptr, int* length);
//...
extern "C" __declspec(dllexport) int fn(int* size, double* distance);

And I want to use this in C# - thus crate the according mappings in C#.

[DllImport(@"mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[SuppressUnmanagedCodeSecurity]
private static extern unsafe IntPtr f1(string name);

[DllImport(@"mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[SuppressUnmanagedCodeSecurity]
private static extern unsafe bool f2(string name, double* ptr, int* length);

//...

[DllImport(@"mydll.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
[SuppressUnmanagedCodeSecurity]
private static extern unsafe int fn(int* size, double* distance);

So far so good, all works well.

Now suppose I have a C++ header with 100 API's and want to export them all, seems unreasonable to create individual api mappings manually for all 100 - there should be an automated way.
Are there any tools/libs/frameworks that can take this .h file from C++ and create according mappings in C#?

Something like Vanara but for our custom DLL/APIs.

0 Answers
Related