Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use LIB file in C# project?

There is a lib file along with .h, which is working with some device driver. A sample Visual Studio C++ project is using that library as static linked.

There are only 2 functions exported from that library.

HANDLE open_dev();
HANDLE open_file( char *filename );

I want to use that library in my C# project and again static link it. How it's possible?

like image 787
Pablo Avatar asked Nov 26 '25 14:11

Pablo


1 Answers

You can create a DLL where you create a wrapper function for each function from the .lib:

extern "C" __declspec (dllexport) HANDLE OpenFile(char const * filename)
{
    return open_file(filename);
}

In C# you use P/Invoke to make the dll function available:

[DllImport("WrapperLib.dll")]
private static extern System.IntPtr OpenFile(string filename);
like image 145
wimh Avatar answered Nov 28 '25 03:11

wimh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!