vc下安装控件和卸载控件,已经封装成类。OperateOcx. 和OperateOcx.cpp
OperateOcx.h 如下
#pragma once
// 向IE中安装控件,以便IE发送消息
class COperateOcx
{
public:
COperateOcx(void);
~COperateOcx(void);
BOOL InstallOcx(CString strOcxPath);//安装一个ocx
BOOL IsHaveInstall(const char* pPath);
BOOL UninstallOcx(const char* pPath);//卸载一个ocx
BOOL SupportsSelfRegister(CString strOcxPath) ;
};
OperateOcx.cpp 如下
#include "StdAfx.h"
#include "OperateOcx.h"
#ifdef _WIN32
#define LOADLIBRARY_FAILED(x) (x == 0)
#else
#define LOADLIBRARY_FAILED(x) (x <= HINSTANCE_ERROR)
#endif
#pragma comment(lib,"Version.lib")
typedef HRESULT(STDAPICALLTYPE *CTLREGPROC)() ;
COperateOcx::COperateOcx(void)
{
}
COperateOcx::~COperateOcx(void)
{
}
/*
************************************
安装一个ocx
************************************
*/
BOOL COperateOcx::InstallOcx(CString strOcxPath)
{
char szTm[MAX_PATH] = {0};
::GetShortPathName(strOcxPath, szTm, MAX_PATH-1);
strOcxPath = szTm;
BOOL bResult = FALSE;
CString szTemp="regsvr32.exe ";
//MessageBox(szTemp);
CString strTemp="/s "+strOcxPath;
//WinExec(szTemp,SW_HIDE);
if((int)ShellExecute(NULL,"OPEN",szTemp,strTemp,NULL,SW_HIDE) > 32)
{
bResult = TRUE;
}
return bResult;
}
/*
************************************
安装一个ocx
************************************
*/
BOOL COperateOcx::IsHaveInstall(const char* pPath)
{
return 0;
}
/*
************************************
卸载一个ocx
************************************
*/
BOOL COperateOcx::UninstallOcx(const char* pPath)
{
char szTm[MAX_PATH];
::GetShortPathName(pPath, szTm, MAX_PATH-1);
BOOL bResult = FALSE ;
CString szTemp="regsvr32.exe " ;
//MessageBox(szTemp);
CString strTemp="/u /s "+ CString(szTm);
//WinExec(szTemp,SW_HIDE);
ShellExecute(NULL,"OPEN",szTemp,strTemp,NULL,SW_HIDE);
return FALSE ;
}
BOOL COperateOcx::SupportsSelfRegister(CString strOcxPath)
{
BOOL bResult = FALSE;
DWORD handle;
UINT uiInfoSize;
UINT uiVerSize ;
UINT uiSize ;
BYTE* pbData = NULL ;
DWORD* lpBuffer;;
char szName[512] ;
// 获得文件大小信息
uiInfoSize =
::GetFileVersionInfoSize(strOcxPath.GetBuffer(0),
&handle);
if (uiInfoSize == 0) return FALSE ;
pbData = new BYTE[uiInfoSize] ;
// 获得文件信息
bResult =
::GetFileVersionInfo(strOcxPath.GetBuffer(0),
handle,
uiInfoSize,
pbData);
if (!bResult) goto NastyGoto ;
// 在注册表中查找
bResult =
::VerQueryValue( pbData,
"\\VarFileInfo\\Translation",
(void**)&lpBuffer,
&uiVerSize);
if (!bResult) goto NastyGoto ;
bResult = uiVerSize ;
if (!bResult) goto NastyGoto ;
// Build the path to the OLESelfRegister key
// using the translation information.
sprintf( szName,
"\\StringFileInfo\\%04hX%04hX\\OLESelfRegister",
LOWORD(*lpBuffer),
HIWORD(*lpBuffer)) ;
// Search for the key.
bResult = ::VerQueryValue( pbData,
szName,
(void**)&lpBuffer,
&uiSize);
NastyGoto:
delete [] pbData ;
return bResult ;
}