Help me!!! A issue of wrapping unmanaged class with Managed C++
-
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
-
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
-
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.