public class DsoframerHelper
{
private static string dsoframerPath = System.Windows.Forms.Application.StartupPath + @"/Plugins/dsoframer.ocx";
private static string sys32Path = @"c:\windows\System32\dsoframer.ocx";//32位系統存放dsoframer.ocx的目錄
private static string sys64Path = @"c:\windows\SysWOW64\dsoframer.ocx";//64位系統存放dsoframer.ocx的目錄
/// <summary>
/// 判斷ocx控件是否注冊的
/// </summary>
/// <param name="clsid"></param>
/// <returns></returns>
private static bool IsRegistered(string clsid)
{
String key = String.Format(@"CLSID\{{{0}}}", clsid);
Microsoft.Win32.RegistryKey Regkey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(key);//獲取注冊key
if (Regkey != null)
if(Regkey != null)
{
if(Regkey.OpenSubKey("InprocServer32").GetValue("") != null)//獲取注冊路徑
return true;
else
return false;
}
else
return false;
}
/// <summary>
/// 執行cmd.exe
/// </summary>
/// <param name="cmdExe"></param>
/// <param name="cmdPara"></param>
private static void Cmd(string cmdExe, string cmdPara)
{
using (System.Diagnostics.Process myPro = new System.Diagnostics.Process())
{
myPro.StartInfo.FileName = "cmd.exe";
myPro.StartInfo.UseShellExecute = false; //是否使用操作系統shell啟動
myPro.StartInfo.RedirectStandardInput = true;//接受來自調用程序的輸入信息
myPro.StartInfo.RedirectStandardOutput = true;//由調用程序獲取輸出信息
myPro.StartInfo.RedirectStandardError = true;//重定向標准錯誤輸出
myPro.StartInfo.CreateNoWindow = true;//不顯示程序窗口
myPro.StartInfo.Verb="runas";//以管理員的身份打開
myPro.Start();
string strCmd = $@"{cmdExe} {cmdPara} &exit"; //這里使用 & 是批處理命令的符號,表示前面一個命令不管是否執行成功都執行后面(exit)命令
myPro.StandardInput.WriteLine(strCmd);
myPro.StandardInput.AutoFlush = true;
myPro.WaitForExit();//等待程序執行完退出進程
}
}
/// <summary>
/// 判斷dsoframer是否注冊
/// </summary>
/// <returns></returns>
public static bool IsRegisteredDsoframer()
{
return IsRegistered("00460182-9E5E-11d5-B7C8-B8269041DD57");
}
/// <summary>
/// 注冊dsoframer
/// </summary>
public static void RegisteredDsoframer()
{
if (!File.Exists(dsoframerPath))
return;
//將dsoframer.ocx拷貝到系統目錄
string sysPath = "";
if (Environment.Is64BitOperatingSystem)
sysPath = sys64Path;
else
sysPath = sys32Path;
if (!File.Exists(sysPath))
File.Copy(dsoframerPath, sysPath);
Cmd("regsvr32.exe", sysPath);
}
/// <summary>
/// 解注冊dsoframer
/// </summary>
public static void UnRegisteredDsoframer()
{
Cmd("regsvr32.exe", $@" -u {dsoframerPath}");
}
}
注意:如果注冊的時候,被殺毒軟件阻攔了,會造成注冊表中有key沒有value的情況。所以注冊是否成功需要判斷key和value是否都有值才行!!!
(1)准備工作:
在解決方案下創建Plugins目錄,然后將dsoframer.ocx復制到Plugins目錄下
(2)使用:
if (!DsoframerHelper.IsRegisteredDsoframer())
DsoframerHelper.RegisteredDsoframer();
檢查ocx控件是否注冊需要用到clsid,一下是查找方法
dsoframer.ocx(32位)下載地址:https://pan.baidu.com/s/16Jd60vgU09KYxzOlZsti-A 提取碼:7xgh 內涵函數使用方法
