0

I have an API which is comprised of a header (.h) and the library (.a) files.

I've never touched C and i need to access this API from C# and interact with it's functions.

Most questions regarding this topic say i need to do something like [DllImport("insert_the_dll_name_here")] this to invoke the DLL methods. But i only have the .h and .a files.

What should i do?

Should i compile these files into the DLL and then use pInvoke? If so, How?

Or should i write a wrapper class? If so how?

Any help is appreciated.

3
  • 4
    What OS? .a files are usually compiled for Linux, while .NET is on Windows. Commented Jan 27, 2012 at 21:42
  • I use Windows. But i can also fire-up my virtualbox with linux if i have to. Are you saying i need to fire that up to compile the .a files? Commented Jan 30, 2012 at 13:55
  • If they're linux binaries (making calls to glibc), then you could link them inside a linux VM, but the resulting .so file wouldn't be any use on Windows (Windows DLLs can be used under Linux with the help of Wine, I know of no way to do the reverse). Commented Jan 30, 2012 at 15:35

1 Answer 1

5

If your .a files contain Windows-format object files, then you can extract the archive (use the cygwin ar tool for that) and link those into a C++/CLI project. C++/CLI can use your header files, call C functions directly, and create .NET classes which C# can use just as easily as any Microsoft-provided library class.

If your .a files contain Linux-format object files, you'll need to go back to whoever provided them and ask the code to be recompiled for Windows.

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

2 Comments

Thanks for your answer Ben (+1). How can i tell if the .a files contain Windows-format object files?
If the objdump program that comes with Visual C++ (or Windows SDK) gives reasonable output, they're Windows-format. (The roughly equivalent Linux tool is nm, but I wouldn't bet against it understanding Windows format also). I think you have to extract the objects first.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.