Is it possible to use DLLs with Visual Studio Mac

SteveFerg picture SteveFerg · Nov 30, 2016 · Viewed 10.4k times · Source

I have an application that uses a third party DLL. Is there a way in Visual Studio for Mac to write an application to access it the same way as I can on windows?

Answer

Sevo Kukol picture Sevo Kukol · Dec 12, 2016

It depends:

  1. Managed .NET DLL: Can be imported and used the same way as in VS on Windows
  2. Native DLL: can't be used directly. You'll need to build it for your target system, OSX in your case.

If you have C/C++ code you should be able to build it for OSX (with GCC for example) if it doesn't have some foreign (Windows) platform specific code. Then you can use the compiled *.so / *.dylib file directly. But you'll need to tell the .NET Runtime (Mono for example on OSX) to use the different file using a DllMap configuration file (see http://www.mono-project.com/docs/advanced/pinvoke/dllmap/ for examples).

The sources you've linked look like they're for Unix, so the chance to be able to build them on OSX are pretty good (there's a Makefile and the resulting binary would be libswe.so on Unix). You could try to pass the -dynamiclib parameter to GCC to get a OSX specific libswe.dylib (What are the differences between .so and .dylib on osx? seems to be a good answer about dylib)

If you have the binary of your DLL for OSX, you just need a configuration file for your .NET application, which could look like this:

<configuration>
    <dllmap os="osx" dll="libswe.dll" target="libswe.dylib"/>
<configuration/>

It tells the .NET Runtime to import the symbols from libswe.dylib instead of libswe.dll if the current OS is OSX.