STDMETHODIMP CTCMarshalByValue::UnmarshalInterface(IStream* pStm, REFIID riid, void** ppv) { // Read the endian indicator DWORD value from the stream CLock lock(this); HRESULT hr = pStm->Read(&m_dwEndian, sizeof(m_dwEndian), NULL); if (SUCCEEDED(hr)) { // Unlock the object lock.Unlock(); // Delegate to the IPersistStream::Load of the controlling unknown IPersistStreamPtr pps; if (SUCCEEDED(hr = GetOuterPersistStream(&pps))) if (SUCCEEDED(hr = pps->Load(pStm))) hr = pps->QueryInterface(riid, ppv); } // Return the last HRESULT return hr; }
///////////////////////////////////////////////////////////////////////////// //功 能:创建或者导入控件项目 ///////////////////////////////////////////////////////////////////////////// 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; }