Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The example will be, for <em>Linux</em>:</p> <p>1) Create a <code>C</code> file, <code>libtest.c</code> with this content:</p> <pre><code>#include &lt;stdio.h&gt; void print(const char *message) { printf("%s\\n", message); } </code></pre> <p>That’s a simple pseudo-wrapper for printf. But represents any <code>C</code> function in the library you want to call. If you have a <code>C++</code> function don’t forget to put extern <code>C</code> to avoid mangling the name.</p> <p>2) create the <code>C#</code> file</p> <pre><code>using System; using System.Runtime.InteropServices; public class Tester { [DllImport("libtest.so", EntryPoint="print")] static extern void print(string message); public static void Main(string[] args) { print("Hello World C# =&gt; C++"); } } </code></pre> <p>3) Unless you have the library libtest.so in a standard library path like “/usr/lib”, you are likely to see a System.DllNotFoundException, to fix this you can move your libtest.so to /usr/lib, or better yet, just add your CWD to the library path: <code>export LD_LIBRARY_PATH=pwd</code></p> <p>credits from <a href="http://www.moythreads.com/wordpress/2008/02/04/pinvoke-how-to-call-c-from-c/">here</a></p> <p><strong>EDIT</strong></p> <p>For <em>Windows</em>, it's not much different. Taking an example from <a href="http://www.codeproject.com/Articles/6912/Calling-methods-from-Dll-compiled-in-C-from-C">here</a>, you only have yo enclose in your <code>*.cpp</code> file your method with <code>extern "C"</code> Something like</p> <pre><code>extern "C" { //Note: must use __declspec(dllexport) to make (export) methods as 'public' __declspec(dllexport) void DoSomethingInC(unsigned short int ExampleParam, unsigned char AnotherExampleParam) { printf("You called method DoSomethingInC(), You passed in %d and %c\n\r", ExampleParam, AnotherExampleParam); } }//End 'extern "C"' to prevent name mangling </code></pre> <p>then, compile, and in your C# file do</p> <pre><code>[DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")] public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam); </code></pre> <p>and then just use it:</p> <pre><code>using System; using System.Runtime.InteropServices; public class Tester { [DllImport("C_DLL_with_Csharp.dll", EntryPoint="DoSomethingInC")] public static extern void DoSomethingInC(ushort ExampleParam, char AnotherExampleParam); public static void Main(string[] args) { ushort var1 = 2; char var2 = ''; DoSomethingInC(var1, var2); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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