C#注册OCX控件

1、注册方法
(1)
//声明注册方法
[DllImport("C:\\Windows\\barcodex.ocx")]
public static extern int DllRegisterServer();//注册时用
//DLL注册
int i = DllRegisterServer();
if (i >= 0)
{
return true;
}
else
{
return false;
}
(2)
string file="";//ocx的文件名称
string fileFullName = "\"" + file + "\"";
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");
 
2、反注册(卸载)方法
(1)
//声明注册方法
[DllImport("C:\\Windows\\barcodex.ocx")]
public static extern int DllUnregisterServer();//取消注册时用
//DLL反注册
int i = DllUnregisterServer();
if (i >= 0)
{
return true;
}
else
{
return false;
}
(2)
string file="";//ocx的文件名称
string fileFullName = "\"" + file + "\"";
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s /u");
 
3、检测是否安装的方法
(1)注册在CLSID目录下
String clsid="";//ocx组件的ClassID,不会重复
//设置返回值
Boolean result = false;
String key = String.Format(@"CLSID\{0}", clsid);//注意{},设置的clsid变量的id必须带{}
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
if (regKey != null)
{
result = true;
}
return result;
返回的regKey对象不为null,则已经表示安装。
(2)注册在TypeLib目录下
String clsid="";//ocx组件的ClassID,不会重复
//设置返回值
Boolean result = false;
//检查方法,查找注册表是否存在指定的clsid
String key = String.Format(@"TypeLib\{0}", clsid);//注意{},设置的clsid变量的id必须带{}
RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
if (regKey != null)
{
result = true;
}
return result;
 
4、注意:
COM组件注册到注册表中的位置,是CLSID还是TypeLib。

猜你喜欢

转载自www.cnblogs.com/masonblog/p/12740798.html