Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Help me!!! A issue of wrapping unmanaged class with Managed C++

Help me!!! A issue of wrapping unmanaged class with Managed C++

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++csharp
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anonymous
    wrote on last edited by
    #1

    I met a problem when I tried to wrap unmanaged class into managed class with managed C++. First, I built a static library, VcClass.lib, from the following source code with VC++ 6.0 : /* interface.h */ #include class CMyClassA { public: BYTE *m_pBuf; int m_bufLen; CMyClassA() { m_pBuf = NULL; m_bufLen = 0; } ~CMyClassA() { FreeBuf(); } HRESULT LoadBuf(BYTE *pBuf, DWORD dwBufLen); private: HRESULT FreeBuf(void); }; class CMyClassB { public: CMyClassB() { m_pA = NULL; } ~CMyClassB() { m_pA = NULL; } HRESULT Func(CMyClassA *pClassA); HRESULT Func2(void); private: CMyClassA *m_pA; }; #endif /* VcClass.cpp */ #include #include "interface.h" HRESULT CMyClassA::LoadBuf(BYTE *pBuf, DWORD dwBufLen) { m_bufLen = dwBufLen; m_pBuf = new BYTE[m_bufLen]; memcpy(m_pBuf, pBuf, m_bufLen); return 0; } HRESULT CMyClassA::FreeBuf(void) { m_bufLen = 0; delete [] m_pBuf; return 0; } HRESULT CMyClassB::Func(CMyClassA *pClassA) { m_pA->m_pBuf = pClassA->m_pBuf; m_pA->m_bufLen = pClassA->m_bufLen; for (int i=0; im_bufLen; i++) printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]); return 0; } HRESULT CMyClassB::Func2(void) { if (m_pA->m_pBuf == NULL) { printf("buf is empty, call Func() first!\n"); return 1; } for (int i=0; im_bufLen; i++) printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]); return 0; } then, I wrote two wrapped classes with VC++ 7.0 and compiled them into a DLL: #include "interface.h" using namespace System; namespace McClass { public __gc class McMyClassA { public: McMyClassA() { m_pClassA = new CMyClassA; } ~McMyClassA() { delete m_pClassA; } int LoadBuf(System::Byte Buf[], int Len) { System::Byte __pin * p = &Buf[0]; return m_pClassA->LoadBuf(p, Len); } void *GetA() { return (void*)m_pClassA; } private: CMyClassA * m_pClassA; }; public __gc class McMyClassB { public: McMyClassB() { m_pClassB = new CMyClassB; } ~McMyClassB() { delete m_pClassB; } int Func(McMyClassA *pMA) { CMyClassA *pA = (CMyClassA*)pMA->GetA(); return m_pClassB->Func(pA); } int Func2(void) { return m_pClassB->Func2(); } private: CMyClassB * m_pClassB; }; } Finally, I wrote an application with C# to call the DLL: using System; using McClass; namespace CSharpApp { class Class1 { [STAThread] static void Main(string[] args) { int i; McMyClassA cl

    A 1 Reply Last reply
    0
    • A Anonymous

      I met a problem when I tried to wrap unmanaged class into managed class with managed C++. First, I built a static library, VcClass.lib, from the following source code with VC++ 6.0 : /* interface.h */ #include class CMyClassA { public: BYTE *m_pBuf; int m_bufLen; CMyClassA() { m_pBuf = NULL; m_bufLen = 0; } ~CMyClassA() { FreeBuf(); } HRESULT LoadBuf(BYTE *pBuf, DWORD dwBufLen); private: HRESULT FreeBuf(void); }; class CMyClassB { public: CMyClassB() { m_pA = NULL; } ~CMyClassB() { m_pA = NULL; } HRESULT Func(CMyClassA *pClassA); HRESULT Func2(void); private: CMyClassA *m_pA; }; #endif /* VcClass.cpp */ #include #include "interface.h" HRESULT CMyClassA::LoadBuf(BYTE *pBuf, DWORD dwBufLen) { m_bufLen = dwBufLen; m_pBuf = new BYTE[m_bufLen]; memcpy(m_pBuf, pBuf, m_bufLen); return 0; } HRESULT CMyClassA::FreeBuf(void) { m_bufLen = 0; delete [] m_pBuf; return 0; } HRESULT CMyClassB::Func(CMyClassA *pClassA) { m_pA->m_pBuf = pClassA->m_pBuf; m_pA->m_bufLen = pClassA->m_bufLen; for (int i=0; im_bufLen; i++) printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]); return 0; } HRESULT CMyClassB::Func2(void) { if (m_pA->m_pBuf == NULL) { printf("buf is empty, call Func() first!\n"); return 1; } for (int i=0; im_bufLen; i++) printf("buf[%d] = %d\n", i, m_pA->m_pBuf[i]); return 0; } then, I wrote two wrapped classes with VC++ 7.0 and compiled them into a DLL: #include "interface.h" using namespace System; namespace McClass { public __gc class McMyClassA { public: McMyClassA() { m_pClassA = new CMyClassA; } ~McMyClassA() { delete m_pClassA; } int LoadBuf(System::Byte Buf[], int Len) { System::Byte __pin * p = &Buf[0]; return m_pClassA->LoadBuf(p, Len); } void *GetA() { return (void*)m_pClassA; } private: CMyClassA * m_pClassA; }; public __gc class McMyClassB { public: McMyClassB() { m_pClassB = new CMyClassB; } ~McMyClassB() { delete m_pClassB; } int Func(McMyClassA *pMA) { CMyClassA *pA = (CMyClassA*)pMA->GetA(); return m_pClassB->Func(pA); } int Func2(void) { return m_pClassB->Func2(); } private: CMyClassB * m_pClassB; }; } Finally, I wrote an application with C# to call the DLL: using System; using McClass; namespace CSharpApp { class Class1 { [STAThread] static void Main(string[] args) { int i; McMyClassA cl

      A Offline
      A Offline
      Anonymous
      wrote on last edited by
      #2

      sorry, I made a mistake. please don't reply. thanks.

      P 1 Reply Last reply
      0
      • A Anonymous

        sorry, I made a mistake. please don't reply. thanks.

        P Offline
        P Offline
        Paul Selormey
        wrote on last edited by
        #3

        deadlock, this is the problem of not logging in before posting. You could have deleted the thread. Best regards, Paul. Jesus Christ is LOVE! Please tell somebody.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups