注意
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;