title: Native C++ to Managed C++ to C#
body: <p>I am working on porting a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#.</p>
  
  <p>Please, I know it'd be a lot easier to port the whole thing to .NET, but if I could I would.  It's 3rd party and all I have are .lib(no exports) and .h files to work with.</p>
  
  <p>Everything has been going smoothly until I hit virtual functions and now I'm having some delegate issues.   </p>
  
  <p>Among the errors I'm getting are:</p>
  
  <blockquote>
    <p>error C3756: 'ThreadFunc': delegate definition conflicts with an existing symbol<br>
    error C2079: 'MyWrapTest::MyThreadWrap::m_threadAttr' uses undefined class 'MyWrapTest::MyThreadAttrWrap'
    error C2664: 'MyWrapTest::AutoPtr::AutoPtr(T *)' : cannot convert parameter 1 from 'MyWrapTest::MyThreadAttrWrap' to 'MyThread *'</p>
  </blockquote>
  
  <p>For clarity, I'll include the native code and the stuff I'm working on now. First, native code:</p>
  
  <pre><code>#ifndef MYTHREAD_HPP
  #define MYTHREAD_HPP
  
  #ifdef WIN32
  #include &lt;winsock2.h&gt;
  #include &lt;windows.h&gt;
  #define STDCALL unsigned __stdcall
  typedef unsigned (__stdcall *ThreadFunc)(void*);
  #else
  #define STDCALL void*
  typedef void* (*ThreadFunc)(void*);
  typedef unsigned int HANDLE ;
  #endif
  #include "generaltypes.hpp"
  
  class MyThreadAttr;
  
  class MyThread
  {
  public:
      MyThread(void);
      MyThread(MyThreadAttr * tta);
      virtual ~MyThread() {};
      virtual HANDLE start(ThreadFunc,void *, unsigned *);
      virtual int stop();
      static void wait(HANDLE);
  #ifdef WIN32
      static void wait(HANDLE, int);// msec timeout required since 'cancelThread' is no-op  
  #endif                            
      static void sleep(unsigned int);
      static int32 cancelThread(HANDLE hThread);  // no-op on Windows (returns -1)!
  #ifndef WIN32
      static void setCancelStates(void);
      static void endProcess(); 
  #endif
  
  protected:
    MyThreadAttr * m_threadAttr;
    void setThreadAttr(MyThreadAttr * tta);
  };
  
  #endif
  </code></pre>
  
  <p>AND THE NEW STUFF I'M DEVELOPING:</p>
  
  <pre><code>#pragma once
  
  #ifdef WIN32
  #include &lt;winsock2.h&gt;
  #include &lt;windows.h&gt;
  #define STDCALL unsigned __stdcall
  //typedef unsigned (__stdcall ThreadFunc)(Object^);
  #else
  #define STDCALL Object^
  typedef unsigned int HANDLE;
  #endif
  #include "gentypes.hpp"
  #include "AutoPtr.h"
  #include "MyThread.hpp"
  
  using namespace System;
  using namespace System::Runtime::InteropServices;
  
  namespace MyWrapTest
  {
  
  public delegate Object^ ThreadFunc(Object^ o);
  
  ref class MyThreadAttrWrap;
  //#include "MyThreadAttrWrap.h"
  
  public ref class MyThreadWrap
  {
  public:
      MyThreadWrap(void)
      {
          AutoPtr&lt;MyThread&gt; m_NativeMyThread(new MyThread);
      };
      MyThreadWrap(MyThreadAttrWrap tta)
      {
          AutoPtr&lt;MyThread&gt; m_NativeMyThread(tta);
      };
      /*virtual ~MyThreadWrap(){};
      virtual HANDLE start(ThreadFunc,System::Object^, unsigned ^);
      virtual int stop();*/
      static void wait(HANDLE h)
      {
          m_NativeMyThread-&gt;wait(h);
      };
  #ifdef WIN32
      static void wait(HANDLE h, int i) // msec timeout required since 'cancelThread' is no-op  
      {
          m_NativeMyThread-&gt;wait(h, i);
      };
  #endif                            
      static void sleep(unsigned int i)
      {
          m_NativeMyThread-&gt;sleep(i);
      };
      static int32 cancelThread(HANDLE hThread);  // no-op on Windows (returns -1)!
  #ifndef WIN32
      static void setCancelStates(void);
      static void endProcess(); 
  #endif
  
  protected:
    MyThreadAttrWrap m_threadAttr;
    void setThreadAttr(MyThreadAttrWrap tta);
  
  
  private:
      AutoPtr&lt;MyThread&gt; m_NativeMyThread;
  };
  }
  </code></pre>