C#注册OCX控件


注意

COM组件注册到注册表中的位置,是CLSID还是TypeLib

注册方法

代码执行

//声明注册方法
[DllImport("C:\\Windows\\barcodex.ocx")]
public static extern int DllRegisterServer();//注册时用
//DLL注册
int i = DllRegisterServer();
if (i >= 0)
{
  return true;
}
else
{
  return false;
}

调用控制台执行

string file="";//ocx的文件名称
string fileFullName = "\"" + file + "\"";
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");
 

反注册(卸载)方法

代码执行

//声明注册方法
[DllImport("C:\\Windows\\barcodex.ocx")]
public static extern int DllUnregisterServer();//取消注册时用
//DLL反注册
int i = DllUnregisterServer();
if (i >= 0)
{
  return true;
}
else
{
  return false;
}

调用控制台执行

string file="";//ocx的文件名称
string fileFullName = "\"" + file + "\"";
System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s /u");
 

检测是否安装的方法

注册在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,则已经表示安装。

注册在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;
 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM