Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's what I would start with.</p> <p>Usage:</p> <pre><code>IFoo foo = UnsafeNativeMethods.GetFooInterface(); foo.Method1(0, 1.0f); </code></pre> <p>Implementation:</p> <pre><code>internal interface IFoo { void Method1(int a, float b); void Method2(int a, float b, int c); } internal static class UnsafeNativeMethods { public static IFoo GetFooInterface() { IntPtr self = GetInterface(InterfaceType.Foo); NativeFoo nativeFoo = (NativeFoo)Marshal.PtrToStructure(self, typeof(NativeFoo)); return new NativeFooWrapper(self, nativeFoo.Method1, nativeFoo.Method2); } [DllImport("mydll.dll", EntryPoint = "getInterface", CallingConvention = CallingConvention.Cdecl)] private static extern IntPtr GetInterface(InterfaceType id); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Method1Delegate(IntPtr self, int a, float b); [UnmanagedFunctionPointer(CallingConvention.Cdecl)] private delegate void Method2Delegate(IntPtr self, int a, float b, int c); private enum InterfaceType { Foo, Bar } private struct NativeFoo { public Method1Delegate Method1; public Method2Delegate Method2; } private sealed class NativeFooWrapper : IFoo { private IntPtr _self; private Method1Delegate _method1; private Method2Delegate _method2; public NativeFooWrapper(IntPtr self, Method1Delegate method1, Method2Delegate method2) { this._self = self; this._method1 = method1; this._method2 = method2; } public void Method1(int a, float b) { _method1(_self, a, b); } public void Method2(int a, float b, int c) { _method2(_self, a, b, c); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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. 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.
 

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