½üÀ´Ñ§Ï° com¼¼Êõ,¾õµÃ¿ÉÁ¬½Ó¶ÔÏóʵÏÖÆðÀ´ºÜÄÑ.¾¹ý°Ý¶ÁÅËÀÏʦµÄÊé¼®,ÓÃATLд³öÁËÒ»¸ö¼òµ¥Àý×Ó:
Ò» com×é¼þ·½:
ÉùÃ÷ҪʵÏֵĿÉÁ¬½Ó¶ÔÏó
[
uuid(87EA0963-F479-4ABC-95DD-DF0F081B73D2),
helpstring("LJ Class")
]
interface _IWLEvents:IUnknown
{
[ helpstring("method tongku")] HRESULT JINGJING();
};
ÔÚÀàÖÐÉùÃ÷
coclass LJ
{
[default] interface ILJ;
[source] interface _IWLEvents;
};
ÏÂÒ»²½ÓÃATLÉú³É¿ÉÁ¬½Ó¶ÔÏó,ATL×Ô¶¯Éú³ÉÁË º¯ÊýJINGJING µÄ¼¤»î·½Ê½,ÈçÏÂËùʾ:
class CProxy_IWLEvents : public IConnectionPointImpl<T, &IID__IWLEvents, CComDynamicUnkArray>
{
//Warning this class may be recreated by the wizard.
public:
HRESULT Fire_JINGJING()
{
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();
_IWLEvents* p_IWLEvents = reinterpret_cast<_IWLEvents*>(sp.p);
if (p_IWLEvents != NULL)
ret = p_IWLEvents->JINGJING(); //µ÷Óÿͻ§¶Ë³ÌÐò,ºÍ¿Í»§¶Ë·¢Éú¹ØÁª
} return ret;
}
};
¶þ ¿Í»§¶Ë
ÔÚ¿Í»§¶ËÓÃÒ»¸ö¶Ô»°¿ò³ÌÐòʵÏÖ,Ê×ÏÈҪʵÏÖ½Ó¿ÚIWLEventsµÄÀà
class CIWL:public _IWLEvents
{
STDMETHODIMP QueryInterface (REFIID iid,LPVOID *pp)
{
*pp =this;
return S_OK;
}
STDMETHODIMP_(ULONG) AddRef()
{
return 2;
}
STDMETHODIMP_(ULONG) Release ()
{
return 1;
}
STDMETHODIMP JINGJING()
{
AfxMessageBox(“·µ»Øµ½¿Í»§¶Ë“);
return 1;
}
};
ÓÃÒ»¸öbuttonʼþʵÏÖµ÷Óùý³Ì:
void CShowDlg::OnButton1()
{
CoInitialize(NULL);
ILJ * p;
IConnectionPointContainer * pContainer=NULL;
IConnectionPoint *pPoint = NULL;
HRESULT hr = CoCreateInstance(CLSID_LJ,NULL,CLSCTX_INPROC_SERVER , IID_ILJ,(void**)&p );
if(SUCCEEDED(hr))
{
p->QueryInterface(IID_IConnectionPointContainer,(void**)&pContainer);
if(SUCCEEDED(hr))
{
hr = pContainer->FindConnectionPoint(IID__IWLEvents, &pPoint);
if(SUCCEEDED(hr))
{
DWORD dwCookie;
CIWL * m_pDuckInt = new CIWL ;
hr =pPoint->Advise(m_pDuckInt,&dwCookie);
if(SUCCEEDED(hr))
{
p-> Fire() ;
pPoint->Release();
}
pPoint->Unadvise(dwCookie);
}
CoUninitialize();
}
±¾ÈËÊÇÐÂÊÖ,Ï£Íû¸÷λÀÏÄñÃǶà¶àÖ¸½Ì°¢
±¾ÎĵØÖ·£ºhttp://com.8s8s.com/it/it642.htm