Click here to Skip to main content
15,797,672 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 30-Jun-10 4:44am
v3
Comments
Richard MacCutchan 30-Jun-10 7:57am    
There is no word 'virture', try 'virtual' and it may compile.
liuyunhong 30-Jun-10 9:42am    
You are right, mis spell, just in nodepad

1 solution

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.
 
Share this answer
 
Comments
liuyunhong 1-Jul-10 3:33am    
Your suggestion is one direction.
I want to wrapper a COM dll,the the C# app interop the COM DLL, May be it is a better solution.
voloda2 1-Jul-10 3:53am    
If your DLL is COM than it's better to use COM interop. Otherwise I would prefer the managed C++, which will allow you to have event delegates etc.
liuyunhong 2-Jul-10 3:31am    
yes, managed C++ wrapper will ok. but i donot like managed C++.
if i work for unmanaged C++ is the best, or C# is the best.

And I am trying to build a ATL COM DLL for the raw dll. use the Event in C# to get the Objiect callback function.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900