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

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

+6
c ++ c #
source share
8 answers

Not. You can only use the full .dll from a C # program.

+1
source share

There are many ways. Read about "interop" on MSDN ..

One way is to open lib as a DLL, and then use pinvoke to call these functions from a C # project. However, this limits the C-style interface.

If your interface is more complex (for example, object-oriented), you can create a C ++ / CLI layer that will reveal the structure of the lib class to your C # program. This means that you will need to create a C ++ managed project (or C ++ / CLI, now called); then create an interface in managed code that will be implemented by calls in C ++ (i.e. your lib).

Another way to do this is to wrap your library using the COM interface. But com is a pain, so I would not ...

+8
source share

Not directly. You can create a C ++ / CLI assembly that consumes lib, and then access it with C #, or you can wrap lib as a DLL.

+6
source share

You need a managed shell (C ++ / CLI) around your own C / C ++ library that you are working with.

If you are looking for any C ++ / CLI book, I would recommend Nishant Sivakumar C ++ / CLI in action

+3
source share

Already answered to wrap it, but here is an example . Good luck

+2
source share

I would look at swig , we use this to influence our project well, to bring our C ++ API to another platform language.

This is a well-supported project that effectively creates a thin shell around your C ++ library that can allow languages ​​like C # to directly communicate with your own code - eliminating the need to execute (and debug) the glue code.

+2
source share

It depends on whether you have any restrictions on this scenario?

If you have a lib file, you can compile it into a DLL file first, secondly, expose the functions that you want to call in the DLL interface, and thirdly, call them using C # native methods (see pinvoke.net about how to make this bit).

0
source share

you cannot use lib, but like others, you can use it if you wrap it in a dll.

swig can take the headers of your .lib, and if they are not too complex, it can generate a dll for you, which you then call with pinvoke from C #, which will also be generated by swig.

if your library is complex and contains references to smart pointers everywhere, you should find an alternative.

0
source share

All Articles