描述:
我现在正在做一个com组件(是用ATL写的),里面用到了另一个同事写的ActiveX控件(是用mfc写的),它里面定义了一个事件回调,我该如何编写?我现在的方法是实现了他的事件接口,并用AtlAdvise与他的控件建立了连接,但是我实现的事件接口方法始终得不到调用,为什么?
解决方案1:
//
// Chapter7_Client.cpp
//
#include <windows.h>
// Include ATL
#include <atlbase.h>
CComModule _Module;
#include <atlcom.h>
#include <atlimpl.cpp>
BEGIN_OBJECT_MAP(ObjectMap)
END_OBJECT_MAP()
#include "..\Chapter7_CPServer\Chapter7_CPServer.h"
#include "..\Chapter7_CPServer\Chapter7_CPServer_i.c"
class COMModule
{
public:
COMModule()
{
CoInitialize( 0 );
}
~COMModule()
{
CoUninitialize();
}
};
COMModule gModule;
void DisplayMessage( char* szMsg )
{
MessageBox( 0, szMsg, "Chapter7_Client", MB_OK );
}
void HandleError( char*szMsg, HRESULT hr )
{
char szMessage[128];
sprintf( szMessage, "%s. HR = %x", szMsg, hr );
DisplayMessage( szMessage );
}
class CMathEvents :
public CComObjectRoot,
public _IMathEvents
{
public:
CMathEvents()
{
}
BEGIN_COM_MAP(CMathEvents)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(_IMathEvents)
END_COM_MAP()
// IMathEvents
public:
STDMETHODIMP GetTypeInfoCount(UINT*)
{
return E_NOTIMPL;
}
STDMETHODIMP GetTypeInfo( UINT iTInfo,
LCID lcid,
ITypeInfo **ppTInfo)
{
return E_NOTIMPL;
}
STDMETHODIMP GetIDsOfNames( REFIID riid,
LPOLESTR *rgszNames,
UINT cNames,
LCID lcid,
DISPID *rgDispId)
{
return E_NOTIMPL;
}
STDMETHODIMP Invoke( DISPID dispIdMember,
REFIID riid,
LCID lcid,
WORD wFlags,
DISPPARAMS *pDispParams,
VARIANT *pVarResult,
EXCEPINFO *pExcepInfo,
UINT *puArgErr)
{
switch( dispIdMember )
{
case 0x1:
// Make sure the is just one argument
if ( pDispParams->cArgs != 1 )
return DISP_E_BADPARAMCOUNT;
// We don't support named arguments
if ( pDispParams->cNamedArgs )
return DISP_E_NONAMEDARGS;
// Coerce the argument into a long
HRESULT hr;
VARIANTARG var;
VariantInit( &var );
hr = VariantChangeTypeEx( &var,
&(pDispParams->rgvarg[0]),
lcid, 0, VT_I4 );
if FAILED( hr )
return DISP_E_BADVARTYPE;
ComputationComplete( var.lVal );
break;
default:
&nbs