BOOL WindCppAPIImp::initialize() { if (m_pDWindData != NULL) { return TRUE; } HRESULT hr = E_FAIL; hr = m_pDWindData.CreateInstance(__uuidof(WindDataCOMLib::WindDataCOM), NULL, CLSCTX_ALL); if(!SUCCEEDED(hr)) { return FALSE; } m_pDWindData->enableAsyn(1); IConnectionPointContainer *pCPC; hr = m_pDWindData.QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC); if(!SUCCEEDED(hr)) { return FALSE; } IConnectionPoint *pCP; hr = pCPC->FindConnectionPoint(__uuidof(WindDataCOMLib::_DWindDataCOMEvents), &pCP); if (!SUCCEEDED(hr)) { return FALSE; } pCPC->Release(); IUnknown *m_pSinkUnk; m_pSink = new WindDataComSink(); hr = m_pSink->QueryInterface(IID_IUnknown, (void **)&m_pSinkUnk); if(!SUCCEEDED(hr)) { pCP->Release(); return FALSE; } hr = pCP->Advise(m_pSinkUnk, &m_dwAdvise); if(!SUCCEEDED(hr)) { return FALSE; } pCP->Release(); return TRUE; }
HRESULT CJSExtender::SinkObject(CString strPrefix, IDispatch *pUnk, LCID lcid) { if (!pUnk || strPrefix==_T("")) return E_INVALIDARG; HRESULT hr = S_FALSE; GUID iid = GUID_NULL; if (theApp.m_pEventProxy) theApp.m_pEventProxy->GetConnectionInterface(&iid); else { CComQIPtr<IConnectionPointContainer> pContainer(pUnk); if (pContainer == nullptr) return 0; GUID libid; WORD wMajorVer = 1; WORD wMinorVer = 0; hr = AtlGetObjectSourceInterface(pUnk, &libid, &iid, &wMajorVer, &wMinorVer); if (iid == GUID_NULL) { IConnectionPoint* pConnectionPoint = nullptr; IEnumConnectionPoints* pEnum = nullptr; HRESULT hr = pContainer->EnumConnectionPoints(&pEnum); if (pEnum) { ULONG uFetched; while (S_OK == (pEnum->Next(1, &pConnectionPoint, &uFetched)) && uFetched >= 1) { hr = pConnectionPoint->GetConnectionInterface(&iid); if (hr == S_OK) { pConnectionPoint->Release(); pConnectionPoint = nullptr; break; } pConnectionPoint->Release(); pConnectionPoint = nullptr; } pEnum->Release(); } } } if (iid != GUID_NULL) new CJSProxyObj(this, pUnk, iid, strPrefix); return S_OK; }
/********************************************************** * COperator::RegisterTapiEventInterface * *---------------------------------------* * Description: * Get a unique identifier (m_ulAdvice) for * this connection point. * Return: * S_OK * Failed HRESULTs from QI(), * IConnectionPointContainer::FindConnectionPoint(), * or IConnectionPoint::Advise() ***********************************************************/ HRESULT COperator::RegisterTapiEventInterface() { HRESULT hr = S_OK; IConnectionPointContainer * pCPC; IConnectionPoint * pCP; hr = m_pTapi->QueryInterface( IID_IConnectionPointContainer, (void **)&pCPC ); if ( SUCCEEDED( hr ) ) { hr = pCPC->FindConnectionPoint( IID_ITTAPIEventNotification, &pCP ); pCPC->Release(); } if ( SUCCEEDED( hr ) ) { hr = pCP->Advise( m_pTAPIEventNotification, &m_ulAdvise ); pCP->Release(); } return hr; } /* COperator::RegisterTapiEventInterface */
BOOL ZDrOleClient::InterfaceDisconnect( IUnknown *pObj, REFIID riid, LPDWORD pdwConn ) { HRESULT hr; IConnectionPointContainer *pCPointContainer = 0; IConnectionPoint *pCPoint = 0; if ( pObj == 0 || pdwConn == 0 ) return( FALSE ); if ( *pdwConn == 0 ) return( FALSE ); hr = pObj->QueryInterface( IID_IConnectionPointContainer, (LPVOID *) &pCPointContainer ); if ( FAILED( hr ) ) return( FALSE ); hr = pCPointContainer->FindConnectionPoint( riid, &pCPoint ); if ( SUCCEEDED( hr ) ) { // This is the Point to kill m_pEditorEventSink. We are doing this // through Unadvise hr = pCPoint->Unadvise(*pdwConn); if ( SUCCEEDED( hr ) ) *pdwConn = 0L; pCPoint->Release( ); } pCPointContainer->Release( ); return( SUCCEEDED( hr ) ); } // InterfaceDisconnect
//コネクションポイントへの接続 bool Advise(IUnknown * container, REFIID iid, IUnknown * sink, DWORD * cookie, IConnectionPoint ** pcp = NULL) { IConnectionPointContainer * cpc; IConnectionPoint * cp; HRESULT result; if(pcp == NULL) pcp = &cp; container->QueryInterface(IID_IConnectionPointContainer, (void **)&cpc); if(cpc == 0) return false; cpc->FindConnectionPoint(iid, pcp); cpc->Release(); if(*pcp == 0) return false; if(FAILED(result = (*pcp)->Advise(sink, cookie))) return false; if(pcp == &cp) cp->Release(); return true; }
VOID WindCppAPIImp::release() { if (m_pDWindData != NULL) { cancelAllRequest(); HRESULT hr = E_FAIL; IConnectionPointContainer *pCPC; hr = m_pDWindData.QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC); if(SUCCEEDED(hr)) { IConnectionPoint *pCP; hr = pCPC->FindConnectionPoint(__uuidof(WindDataCOMLib::_DWindDataCOMEvents), &pCP); if (SUCCEEDED(hr)) { hr = pCP->Unadvise(m_dwAdvise); pCP->Release(); } pCPC->Release(); } m_pDWindData.Release(); m_pDWindData = NULL; if (m_pSink != NULL) { delete m_pSink; m_pSink = NULL; } m_dwAdvise = 0; } }
// unadvise the world without decrementing the reference count of the client. //reduces refcount of pworld by 1. void DeleteWorld(IWorld* pWorld, DWORD dwCookie) { IConnectionPoint* pconnpt = NULL; IConnectionPointContainer* pconnptctr = NULL; if (pWorld == NULL) return; HRESULT hr = pWorld->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pconnptctr); if (SUCCEEDED(hr)) { hr = pconnptctr->FindConnectionPoint(IID_IVWObjectSite, &pconnpt); if (SUCCEEDED(hr)) { // HACK: artificially addref ptr IUnknown* p = (IUnknown *)dwCookie; p->AddRef(); pconnpt->Unadvise(dwCookie); pconnpt->Release(); } pconnptctr->Release(); } pWorld->Terminate(); // REVIEW: let caller SAFERELEASE ptr rather than do it in here // SAFERELEASE(pWorld); }
BOOL CXTPReportDataManager::DataBind() { HRESULT hr = E_FAIL; try { if (m_pDataSource == NULL) return FALSE; m_pConnection = NULL; // get connection _variant_t vtConnection; if (SUCCEEDED(hr = m_pDataSource->get_ActiveConnection(&vtConnection)) && vtConnection.pdispVal) { m_pConnection = vtConnection; } // clear report control ClearReportControl(); // create columns if (FAILED(hr = CreateColumns())) return FALSE; // create records if (FAILED(hr = AddReportRecords(m_pDataSource, FALSE))) return FALSE; m_pReportControl->Populate(); // advise events m_pRstEvent = new CRstEvent(this); if (!m_pRstEvent) return FALSE; IUnknown *pUnk = NULL; hr = m_pRstEvent->QueryInterface(__uuidof(IUnknown), (void **) &pUnk); if (FAILED(hr)) return FALSE; IConnectionPointContainer *pCPC = NULL; IConnectionPoint *pCP = NULL; hr = m_pDataSource->QueryInterface(__uuidof(IConnectionPointContainer), (void**)&pCPC); if (FAILED(hr)) return FALSE; hr = pCPC->FindConnectionPoint(__uuidof(XTPREPORTADODB::RecordsetEvents), &pCP); pCPC->Release(); if (FAILED(hr)) return FALSE; hr = pCP->Advise(pUnk, &m_dwRstEvent); pCP->Release(); } catch(_com_error &e) { TRACE_ProviderError(m_pConnection); TRACE_ComError(e); hr = FAILED(hr) ? hr : E_FAIL; } catch(...) { TRACE(_T("Unknown error in DataBind()\n")); hr = FAILED(hr) ? hr : E_FAIL; } return FAILED(hr) ? FALSE : TRUE; }
BOOL ZDrOleClient::InterfaceConnect( IUnknown *pObj, REFIID riid, IUnknown *pIUnknownSink, LPDWORD pdwConn ) { TraceLineS( "ZDrOleClient::InterfaceConnect", "" ); HRESULT hr; IConnectionPointContainer *pCPointContainer = 0; IConnectionPoint *pCPoint = 0; if ( pObj == 0 || pIUnknownSink == 0 || pdwConn == 0 ) return( FALSE ); hr = pObj->QueryInterface( IID_IConnectionPointContainer, (LPVOID *) &pCPointContainer ); if ( FAILED( hr ) ) return( FALSE ); hr = pCPointContainer->FindConnectionPoint( riid, &pCPoint ); if ( SUCCEEDED( hr ) ) { hr = pCPoint->Advise( pIUnknownSink, pdwConn ); if ( hr == E_POINTER ) AfxMessageBox( "Invalid Pointer!" ); else if ( hr == CONNECT_E_ADVISELIMIT ) AfxMessageBox( "No more Connections available!" ); else if ( hr == CONNECT_E_CANNOTCONNECT ) AfxMessageBox( "No Connection!" ); pCPoint->Release( ); } else { if ( hr == E_OUTOFMEMORY ) TraceLineS( "FindConnectionPoint failed: ", "E_OUTOFMEMORY" ); else if ( hr == E_UNEXPECTED ) TraceLineS( "FindConnectionPoint failed: ", "E_UNEXPECTED" ); else if ( hr == E_POINTER ) TraceLineS( "FindConnectionPoint failed: ", "E_POINTER" ); else if ( hr == CONNECT_E_NOCONNECTION ) TraceLineS( "FindConnectionPoint failed: ", "CONNECT_E_NOCONNECTION" ); else if ( hr == S_OK ) TraceLineS( "FindConnectionPoint failed: ", "S_OK" ); else TraceLineX( "FindConnectionPoint failed: ", (zLONG) hr ); } pCPointContainer->Release( ); return( SUCCEEDED( hr ) ); } // InterfaceConnect
static void initEventHandler() { IConnectionPointContainer* icpc; iTunes->QueryInterface(IID_IConnectionPointContainer, (void **)&icpc); IConnectionPoint* icp; icpc->FindConnectionPoint(DIID__IiTunesEvents, &icp); icpc->Release(); DWORD dwAdvise; iTunesEventHandler = new CiTunesEventHandler(); icp->Advise(iTunesEventHandler, &dwAdvise); icp->Release(); }
void WmdmLister::ReallyShutdown() { // Unregister for notifications IConnectionPointContainer* cp_container; thread_->manager()->QueryInterface(IID_IConnectionPointContainer, (void**)&cp_container); IConnectionPoint* cp; cp_container->FindConnectionPoint(IID_IWMDMNotification, &cp); cp->Release(); cp_container->Release(); thread_.reset(); }
void tLuaCOM::releaseConnections() { if(conn_point==NULL) return; conn_point->Release(); IConnectionPointContainer *pcpc; HRESULT hr = pdisp->QueryInterface(IID_IConnectionPointContainer, (void **) &pcpc); if(FAILED(hr)) { return; } IEnumConnectionPoints *pecp; hr = pcpc->EnumConnectionPoints(&pecp); pcpc->Release(); if(FAILED(hr)) { return; } pecp->Reset(); IConnectionPoint *pcp; ULONG fetched = 0; hr = pecp->Next(1, &pcp, &fetched); while(SUCCEEDED(hr) && fetched) { IEnumConnections *pec; hr = pcp->EnumConnections(&pec); if(SUCCEEDED(hr)) { pec->Reset(); CONNECTDATA conn; ULONG conn_fetched = 0; hr = pec->Next(1, &conn, &conn_fetched); while(SUCCEEDED(hr) && conn_fetched) { pcp->Unadvise(conn.dwCookie); hr = pec->Next(1, &conn, &conn_fetched); } pec->Release(); } pcp->Release(); pecp->Next(1, &pcp, &fetched); } pecp->Release(); }
void EnumConnectionPoint(IDispatch *pDisp, LPCSTR comment) { if (!pDisp) return; HRESULT hr = S_OK; USES_CONVERSION; IConnectionPointContainer *spCPC = NULL; IConnectionPoint *spCP = NULL; IEnumConnectionPoints *ppEnum = NULL; IID id; LPOLESTR strGUID = 0; CComBSTR bstrId = NULL; if(comment) cout << comment << _T(" "); cout << _T("ConnectionPoint: ["); hr = pDisp->QueryInterface(IID_IConnectionPointContainer, reinterpret_cast<void**>(&spCPC)); if (SUCCEEDED(hr) && spCPC) { hr = spCPC->EnumConnectionPoints(&ppEnum); while(SUCCEEDED(ppEnum->Next(1, &spCP, NULL)) && spCP) { spCP->GetConnectionInterface(&id); // 注意此处StringFromIID的正确用法, 否则会在Debug状态下出错: user breakpoint called from code 0x7c901230 StringFromIID(id, &strGUID); bstrId = strGUID; CoTaskMemFree(strGUID); cout << (bstrId ? OLE2CT(bstrId) : _T("")) << _T(", "); } } cout << "]" << endl; if(spCP) spCP->Release(); if(ppEnum) ppEnum->Release(); if(spCPC) spCPC->Release(); /* {9bfbbc02-eff1-101a-84ed-00aa00341d07} IPropertyNotifySink {3050f3c4-98b5-11cf-bb82-00aa00bdce0b} HTMLObjectElementEvents {3050f620-98b5-11cf-bb82-00aa00bdce0b} HTMLObjectElementEvents2 {00020400-0000-0000-c000-000000000046} IDispatch {1dc9ca50-06ef-11d2-8415-006008c3fbfc} ITridentEventSink {d27cdb6d-ae6d-11cf-96b8-444553540000} ShockwaveFlashObjects::_IShockwaveFlashEvents */ }
void CIncrementSystemBFDlg::ConnectButton1(IHTMLElement *pButtonElem) { HRESULT hr = 0; IConnectionPointContainer* pCPC = NULL; IConnectionPoint* pCP = NULL; IUnknown* pUnk = NULL; DWORD dwCookie; // Check that this is a connectable object. hr = pButtonElem->QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC); if (hr == S_OK) { //AfxMessageBox("ConnectionPointContainer"); // Find the connection point. IEnumConnectionPoints *ppEnum=NULL; hr=pCPC->EnumConnectionPoints(&ppEnum); // ppEnum->Next() // if( hr == S_OK) { //ppEnum->Next() } hr = pCPC->FindConnectionPoint(DIID_HTMLinputTextElementEvents2/*DIID_HTMLButtonElementEvents2*/, &pCP); // hr = pCPC->FindConnectionPoint(__uuidof(IID_HTMLElementEvents2)/*DIID_HTMLButtonElementEvents2*/, &pCP); if ( SUCCEEDED(hr)) { //AfxMessageBox( "Find connection point "); // Advise the connection point. // pUnk is the IUnknown interface pointer for your event sink //CDHTMLEventSink* pSink = new CDHTMLEventSink;// CMyEventSink;// ; //pSink->SetParent(this); //IUnknown* pUnk = pSink->GetInterface(&IID_IUnknown); CMyEventSink* pSink = new CMyEventSink; hr = pCP->Advise(pSink, &dwCookie); pCP->Release(); } pCPC->Release(); } }
void Win32WebControl::removeWebView() { if (_connectionPoint != NULL) { _connectionPoint->Unadvise(_cookie); _connectionPoint->Release(); _connectionPoint = NULL; } if (_htmlDoc != NULL) { _htmlDoc->Release(); _htmlDoc = NULL; } if (_webBrowser2 != NULL) { _webBrowser2->Release(); _webBrowser2 = NULL; } _winContainer.DestroyWindow(); }
void CXTPReportDataManager::RemoveDataSource() { try { // unadvise events if (m_pRstEvent) { IConnectionPointContainer *pCPC = NULL; IConnectionPoint *pCP = NULL; HRESULT hr = m_pDataSource->QueryInterface(__uuidof(IConnectionPointContainer), (void**)&pCPC); if (SUCCEEDED(hr)) { hr = pCPC->FindConnectionPoint(__uuidof(XTPREPORTADODB::RecordsetEvents), &pCP); pCPC->Release(); if (SUCCEEDED(hr)) { hr = pCP->Unadvise(m_dwRstEvent); pCP->Release(); } } //delete m_pRstEvent; m_pRstEvent->Release(); m_pRstEvent = NULL; } // set data source pointer to NULL m_pDataSource = NULL; } catch(_com_error &e) { TRACE_ProviderError(m_pConnection); TRACE_ComError(e); } catch(...) { TRACE(_T("Unknown error in RemoveDataSource()\n")); } }
//******************************************************************* void COpcC_Client::DoOpcDisconnect() { CWaitCursor wait; HRESULT hr = S_OK; if( opcServer.IsOk() && opcGroup.IsOk() ) { if( dwShutdownConnection ) { IConnectionPointContainer *pCPC = 0; hr = opcServer.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC); if( SUCCEEDED(hr) ) { IConnectionPoint *pCallbackCP = 0; hr = pCPC->FindConnectionPoint(IID_IOPCShutdown, &pCallbackCP); if( SUCCEEDED(hr) ) { hr = pCallbackCP->Unadvise(dwShutdownConnection); pCallbackCP->Release(); } pCPC->Release(); } } if( usingCP ) { // OPC 2.0 ConnectionPoints IConnectionPointContainer *pCPC = 0; hr = opcGroup.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC); if( SUCCEEDED(hr) && dwConnection1 ) // This server supports 2.0 { IConnectionPoint *pCallbackCP = 0; hr = pCPC->FindConnectionPoint(IID_IOPCDataCallback, &pCallbackCP); if( SUCCEEDED(hr) ) { hr = pCallbackCP->Unadvise(dwConnection1); pCallbackCP->Release(); } pCPC->Release(); } } else // call IDataObject::DUnadvise to turn off data notification { DataObject dataObject; HRESULT hr = dataObject.Attach( opcGroup ); if( SUCCEEDED(hr) ) { if( dwConnection1 ) hr = dataObject.DUnadvise(dwConnection1); if( dwConnection2 ) hr = dataObject.DUnadvise(dwConnection2); dataObject.Detach(); } } opcServer.RemoveGroup(groupHandle, FALSE); } opcGroup.Detach(); opcServer.Detach(); Sleep( 100 ); // now that the group is released and unadvised, no more data will // be sent from the server. It is safe to delete the items while( !items.IsEmpty() ) delete items.RemoveTail(); }
// ************************************************************************** // Connect () // // Description: // Connect to OPC Server. OPC Server's ProgID and machine name must have // been previously specified. // // Parameters: // none // // Returns: // bool - true if success. // ************************************************************************** bool CKServer::Connect () { // Program ID of OPC Server should have been defined by now: ASSERT (!m_strProgID.IsEmpty ()); // Assume we are not connecting to KEPServerEx: m_bfFlags.bIsKepServerEx = false; // Perform any necessary cleanup from a previous connection: Disconnect (); /* m_server.EnumClassesOfCategories( 1, &catids 0, NULL, out enumerator); */ // Obtain the Class ID of the OPC Server. (GetCLSID() will need the // OPC Server's Program ID to succeed. That's why we checked it above.) CLSID clsid; bool ret = SUCCEEDED (GetCLSID (clsid)); // 2006 debug //if (ret) if (true) { HRESULT hr; // Re-intialize Multi-Query Interface: for (int i = 0; i < sizeof (m_arrMultiQI) / sizeof (MULTI_QI); i++) { m_arrMultiQI [i].pItf = NULL; m_arrMultiQI [i].hr = 0; } // Load up the Interface ID's we hope to get pointers for when we // call CoCreateInstanceEx(): m_arrMultiQI [MQI_IOPCSERVER].pIID = &IID_IOPCServer; m_arrMultiQI [MQI_IOPCCOMMON].pIID = &IID_IOPCCommon; m_arrMultiQI [MQI_IOPCCONNPT].pIID = &IID_IConnectionPointContainer; m_arrMultiQI [MQI_IOPCITEMPROP].pIID = &IID_IOPCItemProperties; m_arrMultiQI [MQI_IOPCBROWSE].pIID = &IID_IOPCBrowseServerAddressSpace; m_arrMultiQI [MQI_IOPCPUBLIC].pIID = &IID_IOPCServerPublicGroups; m_arrMultiQI [MQI_IOPCPERSIST].pIID = &IID_IPersistFile; // Connect to the OPC Server and query all possible interfaces: if (m_strRemoteMachine.IsEmpty ()) { // Since m_strRemoteMachine is empty, we will try to instantiate // the OPC Server on our local machine. // CoCreateInstanceEx will launch the OPC Server if necessary, and // call its QueryInterface for us (bumping its reference count): hr = CoCreateInstanceEx ( clsid, // CLSID NULL, // No aggregation CLSCTX_SERVER, // connect to local, inproc and remote servers NULL, // remote machine name sizeof (m_arrMultiQI) / sizeof (MULTI_QI), // number of IIDS to query m_arrMultiQI); // array of IID pointers to query } else { // Since m_strRemoteMachine is not empty, we will assume it contains // a valid remote machine name. We will try to instantiate the OPC // Server object on the machine with that name. // First we need to initialize a server info structure: COSERVERINFO tCoServerInfo; ZeroMemory (&tCoServerInfo, sizeof (tCoServerInfo)); // Allocate memory for the machine name string: int nSize = m_strRemoteMachine.GetLength () * sizeof (WCHAR); tCoServerInfo.pwszName = new WCHAR [nSize]; // Check validity of pointer. If it's bad, there's no point in continuing: if (!tCoServerInfo.pwszName) { ASSERT (FALSE); return (false); } // Copy the machine name string into the server info structure: #ifdef _UNICODE // For Unicode builds, the contents of m_strRemoteMachine will // already be in wide character format, as demanded by COM, so // copy it as is. lstrcpyn (tCoServerInfo.pwszName, m_strRemoteMachine, nSize); #else // For ANSI builds, the contents of m_strRemoteMachine will not // be in wide character format, as demanded by COM, so we need // to reformat: mbstowcs (tCoServerInfo.pwszName, m_strRemoteMachine, nSize); #endif//_UNICODE // CoCreateInstanceEx will launch the OPC Server if necessary, and // call its QueryInterface for us (bumping its reference count): hr = CoCreateInstanceEx ( clsid, // CLSID NULL, // No aggregation // CLSCTX_SERVER, // connect to local, inproc and remote servers CLSCTX_REMOTE_SERVER, // lyy &tCoServerInfo, // remote machine name sizeof (m_arrMultiQI) / sizeof (MULTI_QI), // number of IIDS to query m_arrMultiQI); // array of IID pointers to query // COM requires us to free memory allocated for [out] and [in/out] // arguments (i.e. name string). delete [] tCoServerInfo.pwszName; } // If CoCreateInstanceEx succeeded, we can check the returned // interface pointers and save them as member variables: if (SUCCEEDED (hr)) { TRACE (_T("OTC: Initializing server %s interfaces.\r\n"), GetProgID ()); // Check IOPCServer interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCSERVER].hr)) { m_pIServer = (IOPCServer *)m_arrMultiQI [MQI_IOPCSERVER].pItf; if (m_pIServer == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCSERVER].pItf != NULL) { // Warning failure but pointer not set to null ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IOPCServer (%08X).\r\n"), m_arrMultiQI [MQI_IOPCSERVER].hr); } // Check IOPCCommon interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCCOMMON].hr)) { m_pICommon = (IOPCCommon *)m_arrMultiQI [MQI_IOPCCOMMON].pItf; if (m_pICommon == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCCOMMON].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IOPCCommon (%08X).\r\n"), m_arrMultiQI [MQI_IOPCCOMMON].hr); } // Check IConnectionPointContainer interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCCONNPT].hr)) { m_pIConnPtContainer = (IConnectionPointContainer *)m_arrMultiQI [MQI_IOPCCONNPT].pItf; if (m_pIConnPtContainer == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCCONNPT].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IConnectionPoint (%08X).\r\n"), m_arrMultiQI [MQI_IOPCCONNPT].hr); } // Check IOPCItemProperties interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCITEMPROP].hr)) { m_pIItemProps = (IOPCItemProperties *)m_arrMultiQI [MQI_IOPCITEMPROP].pItf; if (m_pIItemProps == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCITEMPROP].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IOPCItemProperties (%08X).\r\n"), m_arrMultiQI [MQI_IOPCITEMPROP].hr); } // Check IOPCBrowseServerAddressSpace interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCBROWSE].hr)) { m_pIBrowse = (IOPCBrowseServerAddressSpace *)m_arrMultiQI [MQI_IOPCBROWSE].pItf; if (m_pIBrowse == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCBROWSE].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IOPCBrowseServerAddressSpace (%08X).\r\n"), m_arrMultiQI [MQI_IOPCBROWSE].hr); } // Check IOPCServerPublicGroups interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCPUBLIC].hr)) { m_pIPublicGroups = (IOPCServerPublicGroups *)m_arrMultiQI [MQI_IOPCPUBLIC].pItf; if (m_pIPublicGroups == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCPUBLIC].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IOPCServerPublicGroups (%08X).\r\n"), m_arrMultiQI [MQI_IOPCPUBLIC].hr); } // Check IPersistFile interface pointer: if (SUCCEEDED (m_arrMultiQI [MQI_IOPCPERSIST].hr)) { m_pIPersistFile = (IPersistFile *)m_arrMultiQI [MQI_IOPCPERSIST].pItf; if (m_pIPersistFile == NULL) { // Warning success but no valid pointer: ASSERT (FALSE); } } else { if (m_arrMultiQI [MQI_IOPCPERSIST].pItf != NULL) { // Warning failure but pointer not set to null: ASSERT (FALSE); } TRACE (_T("OTC: Failed to query IPersistsFile (%08X).\r\n"), m_arrMultiQI [MQI_IOPCPERSIST].hr); } // Check IConnectionPointContainer interface pointer: if (m_pIConnPtContainer != NULL) { // If the server supports the shutdown interface, provide a sink // to the server. // Get connection point pointer: IConnectionPoint *pCP = NULL; hr = m_pIConnPtContainer->FindConnectionPoint (IID_IOPCShutdown, &pCP); // If we got the connection point, instantiate our shutdown sink: if (SUCCEEDED (hr)) { try { // Instantiate the shutdown sink and add us to its reference count: m_pIShutdownSink = new IKShutdownSink (this); m_pIShutdownSink->AddRef (); // Give the connection point a pointer to our shutdown sink: // (m_dwCookieShutdownSink is a returned token that uniquely // identifies this connection.) hr = pCP->Advise (m_pIShutdownSink, &m_dwCookieShutdownSink); // We are done with the connection point, so release our reference: pCP->Release (); } catch (...) { // If we find ourselves here, either "new" failed or pCP is bad. ASSERT (FALSE); hr = E_FAIL; } } } // We will base our success on the validity of the IOPCServer interface // pointer. If it is invalid, then we won't be able do do anyting: m_bConnected = (m_pIServer != NULL); // Log success or failure: if (m_bConnected) LogMsg (IDS_SERVER_CONNECT_SUCCESS, GetProgID ()); else LogMsg (IDS_SERVER_REQUIRED_IID_UNSUPPORTED, GetProgID (), hr); } // CoCreateInstanceEx failed: else { // log failure LogMsg (IDS_SERVER_CONNECT_FAILURE, GetProgID (), hr); } } // Failed to get Class ID: else { // Log failure: LogMsg (IDS_SERVER_UNABLE_TO_GET_CLSID, GetProgID ()); } // Return connected state: return (m_bConnected); }
// ************************************************************************** // Disconnect () // // Description: // Called to disconnect from OPC server. // // Parameters: // none // // Returns: // void // ************************************************************************** void CKServer::Disconnect () { // Log success if we were truly connected: if (m_bConnected == true) LogMsg (IDS_SERVER_DISCONNECTED, GetProgID ()); // Reset member variable: m_bConnected = false; // Release all of our server interface references: if (m_pIServer) { m_pIServer->Release (); m_pIServer = NULL; } if (m_pICommon) { m_pICommon->Release (); m_pICommon = NULL; } if (m_pIConnPtContainer) { // Unadvise shutdown notifications: if (m_dwCookieShutdownSink != 0) { HRESULT hr = E_FAIL; IConnectionPoint *pCP = NULL; hr = m_pIConnPtContainer->FindConnectionPoint (IID_IOPCShutdown, &pCP); if (SUCCEEDED (hr)) { hr = pCP->Unadvise (m_dwCookieShutdownSink); pCP->Release (); } if (FAILED (hr)) { TRACE (_T("OTC: CKServer::Disconnect () - failed to unadvise shutdown notifications\r\n")); } m_dwCookieShutdownSink = 0; } if (m_pIShutdownSink != NULL) { m_pIShutdownSink->Release (); m_pIShutdownSink = NULL; } m_pIConnPtContainer->Release (); m_pIConnPtContainer = NULL; } if (m_pIItemProps) { m_pIItemProps->Release (); m_pIItemProps = NULL; } if (m_pIBrowse) { m_pIBrowse->Release (); m_pIBrowse = NULL; } if (m_pIPublicGroups) { m_pIPublicGroups->Release (); m_pIPublicGroups = NULL; } if (m_pIPersistFile) { m_pIPersistFile->Release (); m_pIPersistFile = NULL; } }
void WmdmLister::Init() { qLog(Debug) << "Starting"; thread_.reset(new WmdmThread); if (!thread_->manager()) return; // Register for notifications qLog(Debug) << "Obtaining CP container"; IConnectionPointContainer* cp_container = NULL; thread_->manager()->QueryInterface(IID_IConnectionPointContainer, (void**)&cp_container); qLog(Debug) << "Obtaining CP"; IConnectionPoint* cp = NULL; cp_container->FindConnectionPoint(IID_IWMDMNotification, &cp); qLog(Debug) << "Registering for notifications"; cp->Advise(this, ¬ification_cookie_); cp->Release(); cp_container->Release(); // Fetch the initial list of devices qLog(Debug) << "Fetching device list"; IWMDMEnumDevice* device_it = NULL; if (thread_->manager()->EnumDevices2(&device_it)) { qLog(Warning) << "Error querying WMDM devices"; return; } // Iterate through the devices QMap<QString, DeviceInfo> devices; forever { IWMDMDevice* device = NULL; IWMDMDevice2* device2 = NULL; ULONG fetched = 0; if (device_it->Next(1, &device, &fetched) || fetched != 1) break; qLog(Debug) << "Querying device"; if (device->QueryInterface(IID_IWMDMDevice2, (void**)&device2)) { qLog(Warning) << "Error getting IWMDMDevice2 from device"; device->Release(); continue; } device->Release(); DeviceInfo info = ReadDeviceInfo(device2); if (info.is_suitable_) devices[info.unique_id()] = info; else device2->Release(); } device_it->Release(); // Update the internal cache qLog(Debug) << "Updating device cache"; { QMutexLocker l(&mutex_); devices_ = devices; } // Notify about the changes foreach (const QString& id, devices.keys()) { emit DeviceAdded(id); } qLog(Debug) << "Startup complete"; }
HRESULT UnRegisterCallBack() { LogMessage("UnRegisterCallBack: started"); HRESULT hr = S_OK; // // get connection point container on the tapi object // IConnectionPointContainer *pCPContainer = NULL; hr = g_pTapi->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPContainer); if (FAILED(hr)) { LogError("UnRegisterCallBack: " "Failed to get IConnectionPointContainer on TAPI"); return hr; } // // find connection point for our interface // IConnectionPoint *pConnectionPoint = NULL; hr = pCPContainer->FindConnectionPoint(IID_ITTAPIEventNotification, &pConnectionPoint); pCPContainer->Release(); pCPContainer = NULL; if (FAILED(hr)) { LogError("RegisterCallback: Failed to get a connection point for " "ITTAPIEventNotification"); return hr; } // // unregister the callback // hr = pConnectionPoint->Unadvise(g_nTAPINotificationCookie); pConnectionPoint->Release(); pConnectionPoint = NULL; g_nTAPINotificationCookie = 0; if (FAILED(hr)) { LogError("UnRegisterCallBack: Unadvise failed"); } else { LogMessage("UnRegisterCallBack: succeeded"); } return hr; }
HRESULT RegisterCallback() { LogMessage("RegisterCallback: started"); HRESULT hr = S_OK; // // get connection point container on the tapi object // IConnectionPointContainer *pCPContainer = NULL; hr = g_pTapi->QueryInterface( IID_IConnectionPointContainer, (void **)&pCPContainer ); if (FAILED(hr)) { LogError("RegisterCallback: " "Failed to get IConnectionPointContainer on TAPI"); return hr; } // // find connection point for our interface // IConnectionPoint *pCP = NULL; hr = pCPContainer->FindConnectionPoint(IID_ITTAPIEventNotification, &pCP); pCPContainer->Release(); pCPContainer = NULL; if (FAILED(hr)) { LogError("RegisterCallback: " "Failed to get a connecion point for ITTAPIEventNotification"); return hr; } // // create the callback object and register it with TAPI. // for simplicity, the callback in this sample is not a // full-fledged COM object. So create is with new. // ITTAPIEventNotification *pTAPIEventNotification = new CTAPIEventNotification; if (NULL == pTAPIEventNotification) { LogError("RegisterCallback: Failed to create a tapi event notification object."); pCP->Release(); pCP = NULL; return E_OUTOFMEMORY; } // // will use the cookie later to register for events from addresses // and to unregister the callback when we no longer needs the events // hr = pCP->Advise(pTAPIEventNotification, &g_nTAPINotificationCookie); pCP->Release(); pCP = NULL; // // whether Advise failed or succeeded, we no longer need a reference to // the callback // pTAPIEventNotification->Release(); pTAPIEventNotification = NULL; if (FAILED(hr)) { g_nTAPINotificationCookie = 0; LogError("RegisterCallback: Failed to Advise"); } LogMessage("RegisterCallback: completed"); return hr; }
STDMETHODIMP COPCGroupImpl::put_IsSubscribed(VARIANT_BOOL IsSubscribed) { if( !m_pOPCGroup ) return E_FAIL; HRESULT hr=S_OK; if( IsSubscribed == m_subscribed ) return hr; if( IsSubscribed ) { if( m_usingCP ) { IConnectionPointContainer *pCPC = 0; hr = m_pOPCGroup->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pCPC); if( SUCCEEDED(hr) ) { IConnectionPoint *pCallbackCP = 0; hr = pCPC->FindConnectionPoint(IID_IOPCDataCallback, &pCallbackCP); pCPC->Release(); if( FAILED(hr) ) { return E_FAIL; } IUnknown* pUnk = NULL; ((COPCGroup*)this)->QueryInterface( IID_IUnknown, (LPVOID*)&pUnk); hr = pCallbackCP->Advise(pUnk, &m_DataConnection); pCallbackCP->Release(); pUnk->Release(); if( FAILED(hr) ) { return E_FAIL; } } } else { if( !m_pIDataObject ) { m_pIDataObject = m_pOPCGroup; // get IDataObject Interface if( !m_pIDataObject ) return E_FAIL; } IAdviseSink *pAdviseSink = NULL; hr = ((COPCGroup*)this)->QueryInterface(IID_IAdviseSink, (LPVOID *)&pAdviseSink); if (FAILED(hr)) { return E_FAIL; } FORMATETC formatEtc ; formatEtc.cfFormat = OPCSTMFORMATDATATIME; formatEtc.tymed = TYMED_HGLOBAL; formatEtc.ptd = NULL; formatEtc.dwAspect = DVASPECT_CONTENT; formatEtc.lindex = -1; hr = m_pIDataObject->DAdvise(&formatEtc, (DWORD)ADVF_PRIMEFIRST, pAdviseSink, &m_DataConnection); if( SUCCEEDED(hr) ) // also advise write complete { formatEtc.cfFormat = OPCSTMFORMATWRITECOMPLETE; hr = m_pIDataObject->DAdvise(&formatEtc, (DWORD)ADVF_PRIMEFIRST, pAdviseSink, &m_WriteConnection); } // DAdvise will hold its own reference pAdviseSink->Release(); } m_subscribed = VARIANT_TRUE; } else // Unsubscribing { if( m_usingCP ) { // OPC 2.0 ConnectionPoints IConnectionPointContainer *pCPC = 0; hr = m_pOPCGroup->QueryInterface(IID_IConnectionPointContainer, (LPVOID*)&pCPC); if( SUCCEEDED(hr) ) { IConnectionPoint *pCallbackCP = 0; hr = pCPC->FindConnectionPoint(IID_IOPCDataCallback, &pCallbackCP); if( SUCCEEDED(hr) ) { hr = pCallbackCP->Unadvise(m_DataConnection); pCallbackCP->Release(); } pCPC->Release(); } } else { if( m_pIDataObject.p ) // if a valid interface pointer { hr = m_pIDataObject->DUnadvise(m_DataConnection); if( SUCCEEDED(hr) ) // also unadvise write complete hr = m_pIDataObject->DUnadvise(m_WriteConnection); m_pIDataObject.Release(); } } m_subscribed = VARIANT_FALSE; } return hr; }