I want to call a function from a C dll. This dll was created by compiling on ubuntu/mingw. I want to use this dll functions in C#. How can I do this?
Asked
Active
Viewed 879 times
2 Answers
1
You need to define a managed signature using the [DllImport]
attribute and then P/Invoke into the unmanaged library. Example from MSDN calling the MessageBox function from user32.dll
:
using System;
using System.Runtime.InteropServices;
class Example
{
// Use DllImport to import the Win32 MessageBox function.
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
static void Main()
{
// Call the MessageBox function using platform invoke.
MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
}
}

Darin Dimitrov
- 1,023,142
- 271
- 3,287
- 2,928
-2
- Add the DLL as a project resource
- Add the Using statement to associate the dll
- code away.
See Also: Adding a dll file to a C# project

Community
- 1
- 1

Michael Paulukonis
- 9,020
- 5
- 48
- 68