转载请标明是引用于 http://blog.csdn.net/chenyujing1234
例子代码:(编译工具:VS2005)
http://www.rayfile.com/zh-cn/files/4468c85c-7a4e-11e1-8329-0015c55db73d/
// 第一步得初始化COM环境
HRESULT hr = CoInitialize(NULL);
if (FAILED(hr))
{
return -3;
}
// 第二步是获得接口 IDccMan
hr = CoCreateInstance(CLSID_DccMan, NULL, CLSCTX_SERVER, IID_IDccMan, (LPVOID*)&g_pDccMan);
if (FAILED(hr))
{
return -1;
}
// Create new notification object. g_pDanSink = new DanDccSink(hWnd, g_pDccMan); if (g_pDanSink) { // 第三步: 查询IDccManSink的接口 g_pDanSink->QueryInterface(IID_IDccManSink, (void **)&pdms); // 这里调用后会自动调用OnLogActive函数 g_pDccMan->Advise(pdms, &g_Context); }
g_pDanSink->QueryInterface(IID_IDccManSink, (void **)&pdms);,
1、会调用DanDccSink::QueryInterface,
STDMETHODIMP DanDccSink::QueryInterface(REFIID riid, LPVOID * ppvObj) { if (IID_IUnknown == riid || IID_IDccManSink == riid) { *ppvObj = (IDccManSink*)this; } else { *ppvObj = NULL; return E_NOINTERFACE; } AddRef(); return NO_ERROR; }
它返回了(IDccManSink*)this;
// 第三步:这里调用后会自动调用OnLogActive函数
g_pDccMan->Advise(pdms, &g_Context);
1、它会先调用
STDMETHODIMP_(ULONG) DanDccSink::AddRef(THIS) { return (ULONG)InterlockedIncrement(&m_lRef); }
2、然后又调用了一次QueryInterface,不过是返回了空接口.
3、调用调用
// // The connection manager calls OnLogActive to indicate that the connection // between the PC and the device is up and fully operational. // STDMETHODIMP DanDccSink::OnLogActiveC() { return NO_ERROR; }
4、然后又调用了一次QueryInterface,不过是返回了空接口.
5、然后调用
// // When a connection is established, the connection manager calls OnLogIpAddr // to notify you of the IP address of the connected device. // STDMETHODIMP DanDccSink::OnLogIpAddr(DWORD dwIpAddr) { TCHAR szIP[30]; _stprintf_s(szIP, 30, TEXT("OnLogIpAddr: %02d.%02d.%02d.%02d"), (dwIpAddr & 0x000000ff), (dwIpAddr & 0x0000ff00)>>8, (dwIpAddr & 0x00ff0000)>>16, dwIpAddr>>24); //SendMessage(g_hwndStatusBar, SB_SETTEXT, (WPARAM)0, (LPARAM)szIP); return NO_ERROR; }
下午很累了,具体原理下次补充.