title: C# wrapper for unmanaged DLL
body: <p>First of all - I know how to call unmanaged functions. I'm stuck with interface methods in unmanaged dll. I think, writing wrapper - is what I need.</p>
  
  <p>Please, help me with real code.</p>
  
  <p>I have:</p>
  
  <ul>
  <li>old DLL (C);</li>
  <li>2 header files;</li>
  <li>lib files (what for?).</li>
  <li>!NO SOURCES!</li>
  </ul>
  
  <p>Here is some code in .h file (point of interest - DECLARE INTERFACE):</p>
  
  <pre><code>#ifdef  __BUILD_DLL
  #define BLNETPASS_EXPORT __declspec(dllexport)
  #else
  #define BLNETPASS_EXPORT
  #endif
  
  #include "BlNetpassUid.h"
  extern "C"
  {
   BLNETPASS_EXPORT HRESULT WINAPI BlNetPassCreateA
                   (LPCSTR pHostName, const GUID *, VOID **);
  
   BLNETPASS_EXPORT HRESULT WINAPI BlNetPassCreateW
                   (LPCWSTR pHostName, const GUID *, VOID **);
  
   BLNETPASS_EXPORT HRESULT WINAPI CanBeUnload (DWORD dTimeout );
  
  
  ...
  
  #ifdef  UNICODE
  #define BlNetPassCreate BlNetPassCreateW
  #else
  #define BlNetPassCreate BlNetPassCreateA
  #endif
  
  #undef INTERFACE
  #define INTERFACE INetPass
  DECLARE_INTERFACE_ (INetPass, IUnknown)
  {
  // IUnknown methods
  
   STDMETHOD  (    QueryInterface)(REFIID, VOID **)   PURE;
   STDMETHOD_ (ULONG,      AddRef)()          PURE;
   STDMETHOD_ (ULONG,     Release)()          PURE;
  
  // INetPass methods
  
   STDMETHOD  ( CreateNetPassEnum)(VOID **, REFIID cid,
                           REFIID iid)    PURE;
  };
  </code></pre>
  
  <p>It's not a COM object, i think, i can't use type library import for this dll</p>
  
  <p>What I need:</p>
  
  <ul>
  <li>call BlNetPassCreateA; </li>
  <li>get pointer to interface INetPass; </li>
  <li>call interface method CreateNetPassEnum.</li>
  </ul>
  
  <p>That's how i did it before (it's PInvoke):</p>
  
  <pre><code>[DllImport("BlNetpassApi")] 
  public static extern int BlNetPassCreateA(string pHostName, ref Guid GUID, out IntPtr rINetPass);
  
  ...
  ...
  
   IntPtr something; 
   Guid temp_guid = Guid.Empty;
  
   int temp_int = BlNetPassCreateA(null, ref temp_guid, out something);
  </code></pre>
  
  <p>Ok! IntPtr contains some data, but what's next - how do I describe the interface that is behind and call its method?</p>