/********************************************************** * 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 */
Win32HtmlViewRep( Win32HtmlView *view, HWND hw,int style ):hwnd(hw){ owner=view; site.rep=this; eventsink.rep=this; frame.rep=this; viewstyle=style; emitNavEvent=!!(viewstyle & BBHtmlView::NONAVIGATE); currenturl=new BBString(""); eventurl=new BBString(""); OleCreate( CLSID_WebBrowser,IID_IOleObject,OLERENDER_DRAW,0,&site,&storage,(void**)&oleObject ); OleSetContainedObject( oleObject,TRUE); oleObject->SetHostNames(L"Web Host",L"Web View"); oleObject->QueryInterface(IID_IWebBrowser2,(void**)&iBrowser); oleObject->QueryInterface(IID_IOleInPlaceObject,(void**)&inPlaceObject ); oleObject->QueryInterface(IID_IConnectionPointContainer,(void**)&iConnection); iConnection->FindConnectionPoint(DIID_DWebBrowserEvents, &iConnectionPoint); iConnectionPoint->Advise((LPUNKNOWN)&eventsink, &dwCookie); RECT rect; ::GetClientRect( hwnd,&rect ); oleObject->DoVerb(OLEIVERB_SHOW,NULL,&site,-1,hwnd,&rect); go( "about:blank" ); }
// @pymethod int|PyIConnectionPoint|Advise|Establishes a connection between the connection point object and the client's sink. PyObject *PyIConnectionPoint::Advise(PyObject *self, PyObject *args) { PyObject *obUnk; // @pyparm <o PyIUnknown>|unk||The client's advise sink if ( !PyArg_ParseTuple(args, "O:Advise", &obUnk) ) return NULL; IUnknown *pUnk; if (!PyCom_InterfaceFromPyInstanceOrObject(obUnk, IID_IUnknown, (void **)&pUnk, FALSE)) return NULL; IConnectionPoint *pICP = GetI(self); if ( pICP == NULL ) return NULL; DWORD cookie; PY_INTERFACE_PRECALL; HRESULT hr = pICP->Advise( pUnk, &cookie ); pUnk->Release(); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return SetPythonCOMError(self,hr); // @rdesc The result is the connection point identifier used by <om PyIConnectionPoint::Unadvise> return PyInt_FromLong(cookie); }
void mHpWebClient::AddSink() { TraceFunc("mHpWebClient::AddSink"); IConnectionPointContainer* cpc; IConnectionPoint* cp; if (!browserObject_) { ::MessageBox(0, "browserObject_ is null in AddSink", 0, 0); return; } if (browserObject_->QueryInterface(IID_IConnectionPointContainer, reinterpret_cast<void**>(&cpc)) == S_OK) { if (cpc->FindConnectionPoint(DIID_DWebBrowserEvents2, &cp) == S_OK) { // TODO: Member Var unsigned long cookie = 1; if (! (SUCCEEDED(cp->Advise(static_cast<IDispatch*>(this), &cookie)) ) ) { ::MessageBox(0, "Advise failed", 0, 0); } } else { ::MessageBox(0, "FindConnectionPoint", 0, 0); } } else { ::MessageBox(0, "QueryInterface for IID_IConnectionPointContainer", 0, 0); } }
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(); }
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; }
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(); } }
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; }
int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); IDot11AdHocManager * AdHocManager = NULL; IEnumDot11AdHocNetworks * networks = NULL; HRESULT ans; ULONG cnt; IDot11AdHocNetwork * network [10]; IConnectionPointContainer * pConnectionPointContainer; IConnectionPoint * pConnectionPoint; LPWSTR ssid; cout << "Initialising COM Lib... "; ans = CoInitialize(NULL); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Creating AdHocManager... "; ans = CoCreateInstance(CLSID_Dot11AdHocManager,NULL,CLSCTX_INPROC_SERVER,IID_IDot11AdHocManager,(void**) &AdHocManager); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Casting AdHocManager in ConnectionPointContainer... "; ans = AdHocManager->QueryInterface(IID_IConnectionPointContainer,(void**) &pConnectionPointContainer); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Retrieving connection point for AdHocManagerNotifications... "; ans = pConnectionPointContainer->FindConnectionPoint(IID_IDot11AdHocManagerNotificationSink,&pConnectionPoint); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cManagerSink mSink; DWORD sinkCookie; cout << "Registering for notifications... "; ans = pConnectionPoint->Advise((IUnknown*) &mSink, &sinkCookie); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Getting Network list... "; ans = AdHocManager->GetIEnumDot11AdHocNetworks(NULL, &networks); cout << "OK" << endl << "Extracting Networks... "; ans = networks->Next(10,network,&cnt); printf("Got %d networks\n",cnt); bool found = false; IDot11AdHocNetwork * myNet; for (ULONG i = 0; i < cnt; i++) { network[i]->GetSSID(&ssid); wstring str(ssid); wcout << "\tssid = " << str << endl; if (str == ADHOC_SSID) { found=true; myNet = network[i]; } } if (found) { cout << "Casting NetWork in ConnectionPointContainer... "; ans = myNet->QueryInterface(IID_IConnectionPointContainer,(void**) &pConnectionPointContainer); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Retrieving connection point for NetworkNotifications... "; ans = pConnectionPointContainer->FindConnectionPoint(IID_IDot11AdHocNetworkNotificationSink,&pConnectionPoint); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Registering for notifications... "; ans = pConnectionPoint->Advise((IUnknown*) &sink,&sinkCookie); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; printf("Connecting... "); ans = myNet->Connect(ADHOC_PWD, 0x54, false, false); switch (ans) { case S_OK: printf("OK"); break; case REGDB_E_CLASSNOTREG: printf("REGDB"); break; case CLASS_E_NOAGGREGATION: printf("CLASS"); break; case E_NOINTERFACE: printf("NOINTERFACE"); break; case E_POINTER: printf("POINTER"); break; default : printf("%d",ans); } printf("\n"); } if (!found) { printf("Creating the network... "); ans = AdHocManager->CreateNetwork(ADHOC_SSID, ADHOC_PWD, 0x54, NULL, &securitySettings, NULL, &myNet); switch (ans) { case S_OK: printf("OK"); break; case REGDB_E_CLASSNOTREG: printf("REGDB"); break; case CLASS_E_NOAGGREGATION: printf("CLASS"); break; case E_NOINTERFACE: printf("NOINTERFACE"); break; case E_POINTER: printf("POINTER"); break; default : printf("%d",ans); } printf("\n"); cout << "Casting NetWork in ConnectionPointContainer... "; ans = myNet->QueryInterface(IID_IConnectionPointContainer,(void**) &pConnectionPointContainer); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Retrieving connection point for NetworkNotifications... "; ans = pConnectionPointContainer->FindConnectionPoint(IID_IDot11AdHocNetworkNotificationSink,&pConnectionPoint); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Registering for notifications... "; ans = pConnectionPoint->Advise((IUnknown*) &sink,&sinkCookie); cout << ((SUCCEEDED(ans)) ? "OK" : "KO") << endl; cout << "Committing the network... "; ans = AdHocManager->CommitCreatedNetwork(myNet,false,false); switch (ans) { case S_OK: printf("OK"); break; case REGDB_E_CLASSNOTREG: printf("REGDB"); break; case CLASS_E_NOAGGREGATION: printf("CLASS"); break; case E_NOINTERFACE: printf("NOINTERFACE"); break; case E_POINTER: printf("POINTER"); break; default : printf("%d",ans); } printf("\n"); } return a.exec(); }
bool Win32WebControl::createWebView( const std::function<bool (const std::string &)> &shouldStartLoading, const std::function<void (const std::string &)> &didFinishLoading, const std::function<void (const std::string &)> &didFailLoading, const std::function<void (const std::string &)> &onJsCallback) { bool ret = false; IConnectionPointContainer *container = NULL; do { HWND hwnd = cocos2d::Director::getInstance()->getOpenGLView()->getWin32Window(); _winContainer.Create(hwnd, NULL, NULL, WS_CHILD | WS_VISIBLE); HRESULT hr; hr = _winContainer.CreateControl(L"shell.Explorer.2"); CC_BREAK_IF(FAILED(hr)); hr = _winContainer.QueryControl(__uuidof(IWebBrowser2), (void **)&_webBrowser2); CC_BREAK_IF(FAILED(hr) || _webBrowser2 == NULL); _webBrowser2->put_Silent(VARIANT_TRUE); VARIANT var; VariantInit(&var); var.vt = VT_BSTR; var.bstrVal = SysAllocString(L"about:blank"); hr = _webBrowser2->Navigate2(&var, NULL, NULL, NULL, NULL); SysFreeString(var.bstrVal); VariantClear(&var); CC_BREAK_IF(FAILED(hr)); hr = _webBrowser2->QueryInterface(IID_IConnectionPointContainer, (void **)&container); CC_BREAK_IF(FAILED(hr)); hr = container->FindConnectionPoint(DIID_DWebBrowserEvents2, &_connectionPoint); CC_BREAK_IF(FAILED(hr)); hr = _connectionPoint->Advise(this, &_cookie); CC_BREAK_IF(FAILED(hr)); hr = _webBrowser2->get_Document(&_htmlDoc); CC_BREAK_IF(FAILED(hr)); ret = true; } while (0); if (!ret) { removeWebView(); } if (container != NULL) { container->Release(); container = NULL; } _shouldStartLoading = shouldStartLoading; _didFinishLoading = didFinishLoading; _didFailLoading = didFailLoading; _onJsCallback = onJsCallback; return ret; }
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"; }
// ************************************************************************** // 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); }