title: CoCreateInstance() in C#
body: <p>I am converting a piece of code from c++ to c#, but am unsure how to use CoCreateInstance(). Here is the c++ (this works and is what I want in C#):</p>
  
  <pre><code>IComDevice *Device=NULL;
  HRESULT hr = CoCreateInstance(
      __uuidof(ComDevice),
      NULL,
      1,
      IID_IComDevice,
      reinterpret_cast&lt;void **&gt;(&amp;Device)
      );
  </code></pre>
  
  <p>and here is what I thought would work in c#</p>
  
  <pre><code>public class Ole32Methods
  {
    [DllImport("ole32.Dll")]
    static public extern uint CoCreateInstance(ref Guid clsid, [MarshalAs(UnmanagedType.IUnknown)] object inner, uint context, ref Guid uuid, [MarshalAs(UnmanagedType.IUnknown)] out object rReturnedComObject);
  }
  </code></pre>
  
  <p>And then in main</p>
  
  <pre><code>IComDevice Device;
  uint duh = 1;
  Guid uuid=typeof(ComDevice).GUID;
  Guid uuid2 = typeof(IComDevice).GUID;
  Ole32Methods.CoCreateInstance(ref uuid, null, duh, ref uuid2, out Device);
  </code></pre>
  
  <p>IComDevice is an interface in a .dll made in c++. I reference it in my C# code. Basically I need this code because I can't call any functions in the interface unless I initialize my Device. CoCreateInstance was doing that in my C++ code. The code I posted doesn't run because it can't use Device as object. I notice that in the C++ code the Device is a pointer, which may be something to implement here. I am just unsure.</p>
  
  <p>UPDATE: I used <a href="https://stackoverflow.com/questions/15987491/cocreateinstance-exact-match-in-net">CoCreateInstance exact match in .NET?</a> to write my code, but his doesn't use an interface, pointers, or reinterpretcast</p>