title: C++/CLI wrapper for native C++ dll
body: <p>I have written an C++/Cli wrapper for a native C++ dll, but when I call some method from C# I get an System.AccessViolationException error in my C++/Cli Wrapper dll! It's necessary to marshal the unmanaged types or something else?!</p>
  
  <pre><code>// Wrapper.h
  
  typedef UnmanagedClass* (*Instance)(void);
  
  private:
      UnmanagedClass *m_object; // unmanaged object   
  
  // Wrapper.cpp
  
  Wrapper:Wrapper()
  {
      HINSTANCE unmanagedLib;
      unmangedLib = LoadLibrary(SystemStringToLPCSTR(dllPath+dllName));
  
      // load instance
      Instance _createInstance = (Instance)GetProcAddress(unmangedLib, "GetInstance");
      m_object = (_createInstance)(); 
  }
  
  Wrapper::~Wrapper()
  {
      m_object-&gt;~UnmanagedClass();
  }
  
  
  Uint32 Wrapper::SomeMethod(Uint8 *bytRecvBuffer, int &amp;iRecvLen)
  {
      return m_object-&gt;SomeMethod(bytRecvBuffer, iRecvLen);
  }
  
  // Unmanaged Class
  
  class UnmanagedClass    
  {
  public:
      /**
      * Default constructor. 
      */
      UnmanagedClass(void);
      /**
      * Default Destructor
      */
      ~UnmanagedClass(void);
  
      virtual Uint32 Wrapper::SomeMethod(Uint8 *bytRecvBuffer, int &amp;iRecvLen);
  };
  
  // export the UnmanagedClass object
  extern "C" _declspec(dllexport) UnmanagedClass* GetInstance();
  
  // UnamangedClass.cpp
  
  UnamangedClass::~UnamangedClass(void)
  {
      if (UnamangedClassDLL != NULL)
          FreeLibrary(UnamangedClassDLL);
  
      UnamangedClassDLL = NULL;
  }
  
  extern "C" _declspec(dllexport) UnmanagedClass* GetInstance()
  {
  
      return new UnmanagedClass();
  }
  </code></pre>
  
  <p>When I call at example SomeMethod from C# I get the error in C++/Cli dll!
  (I included the C++/cli dll with add reference in C sharp project and create the Wrapper object)</p>
  
  <p>Thank you for your help!</p>
  
  <p>greets</p>