Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>I believe you can call functions inside an extern "C" block from C# by just referencing the DLL.</p> </blockquote> <p>Wrong, you have to use <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.dllimportattribute.aspx" rel="nofollow noreferrer"><code>DllImport</code></a> or so-called <a href="http://www.codeproject.com/KB/cs/essentialpinvoke.aspx" rel="nofollow noreferrer">PInvoke</a> (Platform Invoke) to call a native C function from managed code. Referencing works only for .NET assemblies or COM with an <a href="http://www32.brinkster.com/srisamp/netArticles/article_16.htm" rel="nofollow noreferrer">automatically generated interop dll</a>.</p> <p>Using C++ from C# becomes a real nightmare because of name mangling among other things.</p> <p>If you can you could compile a managed C++ Dll as a wrapper to make both worlds meet. This has also the advantage that you can mark your assembly as <a href="http://msdn.microsoft.com/en-us/library/ms182157%28VS.90%29.aspx" rel="nofollow noreferrer"><code>ComVisible</code></a>, thus making it available to plenty of tools capable of handling COM.</p> <p>An alternative is to write a C wrapper around the C++ API, what can be tedious and ugly.</p> <p>EDIT:</p> <p>Help to decide which approach to use:</p> <ul> <li>1) You have a native C dll <ul> <li>WARNING: if you use a .lib from Managed C++, then the dll is not truely dynamically linked and cannot be replaced with a simple drop in of a newer compatible version.</li> <li>PREFERRED: Use P/Invoke from any .NET language (drop-in replacement possible)</li> </ul></li> </ul> <blockquote> <p></p> </blockquote> <ul> <li>2) You have a native C++ dll <ul> <li>DO NOT use P/Invoke, this is a real nightmare (because of name mangling among other things)</li> <li>PREFERRED: Build a wrapper .NET dll with managed C++, works only if you have a compiler compatible with the one that was used to compile the native dll.</li> <li>If the native DLL was build with an incompatible compiler (Compiler A) of the one you can use for managed C++ (Compiler C), then I advise to build (With the same compiler A) a C wrapper around the native C++ dll. Then use the P/Invoke method to use this wrapper C dll like described in 1) </li> </ul></li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload