Search code examples
c#pinvoke

Pinvoking functions from a static C library


I have a native C++ library (static i.e. .lib target). I wish to use some of the functions in this file in my C# projects. For dlls, I could pinvoke them. How do I do this for static libraries?

I read this question and there David's answer says you can pinvoke only for dlls and not for libs.


Solution

  • A static library is used by linking it into a larger module. In Windows that means a DLL or an executable.

    They do not stand alone and only make sense when you link them into a larger module. So, that would imply that, in order to use pinvoke you need to build a DLL which includes the library, and pinvoke to that DLL.

    As an alternative to pinvoke, you could make a mixed mode C++/CLI assembly. Link the static library to that C++/CLI assembly and expose the functionality via a C++ managed class. That managed class can then be consumed by your C# code.