0

As the description states. I have some static .lib files created in other rather large c++ projects containing a class I would like to be able to use in C#. What would the best way to use these classes from inside c#. The C++ libraries are not managed.

3 Answers 3

3

By far the easiest option is to build a wrapper library in C++/CLI.

Sign up to request clarification or add additional context in comments.

8 Comments

Sounds like the only reasonable option to me, since the stuff in the .lib file will almost certainly rely on the C++ runtime libraries that are in other .lib files, and will need to run through a linker before it can be executed.
What @marcelo-cantos said: use C++/CLI as a bridge between the managed and unmanaged worlds. It is easy.The only other options you have are (1) write a wrapper DLL that exports extern "C" functions that call onto your existing C++ objects. Then use [DllImport] in C# to call the DLL's C functions. Or (2), wrap your native C++ classes in COM interfaces and use C# COM Interop to invoke methods on the classes. The C++/CLI bridge is by far the easiest option.
@dripdeef: This should be an answer.
I've been working on this as the COM interop seems to only allow for dll's and I have lib's. I just have some trouble converting a managed string to QString etc. but I'm sure in time I will get that figured out.
@dko: No, don't use COM interop. Build a C++/CLI project and link your static libraries directly into it. Write C++/CLI classes (which are .Net classes) that act as simple wrappers for the native classes in the libraries and you're done. You can then link the C++/CLI project to your C# project. The C++/CLI compiler will automagically generate all the necessary interop code to call the native static code from the managed C++ classes. They call it IJW, for "It Just Works".
|
1

I am afraid class exporting from native .lib files is not possible inside C#. But using C++ Cli, you could wrap the class and make use of it via wrapper .net class. But this approach also requires a header (.h or .hpp) file to included.

COM dlls are different story. They can be referenced in a C# project and MSVS automatically wrapps the native classes.

Comments

1

What @marcelo-cantos said: use C++/CLI as a bridge between the managed and unmanaged worlds. It is easy.

The only other options you have are:

(1) Write a wrapper DLL that exports extern "C" functions that call onto your existing C++ objects. Then use [DllImport] and PInvoke in C# to call the DLL's C functions.

(2) Wrap your native C++ classes in COM interfaces and use C# COM Interop to invoke methods on the classes.

The C++/CLI bridge is by far the easiest option.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.