HRESULT CAdminPageThreadWnd::PostAdminPage(IAGCEvent* pEvent, UINT message) { // Persist the event object into a stream IStreamPtr spStm; HRESULT hr = CreateStreamOnHGlobal(NULL, true, &spStm); if (FAILED(hr)) return hr; IPersistStreamPtr spPersist(pEvent); if (NULL == spPersist) return E_NOINTERFACE; if (FAILED(hr = spPersist->Save(spStm, false))) return hr; // Post the stream to the worker thread window PostMessage(message, reinterpret_cast<WPARAM>(spStm.Detach()), NULL); return S_OK; }
STDMETHODIMP CAdminSessionClass::CreateSession(IAdminSessionHost* pHost, IAdminSession** ppSession) { // pHost must be valid if (!pHost) return E_INVALIDARG; // Create a decoy cookie based on the current FILETIME FILETIME ft; GetSystemTimeAsFileTime(&ft); // Get the IUnknown from the admin session host IUnknownPtr spUnk; HRESULT hr = pHost->GetIdentity(ft.dwLowDateTime, &spUnk); if (hr != (ft.dwLowDateTime & 0x7FFFFFFF)) return E_INVALIDARG; // IUnknown must support IStream IStreamPtr spStm(spUnk); if (NULL == spStm) return E_INVALIDARG; // Create a decoded copy of the specified stream IStreamPtr spStmOut; RETURN_FAILED(CAdminSessionSecure< &CLSID_AdminInterfaces>::CreateDecryptedStream(spStm, &spStmOut)); // Rewind the decoded stream LARGE_INTEGER li = { 0 }; RETURN_FAILED(spStmOut->Seek(li, STREAM_SEEK_SET, NULL)); // Create a version info object for the host IAGCVersionInfoPtr spVersionHost; RETURN_FAILED(spVersionHost.CreateInstance(CLSID_AGCVersionInfo)); // Initialize the version info object from the decoded stream IPersistStreamPtr spPersist(spVersionHost); if (NULL == spPersist) return E_UNEXPECTED; RETURN_FAILED(spPersist->Load(spStmOut)); // Create a version info object for this module IAGCVersionInfoPtr spVersion; RETURN_FAILED(spVersion.CreateInstance(CLSID_AGCVersionInfo)); RETURN_FAILED(spVersion->put_FileName(NULL)); // Compare exactly for several fields of the version info object // CompanyName { CComBSTR bstr, bstrHost; RETURN_FAILED(spVersion->get_CompanyName(&bstr)); RETURN_FAILED(spVersionHost->get_CompanyName(&bstrHost)); if (!IsEqualBSTR(bstr, bstrHost)) return E_INVALIDARG; } // LegalCopyright { CComBSTR bstr, bstrHost; RETURN_FAILED(spVersion->get_LegalCopyright(&bstr)); RETURN_FAILED(spVersionHost->get_LegalCopyright(&bstrHost)); if (!IsEqualBSTR(bstr, bstrHost)) return E_INVALIDARG; } // Now, we can FINALLY create the sesion by calling the base class return CComClassFactory::CreateInstance(NULL, IID_IAdminSession, (void**)ppSession); }
// Get source interface for an object HRESULT SIMPLEAPI GetObjectSourceInterface(IUnknown* pObject, IID* piidEvents, ITypeInfo** ppEventInfo) { // Get dispatch interface CComQIPtr<IDispatch> spDispSource(pObject); if (!spDispSource) return E_NOINTERFACE; // Get type info CComPtr<ITypeInfo> spTypeInfo; spDispSource->GetTypeInfo(0, LOCALE_USER_DEFAULT, &spTypeInfo); if (!spTypeInfo) return E_FAIL; // Get containing type library CComPtr<ITypeLib> spTypeLib; UINT uIndex; RETURNIFFAILED(spTypeInfo->GetContainingTypeLib(&spTypeLib, &uIndex)); // Can object provide source interface? *piidEvents=IID_NULL; CComQIPtr<IProvideClassInfo2> spPCI2(pObject); if (spPCI2) { if (SUCCEEDED(spPCI2->GetGUID(GUIDKIND_DEFAULT_SOURCE_DISP_IID, piidEvents))) { if (SUCCEEDED(spTypeLib->GetTypeInfoOfGuid(*piidEvents, ppEventInfo))) return S_OK; } } // See if object can provide class info CComPtr<ITypeInfo> spClassInfo; CComQIPtr<IProvideClassInfo> spPCI(pObject); if (spPCI) spPCI->GetClassInfo(&spClassInfo); // If still no class info, get object's class ID from IPersist, then get // class info from containing type library if (!spClassInfo) { CComQIPtr<IPersist> spPersist(pObject); if (spPersist) { CLSID clsid; RETURNIFFAILED(spPersist->GetClassID(&clsid)); RETURNIFFAILED(spTypeLib->GetTypeInfoOfGuid(clsid, &spClassInfo)); } } // Object doesn't support enough to find out default source interface if (!spClassInfo) return E_NOINTERFACE; // How many interface? TYPEATTR* pAttr=NULL; RETURNIFFAILED(spClassInfo->GetTypeAttr(&pAttr)); int iInterfaces=pAttr->cImplTypes; spClassInfo->ReleaseTypeAttr(pAttr); // Find default source interface for (int i=0; i<iInterfaces; i++) { // Is it the default source interface? int nType; RETURNIFFAILED(spClassInfo->GetImplTypeFlags(i, &nType)); if (nType == (IMPLTYPEFLAG_FDEFAULT | IMPLTYPEFLAG_FSOURCE)) { // Get type info for the interface HREFTYPE hRefType; RETURNIFFAILED(spClassInfo->GetRefTypeOfImplType(i, &hRefType)); CComPtr<ITypeInfo> spEventInfo; RETURNIFFAILED(spClassInfo->GetRefTypeInfo(hRefType, &spEventInfo)); // Get interface GUID TYPEATTR* pAttr; RETURNIFFAILED(spEventInfo->GetTypeAttr(&pAttr)); *piidEvents=pAttr->guid; spEventInfo->ReleaseTypeAttr(pAttr); // Done! return spEventInfo.CopyTo(ppEventInfo); } } return E_NOINTERFACE; };