C# – way to call an unmanaged (not COM) dll from C# application

cinteropmarshallingunmanaged

Is there a way to use (reference) a DLL written in an unmanaged C++ (not a COM library) in my C# application?

When I try to reference it from within Visual Studio, I get 'not a COM object' error message.

Maybe there is some kind of translator\router that would COMify my DLL reference?
I have no clue how COM and COM interop work, since I started programming when this was already unnecessary for me.

Thank you.

Best Answer

You need to use the DllImport attribute. Here's an example for the Win32 PostMessage function:

[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr handle, int message, IntPtr wparam, IntPtr lparam);