void main(void) { HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED); if (SUCCEEDED(hr)) { { SI(IUnknown) siUnk; hr = siUnk.Instantiate(CLSID_Goodbye); if (SUCCEEDED(hr)) { SI(IHello) siHello = siUnk; siHello->HelloWorld(); } SI(IPersistStorage) sips; hr = sips.BindToObject(_U("D:\\dbox\\courseware\\ecom\\02_cxx.ppt")); SI(IClassFactory) sicf; hr = sicf.GetClassObject(OLESTR("Excel.Sheet")); SI(IUnknown) p1; hr = sicf->CreateInstance(0, IID_PPV(p1)); hr = OleRun(p1.GetNonAddRefedInterface()); } CoUninitialize(); } }
JNIEXPORT jlong JNICALL Java_com4j_Native_createInstance( JNIEnv* env, jclass __unused__, jstring _progId, jint clsctx, jlong iid1, jlong iid2 ) { MyGUID iid(iid1,iid2); CLSID clsid; HRESULT hr; JString progId(env,_progId); hr = CLSIDFromProgID(progId,&clsid); if(FAILED(hr)) { if(FAILED(CLSIDFromString( const_cast<LPOLESTR>(LPCOLESTR(progId)),&clsid))) { error(env,__FILE__,__LINE__,hr,"Unrecognized CLSID"); return 0; } } void* p; if(clsctx&(CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER)) { IUnknown* pUnk = NULL; hr = CoCreateInstance(clsid,NULL,clsctx,__uuidof(IUnknown),(void**)&pUnk); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"CoCreateInstance failed"); return 0; } hr = OleRun(pUnk); if(FAILED(hr)) { pUnk->Release(); error(env,__FILE__,__LINE__,hr,"OleRun failed"); return 0; } hr = pUnk->QueryInterface(iid,&p); pUnk->Release(); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"QueryInterface failed"); return 0; } } else { // just the plain CoCreateInstance hr = CoCreateInstance(clsid,NULL,clsctx,iid,&p); if(FAILED(hr)) { error(env,__FILE__,__LINE__,hr,"CoCreateInstance failed"); return 0; } } return reinterpret_cast<jlong>(p); }
/* * CSite::ObjectInitialize * (Protected) * * Purpose: * Performs operations necessary after creating an object or * reloading one from storage. * * Parameters: * pchPath The path that we want to load from * * Return Value: * BOOL TRUE if the function succeeded, FALSE otherwise. */ BOOL CSite::ObjectInitialize(LPCTSTR pchPath) { HRESULT hr; DWORD dw; if ( NULL == m_pObj ) { ASSERT(FALSE); return FALSE; } //We need an IOleObject most of the time, so get one here. hr = m_pObj->QueryInterface( IID_IOleObject, (void **)&m_pIOleObject ); if ( FAILED( hr ) ) { ASSERT(FALSE); return FALSE; } //SetClientSite is critical for DocObjects hr = m_pIOleObject->SetClientSite( m_pImpIOleClientSite ); if(FAILED(hr)) { ASSERT(FALSE); } hr = m_pIOleObject->Advise( m_pImpIAdviseSink, &dw ); if(FAILED(hr)) { ASSERT(FALSE); } if ( FAILED( Load( pchPath ) ) ) { ASSERT(FALSE); return FALSE; } // Put the object in the running state hr = OleRun( m_pIOleObject ); if(FAILED(hr)) { ASSERT(FALSE); } return TRUE; }
BOOL tOleHandler::ObjectInitialize(LPUNKNOWN pObj) { HRESULT hr; FORMATETC fe; SETDefFormatEtc(fe, 0, TYMED_NULL); LPFORMATETC pFE = &fe; if (NULL==pObj || NULL==pFE) return FALSE; m_pObj=pObj; m_fe=*pFE; m_fe.ptd=NULL; m_dwState=TENANTSTATE_DEFAULT; m_pIViewObject2=NULL; hr=pObj->QueryInterface(IID_IViewObject2 , (LPVOID*)&m_pIViewObject2); if (FAILED(hr)) return FALSE; /* * Get the MiscStatus bits and check for OLEMISC_ONLYICONIC. * If set, force dwAspect in m_fe to DVASPECT_ICON so we * remember to draw it properly and do extents right. */ m_pIOleObject->GetMiscStatus(m_fe.dwAspect, &m_grfMisc); //CHAPTER24MOD //Run the object if it says to do so if (OLEMISC_ALWAYSRUN & m_grfMisc) OleRun(pObj); //End CHAPTER24MOD //CHAPTER24MOD //Go try initializing control-related things. ControlInitialize(); //End CHAPTER24MOD return TRUE; }
BOOL IXArrayDB::CreateDispatch(REFCLSID clsid, COleException* pError ) { ASSERT(m_lpDispatch == NULL); m_bAutoRelease = TRUE; // good default is to auto-release // create an instance of the object LPUNKNOWN lpUnknown = NULL; HRESULT sc = CreateInstanceLic( this->GetCLSID(), NULL, CLSCTX_ALL | CLSCTX_REMOTE_SERVER, IID_IUnknown, (zPVOID*)&lpUnknown); if (sc == E_INVALIDARG) { // may not support CLSCTX_REMOTE_SERVER, so try without sc = CreateInstanceLic(this->GetCLSID(), NULL, CLSCTX_ALL & ~CLSCTX_REMOTE_SERVER, IID_IUnknown, (zPVOID*)&lpUnknown); } if (FAILED(sc)) goto Failed; // make sure it is running sc = OleRun(lpUnknown); if (FAILED(sc)) goto Failed; // query for IDispatch interface sc = lpUnknown->QueryInterface( IID_IDispatch, (zPVOID*) &m_lpDispatch); if (m_lpDispatch == NULL) goto Failed; lpUnknown->Release(); ASSERT(m_lpDispatch != NULL); return TRUE; Failed: lpUnknown->Release(); if (pError != NULL) pError->m_sc = sc; TRACE1("Warning: CreateDispatch returning scode = %s.\n", AfxGetFullScodeString(sc)); return FALSE; }
BOOL tOleHandler::Activate(LONG iVerb, LPMSG pMSG) { RECT rc, rcH; SIZEL szl; //Can't activate statics. /* if (TENANTTYPE_STATIC==m_tType || NULL==m_pIOleObject) { MessageBeep(0); return FALSE; }*/ RECTFROMRECTL(rc, m_rcl); RectConvertMappings(&rc, NULL, TRUE); XformRectInPixelsToHimetric(NULL, &rc, &rcH); //Get the server running first, then do a SetExtent, then show it OleRun(m_pIOleObject); if (m_fSetExtent) { SETSIZEL(szl, rcH.right-rcH.left, rcH.top-rcH.bottom); m_pIOleObject->SetExtent(m_fe.dwAspect, &szl); m_fSetExtent=FALSE; } //CHAPTER24MOD /* * If we have a pending deactivation, but we're activating * again, clear the pending flag. */ if (OLEIVERB_UIACTIVATE==iVerb || OLEIVERB_INPLACEACTIVATE==iVerb) m_fPendingDeactivate=FALSE; //End CHAPTER24MOD m_pIOleObject->DoVerb(iVerb, pMSG, m_pImpIOleClientSite, 0 , m_hWnd, &rcH); //If object changes, IAdviseSink::OnViewChange will see it. return FALSE; }
BOOL COleDispatchDriver::CreateDispatch(REFCLSID clsid, COleException* pError) { ASSERT(m_lpDispatch == NULL); m_bAutoRelease = TRUE; // good default is to auto-release // create an instance of the object LPUNKNOWN lpUnknown = NULL; SCODE sc = CoCreateInstance(clsid, NULL, CLSCTX_ALL | CLSCTX_REMOTE_SERVER, IID_IUnknown, (LPLP)&lpUnknown); if (sc == E_INVALIDARG) { // may not support CLSCTX_REMOTE_SERVER, so try without sc = CoCreateInstance(clsid, NULL, CLSCTX_ALL & ~CLSCTX_REMOTE_SERVER, IID_IUnknown, (LPLP)&lpUnknown); } if (FAILED(sc)) goto Failed; // make sure it is running sc = OleRun(lpUnknown); if (FAILED(sc)) goto Failed; // query for IDispatch interface m_lpDispatch = QUERYINTERFACE(lpUnknown, IDispatch); if (m_lpDispatch == NULL) goto Failed; lpUnknown->Release(); ASSERT(m_lpDispatch != NULL); return TRUE; Failed: RELEASE(lpUnknown); if (pError != NULL) pError->m_sc = sc; TRACE1("Warning: CreateDispatch returning scode = %s.\n", AfxGetFullScodeString(sc)); return FALSE; }
//--------------------------------------------------------------------------- // wxActiveXContainer::CreateActiveX // // Actually creates the ActiveX container through the FrameSite // and sets up ActiveX events // // TODO: Document this more //--------------------------------------------------------------------------- void wxActiveXContainer::CreateActiveX(REFIID iid, IUnknown* pUnk) { HRESULT hret; hret = m_ActiveX.QueryInterface(iid, pUnk); CHECK_HR(hret); // FrameSite m_frameSite = new FrameSite(m_realparent, this); // oleClientSite hret = m_clientSite.QueryInterface( IID_IOleClientSite, (IDispatch *) m_frameSite); CHECK_HR(hret); // adviseSink wxAutoIAdviseSink adviseSink(IID_IAdviseSink, (IDispatch *) m_frameSite); wxASSERT(adviseSink.Ok()); // Get Dispatch interface hret = m_Dispatch.QueryInterface(IID_IDispatch, m_ActiveX); CHECK_HR(hret); // // SETUP TYPEINFO AND ACTIVEX EVENTS // // get type info via class info wxAutoIProvideClassInfo classInfo(IID_IProvideClassInfo, m_ActiveX); wxASSERT(classInfo.Ok()); // type info wxAutoITypeInfo typeInfo; hret = classInfo->GetClassInfo(typeInfo.GetRef()); CHECK_HR(hret); wxASSERT(typeInfo.Ok()); // TYPEATTR TYPEATTR *ta = NULL; hret = typeInfo->GetTypeAttr(&ta); CHECK_HR(hret); // this should be a TKIND_COCLASS wxASSERT(ta->typekind == TKIND_COCLASS); // iterate contained interfaces for (int i = 0; i < ta->cImplTypes; i++) { HREFTYPE rt = 0; // get dispatch type info handle hret = typeInfo->GetRefTypeOfImplType(i, &rt); if (! SUCCEEDED(hret)) continue; // get dispatch type info interface wxAutoITypeInfo ti; hret = typeInfo->GetRefTypeInfo(rt, ti.GetRef()); if (! ti.Ok()) continue; CHECK_HR(hret); // check if default event sink bool defEventSink = false; int impTypeFlags = 0; typeInfo->GetImplTypeFlags(i, &impTypeFlags); if (impTypeFlags & IMPLTYPEFLAG_FDEFAULT) { if (impTypeFlags & IMPLTYPEFLAG_FSOURCE) { // WXOLE_TRACEOUT("Default Event Sink"); defEventSink = true; if (impTypeFlags & IMPLTYPEFLAG_FDEFAULTVTABLE) { // WXOLE_TRACEOUT("*ERROR* - Default Event Sink is via vTable"); defEventSink = false; wxFAIL_MSG(wxT("Default event sink is in vtable!")); } } } // wxAutoOleInterface<> assumes a ref has already been added // TYPEATTR TYPEATTR *ta = NULL; hret = ti->GetTypeAttr(&ta); CHECK_HR(hret); if (ta->typekind == TKIND_DISPATCH) { // WXOLE_TRACEOUT("GUID = " << GetIIDName(ta->guid).c_str()); if (defEventSink) { wxAutoIConnectionPoint cp; DWORD adviseCookie = 0; wxAutoIConnectionPointContainer cpContainer(IID_IConnectionPointContainer, m_ActiveX); wxASSERT( cpContainer.Ok()); HRESULT hret = cpContainer->FindConnectionPoint(ta->guid, cp.GetRef()); CHECK_HR(hret); IDispatch* disp; m_frameSite->QueryInterface(IID_IDispatch, (void**)&disp); hret = cp->Advise(new wxActiveXEvents(this, ta->guid), &adviseCookie); CHECK_HR(hret); } } ti->ReleaseTypeAttr(ta); } // free typeInfo->ReleaseTypeAttr(ta); // // END // // Get IOleObject interface hret = m_oleObject.QueryInterface(IID_IOleObject, m_ActiveX); CHECK_HR(hret); // get IViewObject Interface hret = m_viewObject.QueryInterface(IID_IViewObject, m_ActiveX); CHECK_HR(hret); // document advise m_docAdviseCookie = 0; hret = m_oleObject->Advise(adviseSink, &m_docAdviseCookie); CHECK_HR(hret); // TODO:Needed? // hret = m_viewObject->SetAdvise(DVASPECT_CONTENT, 0, adviseSink); m_oleObject->SetHostNames(L"wxActiveXContainer", NULL); OleSetContainedObject(m_oleObject, TRUE); OleRun(m_oleObject); // Get IOleInPlaceObject interface hret = m_oleInPlaceObject.QueryInterface( IID_IOleInPlaceObject, m_ActiveX); CHECK_HR(hret); // status DWORD dwMiscStatus; m_oleObject->GetMiscStatus(DVASPECT_CONTENT, &dwMiscStatus); CHECK_HR(hret); // set client site first ? if (dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST) m_oleObject->SetClientSite(m_clientSite); // stream init wxAutoIPersistStreamInit pPersistStreamInit(IID_IPersistStreamInit, m_oleObject); if (pPersistStreamInit.Ok()) { hret = pPersistStreamInit->InitNew(); CHECK_HR(hret); } if (! (dwMiscStatus & OLEMISC_SETCLIENTSITEFIRST)) m_oleObject->SetClientSite(m_clientSite); m_oleObjectHWND = 0; if (m_oleInPlaceObject.Ok()) { hret = m_oleInPlaceObject->GetWindow(&m_oleObjectHWND); if (SUCCEEDED(hret)) ::SetActiveWindow(m_oleObjectHWND); } if (! (dwMiscStatus & OLEMISC_INVISIBLEATRUNTIME)) { RECT posRect; wxCopyRectToRECT(m_realparent->GetClientSize(), posRect); if (posRect.right > 0 && posRect.bottom > 0 && m_oleInPlaceObject.Ok()) { m_oleInPlaceObject->SetObjectRects(&posRect, &posRect); } hret = m_oleObject->DoVerb(OLEIVERB_INPLACEACTIVATE, NULL, m_clientSite, 0, (HWND)m_realparent->GetHWND(), &posRect); CHECK_HR(hret); hret = m_oleObject->DoVerb(OLEIVERB_SHOW, 0, m_clientSite, 0, (HWND)m_realparent->GetHWND(), &posRect); CHECK_HR(hret); } if (! m_oleObjectHWND && m_oleInPlaceObject.Ok()) { hret = m_oleInPlaceObject->GetWindow(&m_oleObjectHWND); CHECK_HR(hret); } if (m_oleObjectHWND) { ::SetActiveWindow(m_oleObjectHWND); ::ShowWindow(m_oleObjectHWND, SW_SHOW); this->AssociateHandle(m_oleObjectHWND); this->Reparent(m_realparent); wxWindow* pWnd = m_realparent; int id = m_realparent->GetId(); pWnd->Connect(id, wxEVT_SIZE, wxSizeEventHandler(wxActiveXContainer::OnSize), 0, this); // this->Connect(GetId(), wxEVT_PAINT, // wxPaintEventHandler(wxActiveXContainer::OnPaint), 0, this); pWnd->Connect(id, wxEVT_SET_FOCUS, wxFocusEventHandler(wxActiveXContainer::OnSetFocus), 0, this); pWnd->Connect(id, wxEVT_KILL_FOCUS, wxFocusEventHandler(wxActiveXContainer::OnKillFocus), 0, this); } }
void tOleHandler::SizeSet(LPSIZEL pszl, BOOL fDevice, BOOL fInformObj) { SIZEL szl; if (!fDevice) { szl=*pszl; m_rcl.right =pszl->cx+m_rcl.left; m_rcl.bottom=pszl->cy+m_rcl.top; } else { RECT rc; SetRect(&rc, (int)pszl->cx, (int)pszl->cy, 0, 0); RectConvertMappings(&rc, NULL, FALSE); m_rcl.right =(long)rc.left+m_rcl.left; m_rcl.bottom=(long)rc.top+m_rcl.top; SETSIZEL(szl, (long)rc.left, (long)rc.top); } //Tell OLE that this object was resized. if (NULL!=m_pIOleObject && fInformObj) { HRESULT hr; BOOL fRun=FALSE; //Convert our LOMETRIC into HIMETRIC by *=10 szl.cx*=10; szl.cy*=-10; //Our size is stored negative. /* * If the MiscStatus bit of OLEMISC_RECOMPOSEONRESIZE * is set, then we need to run the object before calling * SetExtent to make sure it has a real chance to * re-render the object. We have to update and close * the object as well after this happens. */ if (OLEMISC_RECOMPOSEONRESIZE & m_grfMisc) { if (!OleIsRunning(m_pIOleObject)) { OleRun(m_pIOleObject); fRun=TRUE; } } hr=m_pIOleObject->SetExtent(m_fe.dwAspect, &szl); /* * If the object is not running and it does not have * RECOMPOSEONRESIZE, then SetExtent fails. Make * sure that we call SetExtent again (by just calling * SizeSet here again) when we next run the object. */ if (SUCCEEDED(hr)) { m_fSetExtent=FALSE; if (fRun) { m_pIOleObject->Update(); m_pIOleObject->Close(OLECLOSE_SAVEIFDIRTY); } } else { if (OLE_E_NOTRUNNING==GetScode(hr)) m_fSetExtent=TRUE; } } return; }