如何在C#中处理COM中定义的通知
C++写法:
STDMETHODCALLTYPE Advise(
/* [in] */ IUnknown *pUnkSink,
/* [out] */ DWORD *pdwCookie) = 0
C#写法:
我想用接口 IAdviseSink来实现同样的功能?
-------------------------------------------------------------------------------------
附今天才写的C#代码:
Guid CLSID_TCSUser = new Guid("4B39D001-6F7B-11d5-8CA0-00A0245AE65C");
Guid[] iids = { new Guid("4B39D002-6F7B-11d5-8CA0-00A0245AE65C"), new Guid("4B39D005-6F7B-11d5-8CA0-00A0245AE65C") };
MULTI_QI[] amqi = ole32Ex.CoCreateInstanceEx(CLSID_TCSUser,
ClsCtx.All, "127.0.0.1", iids, RpcAuthent.None, RpcAuthor.Default, "", RpcLevel.Connect, RpcImpers.Default, "", "", "",true);
if (amqi != null)
{
//获取COM对象tt
object tt = Marshal.GetObjectForIUnknown(amqi[0].pItf);
ITCSUser iICSUser = (ITCSUser)(tt);
IConnectionPointContainer cpc ;
//获取连接点对象
IntPtr ppv;
iid_cpc = new Guid("B196B284-BAB4-101A-B69C-00AA00341D07");
Marshal.QueryInterface(amqi[0].pItf, ref iid_cpc, out ppv);
cpc = (IConnectionPointContainer)(Marshal.GetObjectForIUnknown( ppv));
IConnectionPoint cpt;
//需要处理的DCOM中定义的事件
Guid TCSUserEvents = new Guid("4B39D004-6F7B-11d5-8CA0-00A0245AE65C");
cpc.FindConnectionPoint(ref TCSUserEvents, out cpt);
eventhandler = new EventHandle();
int cookie = 0;
//写道这里就进行不下去了。
cpt.Advise((object)eventhandler, out cookie);
最后一句在C++中是下面这样写的:
// Advise ITCSUserEvents
IConnectionPointContainer* pCPC = NULL;
hr = m_pUser-> QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
_ASSERT(SUCCEEDED(hr));
hr = pCPC-> FindConnectionPoint(__uuidof(ITCSUserEvents), &m_pITCSUserCP);
_ASSERT(SUCCEEDED(hr));
pCPC-> Release();
m_iUserEventsCookie = 0;
hr = m_pITCSUserCP-> Advise(static_cast <ITCSUserEvents*> (this), &m_iUserEventsCookie );