BOOL COleClientItem::Reload() { // first, close the object Close(); // release any pointers we have to the object RELEASE(m_lpObject); RELEASE(m_lpViewObject); // then reload the object with OleLoad and finish creation process BOOL bResult = FinishCreate(::OleLoad(m_lpStorage, IID_IUnknown, NULL, (LPLP)&m_lpObject)); ASSERT_VALID(this); return bResult; }
BOOL COleClientItem::ConvertTo(REFCLSID clsidNew) { ASSERT_VALID(this); ASSERT(m_lpObject != NULL); // first, close the object Close(); // convert it m_scLast = _AfxOleDoConvert(m_lpStorage, clsidNew); if (FAILED(m_scLast)) return FALSE; // save IOleObject and IViewObject2 pointers LPOLEOBJECT lpObject = m_lpObject; LPVIEWOBJECT2 lpViewObject = m_lpViewObject; DWORD dwConnection = m_dwConnection; // NULL out IOleObject and IViewObject2 cached pointers m_lpObject = NULL; m_lpViewObject = NULL; m_dwConnection = 0; // then load the new object from the new storage BOOL bResult = FinishCreate(::OleLoad(m_lpStorage, IID_IUnknown, NULL, (LPLP)&m_lpObject)); if (bResult) { RELEASE(lpObject); RELEASE(lpViewObject); } else { m_lpObject = lpObject; m_lpViewObject = lpViewObject; m_dwConnection = dwConnection; UpdateItemType(); } ASSERT_VALID(this); return bResult; }
///////////////////////////////////////////////////////////////////////////// //功 能:创建或者导入控件项目 ///////////////////////////////////////////////////////////////////////////// BOOL CCtrlItem::CreateOrLoad(REFCLSID clsid, REFIID iidPersistanceMedium, IUnknown* pPersistanceMedium) { HRESULT hResult; BOOL bSuccess; IPersistStreamInitPtr pPersistStreamInit; IPersistStreamPtr pPersistStream; IPersistStoragePtr pPersistStorage; IStreamPtr pStream; IStoragePtr pStorage; ASSERT(m_lpObject == NULL); ASSERT(m_pDocument != NULL); m_dwItemNumber = GetNewItemNumber(); GetItemStorage(); ASSERT(m_lpStorage != NULL); m_bExtendedControl = TRUE; hResult = CExtendedControl::CreateInstance(clsid, this, NULL, IID_IOleObject, (void**)&m_lpObject); if (FAILED(hResult)) { m_bExtendedControl = FALSE; hResult = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER| CLSCTX_INPROC_HANDLER|CLSCTX_LOCAL_SERVER, IID_IOleObject, (void**)&m_lpObject); if (SUCCEEDED(hResult)) { TRACE("Could not aggregate on the control, so it won't support extended properties.\n"); } } if (m_bExtendedControl) { m_pExtendedControl = m_lpObject; ASSERT(m_pExtendedControl != NULL); } //设置客户站点 if (SUCCEEDED(hResult)) { m_lpObject->GetMiscStatus(DVASPECT_CONTENT, &m_dwMiscStatus); if (m_dwMiscStatus&OLEMISC_SETCLIENTSITEFIRST) { //GetClientSite 得到接口 hResult = m_lpObject->SetClientSite(GetClientSite()); if (FAILED(hResult)) { TRACE("SetClientSite failed.\n"); } } } //初始化或装载对象的状态 if (SUCCEEDED(hResult)) { if( iidPersistanceMedium == IID_NULL ) { pPersistStreamInit = m_lpObject; if (pPersistStreamInit != NULL) { hResult = pPersistStreamInit->InitNew(); if (hResult == E_NOTIMPL) { //没有实现 InitNew 为 Ok。 hResult = S_OK; } } else { pPersistStorage = m_lpObject; if (pPersistStorage != NULL) { hResult = pPersistStorage->InitNew(m_lpStorage); } else { // 假设控件不支持持久性。 hResult = S_OK; } } } else if (iidPersistanceMedium == IID_IStream) { pStream = pPersistanceMedium; ASSERT(pStream != NULL); pPersistStreamInit = m_lpObject; if (pPersistStreamInit != NULL) { hResult = pPersistStreamInit->Load(pStream); } else { pPersistStream = m_lpObject; if (pPersistStream != NULL) { hResult = pPersistStream->Load(pStream); } else { hResult = E_NOINTERFACE; } } } else if (iidPersistanceMedium == IID_IStorage) { pStorage = pPersistanceMedium; ASSERT(pStorage != NULL); pPersistStorage = m_lpObject; if (pPersistStorage != NULL) { hResult = pStorage->CopyTo(0, NULL, NULL, m_lpStorage); if (SUCCEEDED(hResult)) { hResult = pPersistStorage->Load(m_lpStorage); } } else { hResult = E_NOINTERFACE; } } } if (SUCCEEDED(hResult)) { if (!(m_dwMiscStatus&OLEMISC_SETCLIENTSITEFIRST)) { hResult = m_lpObject->SetClientSite(GetClientSite()); if (FAILED(hResult)) { TRACE("SetClientSite failed.\n"); } } } //SetClientSite 必须被使用 bSuccess = FinishCreate(hResult); return bSuccess; }