Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to use a C++ .lib file from within a C# program?

Tags:

c++

c#

Is it possible to use a C++ .lib file from within a C# program?

like image 408
Brian R. Bondy Avatar asked Oct 01 '08 18:10

Brian R. Bondy


People also ask

What is .LIB file in C?

A library in C is a collection of header files, exposed for use by other programs. The library therefore consists of an interface expressed in a . h file (named the "header") and an implementation expressed in a . c file. This .

Can you use C libraries in C#?

C Libraries compiled for Windows can be called from C# using Platform Invoke. From MSDN, the syntax of making a C function call is as follows: [DllImport("Kernel32. dll", SetLastError=true)] static extern Boolean Beep(UInt32 frequency, UInt32 duration);

How do I play a .LIB file?

To load the LIB file, select File → Load Library..., navigate to the location of your LIB file, select the file, and click Open.


1 Answers

There are plenty of ways. Read about "interop" in MSDN..

One way is to expose the lib as a DLL, and then use pinvoke to call these functions from a C# project. That limits you to a C-style interface, though.

If your interface is more complex (e.g. object oriented) you can create a C++/CLI layer that will expose the lib's class structure to your C# program. This means you'll have to create a managed C++ (or C++/CLI as it's now called) project; then design an interface in managed code that will be implemented by calls to native C++ (i.e. your lib).

Another way of doing this is by wrapping your lib with a COM interface. But COM's a pain, so I wouldn't...

like image 127
Assaf Lavie Avatar answered Oct 05 '22 06:10

Assaf Lavie