如何在C#应用程序和C ++ dll之间相互调用对象的接口类 [英] how to inter call object's interface class between C# app and C++dll
问题描述
在我的c ++ dll中执行以下操作:
我的dll头文件(例如:test.h):
(1)定义一个回调接口类:
class CClassCallback { public: virtual HRESULT OnAdded() = 0; virtual HRESULT OnDeled() = 0; virtual HRESULT OnFailed() = 0; };
(2)这样的结构:
struct SAType { CString strTest; int nTest; CClassCallback* pClassCallback; };
(3)对象接口类,例如:
class CTestInterface { public: virtual HRESULT Test1(SAType* pTest) = 0; virtual HRESULT Test2() = 0; virtual void Release() = 0; };
(4)唯一的导出dll功能:
__declspec(dllexport) CTestInterface* ConstructInterface();
我的dll cppfile(例如:test.cpp):
(1)从CTestInterface派生一个类
class CTestObject:public CTestInterface { public: HRESULT Test1(SAtype& Test) { m_sType = Test; } HRESULT Test2() { } void Release() { delete this; //delete this object } private: void InnerDemoFuc() { if(true) { //in some condition callback the app by a callback object m_sType.pClassCallback->OnAdded(); } } SAtype m_sType; }
(2)构造一个对象并返回其接口:
CTestInterface* ConstructInterface() { return new CTestObject(); }
dll没关系.(实际代码更复杂)
当我在MFC UI APP中使用我的dll时,如下所示:
(1)从CClassCallback派生一个类:
class CClassCallbackEx : public CClassCallback { public: CClassCallbackEx(); virtual HRESULT OnAdded() { .....//do UI app job independly from dll.. } virtual HRESULT OnDeled() { } virtual HRESULT OnFailed() { } // can add other func ,var for app needs. }
(2)像这样使用dll:
//construct a dll''s object,and get its interface CTestInterface* m_pInterface = ConstructInterface(); SAType sAtype; //new a callback object sAtype->pClassCallback = new CClassCallbackEx(); m_pInterface->Test1(sAtype); ....... // all are ok ,delete the ojbject through interface m_pInterface->Relese(); m_pInterface=NULL; delete (CClassCallbackEx)sAtype->pClassCallback ;
好吧.
但是当我想在C#+ .net UI应用程序中使用我的dll时,会出现一些问题:
(1)我无法获取CTestObject实例的接口的成员函数,
由于接口类未导出.我尝试获取对象的虚拟功能表失败.
(2)我必须使用C#代码从C ++接口派生一个类,并在C ++ dll中使用,这意味着ojbect回调,而不是funccallback(OOP)
我不知道该怎么办.
换句话说:
(1)C#通过virture类使用C ++ Dll对象
(2)C ++ dll通过虚拟类使用C#对象
给我一些建议,请给我一些建议.
我建议将托管C ++用作本地C ++库的包装器".以后,您可以根据需要在C#(VB)代码中使用它.
In a my c++ dll do like this:
my dll headerfile(eg:test.h):
(1) define a callback interface class:
class CClassCallback { public: virtual HRESULT OnAdded() = 0; virtual HRESULT OnDeled() = 0; virtual HRESULT OnFailed() = 0; };
(2) a struct like this:
struct SAType { CString strTest; int nTest; CClassCallback* pClassCallback; };
(3)a object interface class like:
class CTestInterface { public: virtual HRESULT Test1(SAType* pTest) = 0; virtual HRESULT Test2() = 0; virtual void Release() = 0; };
(4) the only export dll function:
__declspec(dllexport) CTestInterface* ConstructInterface();
my dll cppfile(eg:test.cpp):
(1) derived a class from CTestInterface
class CTestObject:public CTestInterface { public: HRESULT Test1(SAtype& Test) { m_sType = Test; } HRESULT Test2() { } void Release() { delete this; //delete this object } private: void InnerDemoFuc() { if(true) { //in some condition callback the app by a callback object m_sType.pClassCallback->OnAdded(); } } SAtype m_sType; }
(2) construct a object and return its interface:
CTestInterface* ConstructInterface() { return new CTestObject(); }
that''s ok for dll.(real code is more complex)
when I use my dll in MFC UI APP like this:
(1) derive a class from CClassCallback:
class CClassCallbackEx : public CClassCallback { public: CClassCallbackEx(); virtual HRESULT OnAdded() { .....//do UI app job independly from dll.. } virtual HRESULT OnDeled() { } virtual HRESULT OnFailed() { } // can add other func ,var for app needs. }
(2) Use the dll like this:
//construct a dll''s object,and get its interface CTestInterface* m_pInterface = ConstructInterface(); SAType sAtype; //new a callback object sAtype->pClassCallback = new CClassCallbackEx(); m_pInterface->Test1(sAtype); ....... // all are ok ,delete the ojbject through interface m_pInterface->Relese(); m_pInterface=NULL; delete (CClassCallbackEx)sAtype->pClassCallback ;
run ok.
but when i want use my dll in c# + .net UI app,some questions:
(1) i cannot get the CTestObject instance''s interface''s member function,
because the interface class is not export.i try to get the object''s virture function table,failed.
(2) i have to derive a class from c++ interface in c# code, and use in C++dll, means ojbect callback, not funccallback (OOP)
i donot know how to do.
other words:
(1) C# use a C++Dll ojbect through virture class
(2) C++dll use a C# object through virture class
Give me some suggestions,pls
I would suggest usage of managed C++ as a "wrapper" around the native C++ library. Later you can consume it in your C# (VB) code as you want.
这篇关于如何在C#应用程序和C ++ dll之间相互调用对象的接口类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!