connection point event is not fire from com component
Hi,
I have a com component and fire event to an MFC application using
connection point. But the wizard generated helper function in the event
procxy class does not fire event since the the m_vec is empty. I
suspect the ATLadvise has some problem, but the return result is OK.
can anyone tell me the possible cause? I have the following code.
//in component
//appwizard generated code. m_vec is empty
template <class T>
class CProxyIExtractEvent : public IConnectionPointImpl<T,
&IID_IExtractEvent, CComDynamicUnkArray>
{
//Warning this class may be recreated by the wizard.
public:
HRESULT Fire_OnExtractImage()
{
HRESULT ret;
T* pT = static_cast<T*>(this);
int nConnectionIndex;
int nConnections = m_vec.GetSize();
for (nConnectionIndex = 0; nConnectionIndex < nConnections;
nConnectionIndex++)
{
pT->Lock();
CComPtr<IUnknown> sp = m_vec.GetAt(nConnectionIndex);
pT->Unlock();
IExtractEvent* pIExtractEvent =
reinterpret_cast<IExtractEvent*>(sp.p);
if (pIExtractEvent != NULL)
ret = pIExtractEvent->OnExtractImage();
} return ret;
}
};
.......
HRESULT CScribbleExtractor::Extract(HBITMAP* phBmpThumbnail)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
Fire_OnExtractImage( );
}
//client code
class CTstConnect :
public IDispatchImpl<IExtractEvent, &IID_IExtractEvent,
&CLSID_ScribbleExtractor>,
public CComObjectRoot
{
public:
CTstConnect() {}
BEGIN_COM_MAP(CTstConnect)
COM_INTERFACE_ENTRY(IDispatch)
COM_INTERFACE_ENTRY(IExtractEvent)
END_COM_MAP()
//This method will be called when DoTest() is called, if this
object
// has connected to the COM DLL (AtlAdvise)
// HRESULT STDMETHODCALLTYPE OnExtractImage(LONG *pHBmpThumbnail )
HRESULT STDMETHODCALLTYPE OnExtractImage()
{
AfxMessageBox("Hello");
return S_OK;
}
};
.......
BOOL CScribbleDoc::OnSaveDocument(LPCTSTR lpszPathName)
{
// TODO: Add your specialized code here and/or call the base class
HRESULT hRes = CoInitialize( NULL );
ATLASSERT( SUCCEEDED(hRes) );
//Initialize global CComModule
_Module.Init( NULL, AfxGetInstanceHandle() );
//Pointer to the interface defined in the COM DLL
IScribbleExtractor *pM;
//Create an instance of the interface defined in the COM DLL
HRESULT theResult = CoCreateInstance( CLSID_ScribbleExtractor,
NULL,
CLSCTX_INPROC_SERVER,
IID_IScribbleExtractor,
(void**)&pM
);
if ( theResult != S_OK ) {
std::ostringstream os;
_com_error err(theResult);
os << "CoCreateInstance() failed: " << err.ErrorMessage();
}
ATLASSERT(pM != NULL);
//Create the COM object (client) that is to connect to the COM DLL
(server)
CComObject<CTstConnect> *pTstConnect;
//Create an instance of the client object
CComObject<CTstConnect>::CreateInstance(&pTstConnect);
//Variable for the connection point cookie
DWORD dwAdvise = 0;
//AtlAdvise, connect connection point
hRes = AtlAdvise( pM,
pTstConnect->GetUnknown(),
IID_IExtractEvent,
&dwAdvise
);
_ASSERT (SUCCEEDED (hRes));
BOOL result = CDocument::OnSaveDocument(lpszPathName);
AtlUnadvise(pM, IID_IExtractEvent, dwAdvise);
//Finished with the server
pM->Release();
//Uninitialize to match CoInitialize
CoUninitialize();
return result;
}
//.idl file
import "oaidl.idl";
import "ocidl.idl";
[
object,
uuid(9F236845-3E27-44f5-A939-682BABA892C2),
dual,
helpstring("IIconExtractor Interface"),
pointer_default(unique)
]
interface IScribbleExtractor : IDispatch
{
};
[
uuid(673B02F2-97D8-4b1f-9482-088C017E418E),
version(1.0),
helpstring("ConnectDemo 1.0 Type Library")
]
interface IExtractEvent : IDispatch
{
[helpstring("method OnExtractImage")] HRESULT OnExtractImage();
};
[
uuid(7B077FC1-07BF-4184-89FB-9C09AD331380),
version(1.0),
helpstring("ThumbScb 1.0 Type Library")
]
library THUMBSCBLib
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(B3A3E4E4-08BE-4ad2-9333-E8958FA1E00D),
helpstring("IconExtractor Class")
]
coclass ScribbleExtractor
{
[default] interface IScribbleExtractor;
[default, source] interface IExtractEvent;
};
};