title: Invalid Variant crash
body: <p>I have a situation where I've wrapped a Native C++ DLL with C++/CLI for eventual use in C#. </p>
  
  <p>There are a few callback functions that are causing some issues at run time.  Particularly, I get the following exception:</p>
  
  <blockquote>
    <p>An unhandled exception of type
    'System.Runtime.InteropServices.InvalidOleVariantTypeException'
    occurred in ToadWrapTest.dll</p>
    
    <p>Additional information: Specified OLE
    variant is invalid.</p>
  </blockquote>
  
  <p>On this line of code (C++/CLI):</p>
  
  <pre><code>public delegate int ManagedCallbackFunction (Object^ inst, const Object^ data);
  public delegate int UnManagedCallbackFunction (void* inst, const void* data);
  
  ManagedCallbackFunction^ m_callbackFn;
  
  int intermidiaryCallback(void * pInstance, const void * pData)
      {   
          void* temp = (void*)pData;
          System::IntPtr ip1 = IntPtr(pInstance);
          System::IntPtr ip2 = IntPtr(temp);
          Object^ oInst = Marshal::GetObjectForNativeVariant(ip1);
          Object^ oData = Marshal::GetObjectForNativeVariant(ip2);
          //invoke the callback to c#
          //return m_callbackFn::Invoke(oInst, oData);
          return 0;
      };
  </code></pre>
  
  <p>The reason I've made this "intermediary callback" was an attempt to circumvent the Invalid variant exception being thrown when I tried to directly map the delegate from C# to the native C++ code.  As an attempted work-around, I declare a delegate on the C# side and pass that funcptr to the C++/CLI wrapper.  I then pass the intermediary funcptr to the native C++ and just daisy chain the calls together.</p>
  
  <p>What I know is that it all works in native C++ world.  The problem is mapping the void* to the managed world.  The following code shows the native C++ version of the callback: </p>
  
  <pre><code>int (*CallbackFunction) (void *inst, const void *data);
  </code></pre>
  
  <p>If anyone can help here, I'd really appreciate it.  </p>