描述:
能提供关于TypeLibInfo的使用方法么,
最好能提供一个简单的实例, 谢谢
解决方案1:
void CPlusCfgDlg::OnBtnLoad()
{
#ifdef _USE_MFC_ALWAYS_
char szFileName[_MAX_PATH];
CFileDialog FileDlg(TRUE,"dll",NULL,OFN_FILEMUSTEXIST,"Winmsg PlusIn (*.dll)|*.dll|All Files (*.*)|*.*||",this);
if(FileDlg.DoModal() == IDOK)
{
lstrcpy(szFileName,FileDlg.GetPathName());
#else
char szFileName[_MAX_PATH],szInitialDir[_MAX_PATH];
lstrcpy(szInitialDir,GetAppFilePathName("PlusIn"));
szFileName[0] = '\0';
OPENFILENAME ofn;
memset(&ofn,0,sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = GetSafeHwnd();
ofn.lpstrInitialDir = szInitialDir;
ofn.lpstrFile = szFileName;
ofn.nMaxFile = _countof(szFileName);
ofn.lpstrTitle = lpFileDialogTitle;
ofn.Flags = OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST;
ofn.lpstrFilter = "Winmsg PlusIn (*.dll)\0*.dll\0All Files (*.*)\0*.*\0\0";
if(::GetOpenFileName(&ofn))
{
#endif
if(RegisterControl(szFileName))
{
ITypeLib *pTypeLib;
wchar_t ocFileName[_MAX_PATH];
::MultiByteToWideChar(CP_ACP, 0, szFileName, -1,ocFileName, _MAX_PATH);
HRESULT hr = ::LoadTypeLibEx(ocFileName,REGKIND_REGISTER,&pTypeLib);
if(SUCCEEDED(hr))
{
int count = pTypeLib->GetTypeInfoCount();
for(int i = 0; i < count; i++)
{
ITypeInfo *pTypeInfo;
TYPEATTR *pTypeAttr;
pTypeLib->GetTypeInfo(i,&pTypeInfo);
pTypeInfo->GetTypeAttr(&pTypeAttr);
if(pTypeAttr->typekind == TKIND_COCLASS)
{
CLSID CLSID_PlusIn = pTypeAttr->guid;
BOOL bSuccess;
hr = g_pApp->LoadPlusByClsid(CLSID_PlusIn,&bSuccess);
if(bSuccess)
{
g_pApp->SetActivePlusIn(-1);//Active the last plusin(it is loaded just now)
IWmPlusIn *pPlus = NULL;
g_pApp->GetActivePlusIn((IUnknown **)&pPlus,NULL);//Get it
ASSERT(pPlus != NULL);
BSTR bstrDisplayName,bstrShortName,bstrVerCopyright;
pPlus->GetCopyright(&bstrVerCopyright);
pPlus->GetDisplayName(&bstrDisplayName);
pPlus->GetShortName(&bstrShortName);
pPlus->Release();//important!
//prepare info struction
LPPLUSCFGINFO pInfo = new PLUSCFGINFO;
pInfo->strCLSID = AfxStringFromCLSID(CLSID_PlusIn);
pInfo->strDisplayName = bstrDisplayName;
pInfo->strPath = szFileName;
pInfo->strShortName = bstrShortName;
pInfo->strVerCopyright = bstrVerCopyright;
pInfo->strVerCopyright.Replace("@@@","\r\n");
m_ptrPlusInfo.Add((void *)pInfo);
//BSTR string allocated by COM server,so release it
::SysFreeString(bstrDisplayName);
::SysFreeString(bstrShortName);
::SysFreeString(bstrVerCopyright);
//Add the UI item
int idx = m_lstPlus.AddString(pInfo->strShortName);//add to the listbox
SetControlText(idx);
//save to plusinfo.ini
CIniProfile ini(GetAppFilePathName(lpPlusInfoFile));
int iTotalPlus = ini.GetInteger(lpSysSection,lpiTotalPlus);
iTotalPlus ++;
TCHAR szItem[16];
wsprintf(szItem,lpPlusIdx,iTotalPlus);
ini.SetInteger(lpSysSection,lpiTotalPlus,iTotalPlus);
LPCTSTR lpszShortName = (LPCTSTR)pInfo->strShortName;
ini.SetString(lpSysSection,szItem,lpszShortName);
ini.SetString(lpszShortName,lpShortName,lpszShortName);
ini.SetString(lpszShortName,lpClsid,pInfo->strCLSID);
ini.SetString(lpszShortName,lpDisplayName,pInfo->strDisplayName);
ini.SetString(lpszShortName,lpPathName,szFileName);
CString strCopyright = bstrVerCopyright;
ini.SetString(lpszShortName,lpVerCopyright,strCopyright);
}
else//if loaded fail but the return code is S_OK,that's mean that plusin has already loaded
{
if(SUCCEEDED(hr))
MessageBox(lpAlLoad,lpWinmsg,MB_OK);
}
}
pTypeInfo->ReleaseTypeAttr(pTypeAttr);
pTypeInfo->Release();
}
pTypeLib->Release();
}
}
}
}