一个com组件的练习
//
{7B55D765-68A6-404e-99E2-CA9ABC389A27}
static const GUID IID_IUnknown9 =
{ 0x7b55d765 , 0x68a6 , 0x404e , { 0x99 , 0xe2 , 0xca , 0x9a , 0xbc , 0x38 , 0x9a , 0x27 } } ;
// {060CAA56-33E9-4c79-BE5B-0F70A5DDFE15}
static const GUID CLSID_myadd =
{ 0x60caa56 , 0x33e9 , 0x4c79 , { 0xbe , 0x5b , 0xf , 0x70 , 0xa5 , 0xdd , 0xfe , 0x15 } } ;
// {9A03B1AC-8253-4e69-971F-1E88E817F6FA}
static const GUID IID_IAdd =
{ 0x9a03b1ac , 0x8253 , 0x4e69 , { 0x97 , 0x1f , 0x1e , 0x88 , 0xe8 , 0x17 , 0xf6 , 0xfa } } ;
// {F03E6879-75E9-4cbc-A123-2AD92545869C}
static const GUID IID_ISubcat =
{ 0xf03e6879 , 0x75e9 , 0x4cbc , { 0xa1 , 0x23 , 0x2a , 0xd9 , 0x25 , 0x45 , 0x86 , 0x9c } } ;
class IUnknown9
{
public :
virtual HRESULT QueryInterface(REFIID riid, void ** ppvObject) = 0 ;
virtual ULONG AddRef( void ) = 0 ;
virtual ULONG Release( void ) = 0 ;
protected :
private :
} ;
class IAdd: public IUnknown9
{
public :
virtual int add( int , int ) = 0 ;
} ;
class ISubcat: public IUnknown9
{
public :
virtual int sub( int , int ) = 0 ;
protected :
private :
} ;
class Cmyadd: public IAdd, public ISubcat
{
public :
HRESULT QueryInterface(REFIID riid, void ** ppvObject)
{
if (riid == IID_IUnknown9)
{
* ppvObject = (IAdd * ) this ;
AddRef();
return S_OK;
} else if (riid == IID_IAdd)
{
* ppvObject = (IAdd * ) this ;
AddRef();
return S_OK;
} else if (riid == IID_ISubcat)
{
* ppvObject = (ISubcat * ) this ;
AddRef();
return S_OK;
} else
return E_UNEXPECTED;
}
ULONG AddRef()
{
++ m_ref;
cout << " \n object count = " << m_ref;
return m_ref;
}
ULONG Release()
{
-- m_ref;
if (m_ref == 0 )
{
delete this ;
cout << " \n object delete " ;
}
return m_ref;
}
Cmyadd()
{
m_ref = 0 ;
}
int add( int a, int b )
{
return a + b;
}
int sub( int a, int b )
{
return a - b;
}
protected :
private :
ULONG m_ref;
} ;
HRESULT DlllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (rclsid == CLSID_myadd)
{
Cmyadd * pthis = new Cmyadd;
pthis -> QueryInterface(riid,ppv);
return S_OK;
}
return E_UNEXPECTED;
}
在exe中
HANDLE handle = ::LoadLibrary( " .\\dllcom1.dll " );
typedef HRESULT ( * pc)(REFCLSID rclsid, REFIID riid, LPVOID * ppv);
pc p = (pc)::GetProcAddress((HMODULE)handle, " DlllGetClassObject " );
IAdd * padd = NULL;
ISubcat * psub = NULL;
HRESULT hr = p(CLSID_myadd,IID_IAdd,( void ** ) & padd);
if (FAILED(hr))
{
cout << " \n can not get com " ;
return 1 ;
}
hr = padd -> QueryInterface(IID_ISubcat,( void ** ) & psub);
if (FAILED(hr))
{
cout << " \n can not get interface " ;
return 1 ;
}
cout << " \n " << padd -> add( 5 , 5 );
padd -> Release();
cout << " \n " << psub -> sub( 10 , 5 );
psub -> Release();
cout << " \n " ;
static const GUID IID_IUnknown9 =
{ 0x7b55d765 , 0x68a6 , 0x404e , { 0x99 , 0xe2 , 0xca , 0x9a , 0xbc , 0x38 , 0x9a , 0x27 } } ;
// {060CAA56-33E9-4c79-BE5B-0F70A5DDFE15}
static const GUID CLSID_myadd =
{ 0x60caa56 , 0x33e9 , 0x4c79 , { 0xbe , 0x5b , 0xf , 0x70 , 0xa5 , 0xdd , 0xfe , 0x15 } } ;
// {9A03B1AC-8253-4e69-971F-1E88E817F6FA}
static const GUID IID_IAdd =
{ 0x9a03b1ac , 0x8253 , 0x4e69 , { 0x97 , 0x1f , 0x1e , 0x88 , 0xe8 , 0x17 , 0xf6 , 0xfa } } ;
// {F03E6879-75E9-4cbc-A123-2AD92545869C}
static const GUID IID_ISubcat =
{ 0xf03e6879 , 0x75e9 , 0x4cbc , { 0xa1 , 0x23 , 0x2a , 0xd9 , 0x25 , 0x45 , 0x86 , 0x9c } } ;
class IUnknown9
{
public :
virtual HRESULT QueryInterface(REFIID riid, void ** ppvObject) = 0 ;
virtual ULONG AddRef( void ) = 0 ;
virtual ULONG Release( void ) = 0 ;
protected :
private :
} ;
class IAdd: public IUnknown9
{
public :
virtual int add( int , int ) = 0 ;
} ;
class ISubcat: public IUnknown9
{
public :
virtual int sub( int , int ) = 0 ;
protected :
private :
} ;
class Cmyadd: public IAdd, public ISubcat
{
public :
HRESULT QueryInterface(REFIID riid, void ** ppvObject)
{
if (riid == IID_IUnknown9)
{
* ppvObject = (IAdd * ) this ;
AddRef();
return S_OK;
} else if (riid == IID_IAdd)
{
* ppvObject = (IAdd * ) this ;
AddRef();
return S_OK;
} else if (riid == IID_ISubcat)
{
* ppvObject = (ISubcat * ) this ;
AddRef();
return S_OK;
} else
return E_UNEXPECTED;
}
ULONG AddRef()
{
++ m_ref;
cout << " \n object count = " << m_ref;
return m_ref;
}
ULONG Release()
{
-- m_ref;
if (m_ref == 0 )
{
delete this ;
cout << " \n object delete " ;
}
return m_ref;
}
Cmyadd()
{
m_ref = 0 ;
}
int add( int a, int b )
{
return a + b;
}
int sub( int a, int b )
{
return a - b;
}
protected :
private :
ULONG m_ref;
} ;
HRESULT DlllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
if (rclsid == CLSID_myadd)
{
Cmyadd * pthis = new Cmyadd;
pthis -> QueryInterface(riid,ppv);
return S_OK;
}
return E_UNEXPECTED;
}
在exe中
HANDLE handle = ::LoadLibrary( " .\\dllcom1.dll " );
typedef HRESULT ( * pc)(REFCLSID rclsid, REFIID riid, LPVOID * ppv);
pc p = (pc)::GetProcAddress((HMODULE)handle, " DlllGetClassObject " );
IAdd * padd = NULL;
ISubcat * psub = NULL;
HRESULT hr = p(CLSID_myadd,IID_IAdd,( void ** ) & padd);
if (FAILED(hr))
{
cout << " \n can not get com " ;
return 1 ;
}
hr = padd -> QueryInterface(IID_ISubcat,( void ** ) & psub);
if (FAILED(hr))
{
cout << " \n can not get interface " ;
return 1 ;
}
cout << " \n " << padd -> add( 5 , 5 );
padd -> Release();
cout << " \n " << psub -> sub( 10 , 5 );
psub -> Release();
cout << " \n " ;