Я пытаюсь построить точки подключения в существующем компоненте COM. Я получаю класс из CCmdTarget и вызываю макросы MFC в объявлении и реализации класса, как описано в MSDN. Компонент правильно построен, связан и запущен. Однако, когда я отлаживаю свое клиентское приложение, я вижу следующую проблему:
Клиент находит правильный компонент, находит контейнер точки подключения и соответствующую точку подключения; Однако метод Advise не может установить соединение.
Предполагается, что этот Advise вызывает QueryInterface с IID__IAdHocPresenceEvents.
Не понимаю, почему это вызывает QueryInterface с IID_IMarshal.
Позже это становится причиной не получения события на ClientSink :: OnAdHocPresenceQuery
Итак, пожалуйста, помогите мне решить эту проблему или подскажите, где еще можно найти проблему.
Заранее спасибо,
Hovo
class ClientSink : public _IAdHocPresenceEvents
{
private:
DWORD m_dwRefCount;
public:
ClientSink();
virtual ~CClientSink();
STDMETHODIMP OnAdHocPresenceQuery(int Result)
{
CString strTemp ("OnAdHocPresenceQuery");
AfxMessageBox(strTemp);
return S_OK;
};
HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject)
{
HRESULT hr = S_OK;
if (iid == IID_IUnknown)
{
m_dwRefCount++;
*ppvObject = (IUnknown *)this;
}
else if (iid == IID__IAdHocPresenceEvents)
{
m_dwRefCount++;
*ppvObject = (_IAdHocPresenceEvents *)this;
}
else
{
*ppvObject = NULL;
hr = E_NOINTERFACE;
}
return hr;
}
ULONG STDMETHODCALLTYPE AddRef()
{
m_dwRefCount++;
return m_dwRefCount;
}
ULONG STDMETHODCALLTYPE Release()
{
ULONG l;
l = m_dwRefCount--;
if ( 0 == m_dwRefCount)
{
delete this;
}
return l;
}
};
int SendRequest()
{
CLSID clientCLSID;
if (FAILED(CLSIDFromProgID(A2BSTR("SMC.SMCLink"), &clientCLSID)))
{
clientCLSID = SMC::CLSID_SMCLink;
}
LPUNKNOWN lpUnk;
SMC::ISMCLink* m_pSMCLink = NULL;
if (m_pSMCLink == NULL)
{
if (GetActiveObject(clientCLSID, NULL, &lpUnk) == NOERROR)
{
hr = lpUnk->QueryInterface(SMC::IID_ISMCLink, (LPVOID*)&m_pSMCLink);
lpUnk->Release();
}
}
if(hr != S_OK)
{
return -1;
}
DWORD dwAdvise = 0; //variable,shown here for completeness
IConnectionPoint *pCntPoint = NULL;
IConnectionPointContainer *pConnPtContainer = NULL;
//check if this interface supports connectible objects
hr = m_pSMCLink->QueryInterface(IID_IConnectionPointContainer,(void **)&pConnPtContainer);
if ( !SUCCEEDED(hr) )
{
return hr;
}
// find the specific connection point
hr = pConnPtContainer->FindConnectionPoint(IID__IAdHocPresenceEvents, &pCntPoint);
if ( !SUCCEEDED(hr) )
{
return hr;
}
//we are done with the connection point container interface
pConnPtContainer->Release();
IUnknown *pSinkUnk = NULL;
CSink *pSink = NULL;
pSink = new CSink;
if ( NULL == pSink )
{
return E_FAIL;
}
//Get the pointer to CSink's IUnknown pointer
hr = pSink->QueryInterface (IID_IUnknown,(void **)&pSinkUnk);
if ( !SUCCEEDED(hr) )
{
return hr;
}
// It is assumed that this should call the QueryInterface with IID__IAdHocPresenceEvents
// Do not understand why this calls the QueryInterface with IID_IMarshal
// Later this becomes reason for not getting event at ClientSink::OnAdHocPresenceQuery
hr = pCntPoint->Advise(pSinkUnk, &dwAdvise);
if ( !SUCCEEDED(hr) )
{
return hr;
}
hr = m_pSMCLink->RequestService(BSTR("hov@moco"));
pCntPoint->Unadvise(dwAdvise); //disconnect from server
pCntPoint->Release();
return hr;
}
[
helpstring("Interface to control My Product"),
uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
dual,
oleautomation
]
interface ISMCLink : IDispatch
{
// ...
HRESULT RequestService([in] BSTR user);
// ...
};
// Primary dispatch interface for My Component
[ uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) ]
dispinterface IDispSMCLink
{
interface ISMCLink;
};
[
helpstring("ISMCLink2, Interface to access My Product"),
uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
dual,
oleautomation
]
interface ISMCLink2 : ISMCLink
{
//..
};
[ uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx), version(1.0) ]
library SMC
{
importlib("stdole32.tlb");
importlib("stdole2.tlb");
[
uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx),
helpstring("_IAdHocPresenceEvents Interface")
]
interface _IAdHocPresenceEvents : IUnknown
{
[id(1), helpstring("method OnAdHocPresenceQuery")] HRESULT OnAdHocPresenceQuery(int Result);
};
#include "ISMCLink.idl"
[ uuid(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) ]
coclass SMCLink
{
dispinterface IDispSMCLink;
[default] interface ISMCLink;
interface ISMCLink2;
[default, source] interface _IAdHocPresenceEvents;
};
};
class SMCLink : public CCmdTarget
{
protected:
DECLARE_MESSAGE_MAP()
DECLARE_DISPATCH_MAP()
DECLARE_INTERFACE_MAP()
// Connection point for ISMCLink interface
BEGIN_CONNECTION_PART(SMCLink, AdHocPresenceEvents)
CONNECTION_IID(IID__IAdHocPresenceEvents)
END_CONNECTION_PART(AdHocPresenceEvents)
DECLARE_CONNECTION_MAP()
public:
SMCLink(void);
virtual ~SMCLink(void);
// to be OLE creatable, it must be DYNCREATE and OLECREATE
DECLARE_DYNCREATE(SMCLink)
DECLARE_OLECREATE(SMCLink)
// Generated OLE dispatch map functions
//{{AFX_DISPATCH(SMCLink)
afx_msg HRESULT RequestAdHocPresence(BSTR sipAddr);
//}}AFX_DISPATCH
BEGIN_DUAL_INTERFACE_PART(DualSMCLink, ISMCLink)
//..
STDMETHOD(RequestService)(THIS_ BSTR user);
END_DUAL_INTERFACE_PART(DualSMCLink)
};
//..
BEGIN_MESSAGE_MAP(SMCLink, CCmdTarget)
//{{AFX_MSG_MAP(CAutoProxy)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
IMPLEMENT_DYNCREATE(SMCLink, CCmdTarget)
BEGIN_DISPATCH_MAP(SMCLink, CCmdTarget)
//{{AFX_DISPATCH_MAP(SMCLink)
//..
DISP_FUNCTION(SMCLink, "RequestService", RequestService, VT_ERROR, VTS_BSTR)
//}}AFX_DISPATCH_MAP
END_DISPATCH_MAP()
BEGIN_INTERFACE_MAP(SMCLink, CCmdTarget)
INTERFACE_PART(SMCLink, IID_ISMCLink, DualSMCLink)
INTERFACE_PART(SMCLink, IID_ISMCLink2, DualSMCLink)
INTERFACE_PART(SMCLink, IID_IConnectionPointContainer, ConnPtContainer)
END_INTERFACE_MAP()
BEGIN_CONNECTION_MAP(SMCLink, CCmdTarget)
CONNECTION_PART(SMCLink, IID__IAdHocPresenceEvents, AdHocPresenceEvents)
END_CONNECTION_MAP()
SMCLink::SMCLink(void)
{
// enable this object for OLE automation
EnableAutomation();
// enable this object for connection points
EnableConnections();
}
//..
afx_msg HRESULT SMCLink::RequestService(BSTR sipAddr)
{
HRESULT hr = E_FAIL;
int status = 1;
const CPtrArray* pConnections = m_xAdHocPresenceEvents.GetConnections ();
ASSERT (pConnections != NULL);
int nConnections = pConnections->GetSize ();
if (nConnections) {
for (int i=0; i<nConnections; i++)
{
_IAdHocPresenceEvents* pInterface = (_IAdHocPresenceEvents*) (pConnections->GetAt (i));
ASSERT (pInterface != NULL);
// Outgoing!
hr = pInterface->OnAdHocPresenceQuery (status);
}
}
return hr;
}
// delegate standard IDispatch methods to MFC IDispatch implementation
DELEGATE_DUAL_INTERFACE(SMCLink, DualSMCLink)
STDMETHODIMP SMCLink::XDualSMCLink::RequestService(BSTR user)
{
METHOD_PROLOGUE(SMCLink, DualSMCLink)
return pThis->RequestService(user);
}
//..