VC++注册,卸载OCX控件,以及判断是否注册
注册OCX控件
BOOL CYourClass::RegistOcx()
{
HINSTANCE hLib = LoadLibrary("NTGraph.ocx"); //指定要注册的ocx文件的路径及文件名。
if (hLib == NULL)
{
MessageBox("不能载入OCX文件!");
return;
}
//获取注册函数DllRegisterServer地址
FARPROC lpDllEntryPoint;
lpDllEntryPoint = GetProcAddress(hLib,"DllRegisterServer");
//调用注册函数DllRegisterServer
if(lpDllEntryPoint!=NULL)
{
if(FAILED((*lpDllEntryPoint)()))
{
//MessageBox(Handle,"调用DllRegisterServer失败!","失败ing..",MB_OK);
MessageBox("OCX注册失败!");
FreeLibrary(hLib);
return;
};
//MessageBox(Handle,"注册成功!","Reg",MB_OK);
MessageBox("OCX注册成功!");
}
else
MessageBox("OCX注册失败!");
//MessageBox(Handle,"调用DllRegisterServer失败!","失败ing..",MB_OK);
}
卸载控件
BOOL CYourClass::UnRegistOcx()
{
HINSTANCE hLib = LoadLibrary("NTGraph.ocx");
//指定要注册的ocx文件的路径及文件名。
if (hLib==NULL)
{
//MessageBox(Handle,"不能载入Dll文件!","失败ing..",MB_OK);
MessageBox("不能载入OCX文件!");
return;
}
//获取注册函数DllRegisterServer地址
FARPROC lpDllEntryPoint;
lpDllEntryPoint = GetProcAddress(hLib,"DllUnregisterServer");//注意这里是DllUnregisterServer。
//调用注册函数DllRegisterServer
if(lpDllEntryPoint!=NULL)
{
if(((*lpDllEntryPoint)()))
{
//MessageBox(Handle,"调用DllUnRegisterServer失败!","失败ing..",MB_OK);
MessageBox("调用DllUnRegisterServer失败!!!");
FreeLibrary(hLib);
return;
};
//MessageBox(Handle,"注销成功!","Unreg",MB_OK);
MessageBox("OCX注销成功!");
}
else
//MessageBox(Handle,"调用DllUnRegisterServer失败!!!","结果",MB_OK);
MessageBox("OCX调用DllUnRegisterServer失败!!!");
}
判断是否注册
BOOL CYourClass::IsRegistOcx()
{
HKEY hKey;
BOOL bPresent;
TCHAR szPath[_MAX_PATH];
DWORD dwRegType;
DWORD cbData = sizeof szPath * sizeof TCHAR;
if(RegOpenKeyEx(HKEY_CLASSES_ROOT,"ActiveX.ActiveXControl//Clsid",0,KEY_READ,&hKey)!=ERROR_SUCCESS)
return false;
else
return true; //ActiveX.ActiveXControl为控件的名称和内部名称 如"NTGRAPH.NTGraphCtrl.1//CLSID"
}