HRESULT CoFoldersMonitor::LoadTypeInfo(ITypeInfo **ppTypeInfo, const CLSID &libId, const CLSID &iid, LCID lcid) { HRESULT hr; LPTYPELIB ptlib = nullptr; LPTYPEINFO ptinfo = nullptr; *ppTypeInfo = nullptr; // Load type library. hr = ::LoadRegTypeLib(libId, g_majorVer, g_minorVer, lcid, &ptlib); if (FAILED(hr)) { com::Trace(L"[%s] : LoadRegTypeLib failed ! (%d)", hr); return hr; } // Get the information for the interface of the object. hr = ptlib->GetTypeInfoOfGuid(iid, &ptinfo); if (FAILED(hr)) { com::Trace(L"[%s] : GetTypeInfoOfGuid failed ! (%d)", hr); ptlib->Release(); return hr; } ptlib->Release(); *ppTypeInfo = ptinfo; return NOERROR; }
/****************************************************************************** * LoadTypeInfo -- Gets the type information of an object's interface from the * type library. Returns S_OK if successful. ******************************************************************************/ STDMETHODIMP RTDServer::LoadTypeInfo(ITypeInfo** pptinfo, REFCLSID clsid, LCID lcid) { HRESULT hr; LPTYPELIB ptlib = NULL; LPTYPEINFO ptinfo = NULL; *pptinfo = NULL; // First try to load the type info from a registered type library hr = LoadRegTypeLib(LIBID_RTDServerLib, 1, 0, lcid, &ptlib); if (FAILED(hr)) { //can't get the type information return hr; } // Get type information for interface of the object. hr = ptlib->GetTypeInfoOfGuid(clsid, &ptinfo); if (FAILED(hr)) { ptlib->Release(); return hr; } ptlib->Release(); *pptinfo = ptinfo; return S_OK; }
HRESULT CCmdTarget::GetTypeInfoOfGuid(LCID lcid, REFGUID guid, LPTYPEINFO* ppTypeInfo) { USES_CONVERSION; AfxLockGlobals(CRIT_TYPELIBCACHE); HRESULT hr = TYPE_E_CANTLOADLIBRARY; CTypeLibCache* pTypeLibCache = GetTypeLibCache(); LPTYPELIB pTypeLib = NULL; // If type info is already cached, just return it. if (pTypeLibCache->LookupTypeInfo(lcid, guid, ppTypeInfo)) { hr = S_OK; } else { // If type library isn't already cached, try to locate it. if (!pTypeLibCache->Lookup(lcid, &pTypeLib)) { // First, try getting the subclass to load the type library // (normally this goes through LoadRegTypeLib). if (FAILED(GetTypeLib(lcid, &pTypeLib))) { AFX_MANAGE_STATE(m_pModuleState); // If that failed, try loading the type library from our own // resources. TCHAR szPath[_MAX_PATH]; GetModuleFileName(AfxGetInstanceHandle(), szPath, _MAX_PATH); if (FAILED(LoadTypeLib(T2COLE(szPath), &pTypeLib))) pTypeLib = NULL; } pTypeLibCache->Cache(lcid, pTypeLib); } // If we got a type library, extract the requested type info. if (pTypeLib != NULL) { hr = pTypeLib->GetTypeInfoOfGuid(guid, ppTypeInfo); pTypeLib->Release(); pTypeLibCache->CacheTypeInfo(lcid, guid, *ppTypeInfo); } } AfxUnlockGlobals(CRIT_TYPELIBCACHE); return hr; }
// LIBID_OliverCom := IID for type library HRESULT Sorter::loadITypeInfo() { LPTYPELIB pTypeLib; HRESULT hr = LoadRegTypeLib( LIBID_ComSort, 1, 0, 0, &pTypeLib ); if( SUCCEEDED(hr) ) { hr = pTypeLib->GetTypeInfoOfGuid( IID_ISort, &pITypeInfo ); if( SUCCEEDED(hr) ) { pTypeLib->Release(); pITypeInfo->AddRef(); } } return hr; }
int PolylineTest::writePersistFile() { ComPtr<IPolyline> pl; auto hr = CoCreateInstance(CLSID_PolylineObj, NULL, CLSCTX_INPROC_SERVER, IID_IPolyline, (void**)&pl); if (FAILED(hr)) { return -1; } LPTYPEINFO pTypeInfo = NULL; LPTYPELIB pTypelib = NULL; IRecordInfo* pRecInfo = NULL; hr = LoadRegTypeLib(LIBID_GraphicsLibrary, 1, 0, GetUserDefaultLCID(), &pTypelib); _ASSERT(SUCCEEDED(hr) && pTypelib); hr = pTypelib->GetTypeInfoOfGuid(__uuidof(PolyPoint), &pTypeInfo); _ASSERT(SUCCEEDED(hr) && pTypeInfo); hr = GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo); _ASSERT(SUCCEEDED(hr) && pRecInfo); pTypeInfo->Release(); pTypelib->Release(); std::vector<POINT> points = { { 1, 2 }, { 100, 200 }, { 300, 400 } }; auto psa = SafeArrayCreateVectorEx(VT_RECORD, 0, points.size(), pRecInfo); PolyPoint* pps; SafeArrayAccessData(psa, (void**)&pps); _ASSERT(psa); for (size_t i = 0; i < points.size(); ++i) { pps[i].x = points[i].x; pps[i].y = points[i].y; } pl->put_Points(psa); outputPoints(pl); ComPtr<IPersistFile> pf; pl->QueryInterface(IID_IPersistFile, (void**)&pf); pf->Save(kFileName, TRUE); pf->SaveCompleted(kFileName); return 0; }
HRESULT CoCOMServer::LoadTypeInfo(ITypeInfo ** pptinfo, const CLSID &libid, const CLSID &iid, LCID lcid) { HRESULT hr; LPTYPELIB ptlib = NULL; LPTYPEINFO ptinfo = NULL; *pptinfo = NULL; // Load type library. hr = LoadRegTypeLib(libid, 1, 0, lcid, &ptlib); if (FAILED(hr)) { // search for TypeLib in current dll WCHAR buf[MAX_PATH * sizeof(WCHAR)]; // LoadTypeLibEx needs Unicode string if (GetModuleFileNameW(g_hInstance, buf, _countof(buf))) hr = LoadTypeLibEx(buf,REGKIND_NONE,&ptlib); else // MemoryModule, search troug g_ListOfMemoryModules and use temp file to extract and load TypeLib file { HMEMORYMODULE hmodule = (HMEMORYMODULE)(g_hMemoryModule); HMEMORYRSRC res = MemoryFindResource(hmodule,_T("TYPELIB"),MAKEINTRESOURCE(1)); if (!res) return TYPE_E_INVALIDSTATE; DWORD resSize = MemorySizeOfResource(hmodule,res); // Path to temp directory + our temporary file name DWORD tempPathLength = GetTempPathW(MAX_PATH, buf); wcscpy(buf + tempPathLength,L"AutoHotkey.MemoryModule.temp.tlb"); // Write manifest to temportary file // Using FILE_ATTRIBUTE_TEMPORARY will avoid writing it to disk // It will be deleted after LoadTypeLib has been called. HANDLE hFile = CreateFileW(buf,GENERIC_WRITE,NULL,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY,NULL); if (hFile == INVALID_HANDLE_VALUE) { #if DEBUG_OUTPUT OutputDebugStringA("CreateFile failed.\n"); #endif return TYPE_E_CANTLOADLIBRARY; //failed to create file, continue and try loading without CreateActCtx } DWORD byteswritten = 0; WriteFile(hFile,MemoryLoadResource(hmodule,res),resSize,&byteswritten,NULL); CloseHandle(hFile); if (byteswritten == 0) { #if DEBUG_OUTPUT OutputDebugStringA("WriteFile failed.\n"); #endif return TYPE_E_CANTLOADLIBRARY; //failed to write data, continue and try loading } hr = LoadTypeLibEx(buf,REGKIND_NONE,&ptlib); // Open file and automatically delete on CloseHandle (FILE_FLAG_DELETE_ON_CLOSE) hFile = CreateFileW(buf,GENERIC_WRITE,FILE_SHARE_DELETE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_DELETE_ON_CLOSE,NULL); CloseHandle(hFile); } if (FAILED(hr)) return hr; } // Get type information for interface of the object. hr = ptlib->GetTypeInfoOfGuid(iid, &ptinfo); if (FAILED(hr)) { ptlib->Release(); return hr; } ptlib->Release(); *pptinfo = ptinfo; return NOERROR; }