ocx怎么得到classid,与动态执行 bat 文件

1.注册ocx控件: Regsvr32 [PATH]\xxx.ocx
2.利用Regedit.exe注册表编辑器,在编辑器的查找里直接输入 .OCX
文件名进行查找,找到:
“HKEY_CLASSES_ROOT\CLSID\{xxxxxxxxxxxxxxxxxxxxxxxxxxx}”主键
后,再利用注册表编辑器菜单上-[注册表]-[导出注册表文件]-然后在文件
选择窗里输入导出的注册表文件名

public sealed  class COMUtils
    {
      /// <summary>
      /// 检查指定的 COM 组件是否已注册到系统中
      /// </summary>
      /// <param name="clsid">指定 COM 组件的Class Id</param>
      /// <returns>true: 表示已注册;false: 表示未注册</returns>
      public static System.Boolean IsRegistered(String clsid)
      {
       
         //参数检查
          System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(clsid), "clsid 不应该为空");
         
          //设置返回值
          Boolean result=false;

          //检查方法,查找注册表是否存在指定的clsid
          String key = String.Format(@"CLSID\{{{0}}}", clsid);
          RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(key);
          if (regKey != null)
          {
              result = true;
          }
        
          return result;
      }//end method


      /// <summary>
      /// 注册指定的 COM 组件到系统中
      /// </summary>
      /// <param name="file">指定的 COM 组件</param>
      /// <returns>true: 表示已注册;false: 表示未注册</returns>
      public static System.Boolean Register(String file)
      {

          //参数检查
          System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(file), "file 不应该为空");

          //设置返回值
          Boolean result = false;

          //检查方法,查找注册表是否存在指定的clsid
          string fileFullName = "\"" + file + "\"";
          System.Diagnostics.Process p=System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s");

          if (p != null && p.HasExited)
          {
              Int32 exitCode=p.ExitCode;
              if (exitCode == 0)
              {
                  result = true;
              }
          }
          return result;
      }//end method

      /// <summary>
      /// 反注册指定的 COM 组件
      /// </summary>
      /// <param name="file">指定的 COM 组件</param>
      /// <returns>true: 表示反注册成功;false: 表示反注册失败</returns>
      public static System.Boolean UnRegister(String file)
      {

          //参数检查
          System.Diagnostics.Debug.Assert(!String.IsNullOrEmpty(file), "file 不应该为空");

          //设置返回值
          Boolean result = false;

          //检查方法,查找注册表是否存在指定的clsid
          string fileFullName = "\"" + file + "\"";
          System.Diagnostics.Process p = System.Diagnostics.Process.Start("regsvr32", fileFullName + " /s /u");

          if (p != null && p.HasExited)
          {
              Int32 exitCode = p.ExitCode;
              if (exitCode == 0)
              {
                  result = true;
              }
          }
          return result;
      }//end method


    }//end class

 

原理:

C#代码:

方式一:

引用命名空间:using Microsoft.Win32;

判断指定CLASSID 的注册表键值是否存在来判断是否存在注册类。

RegistryKey regKey = Registry.ClassesRoot.OpenSubKey("CLSID\\{00460182-9E5E-11d5-B7C8-B8269041DD57}\\");

if (regKey != null)

{
                        MessageBox.Show("存在指定ClassID的注册");
                    }

方法二:

通过包装的对象,直接建立实例,来确定对象是否注册,失败表示未注册,成功表示注册。

方法三:

通过ClassID建立对象。

public object GetActiveXObject(Guid clsid)
        {
            Type t = Type.GetTypeFromCLSID(clsid);
            if (t == null)
                return null;
            return Activator.CreateInstance(t);
        }

 

动态执行*.bat 文件:

函数原型为:

/// <summary>
/// 打开控制台执行拼接完成的批处理命令字符串
/// </summary>
/// <param name="inputAction">需要执行的命令委托方法:每次调用 <paramref name="inputAction"/> 中的参数都会执行一次</param>
private static void ExecBatCommand(Action<Action<string>> inputAction)

使用示例如下:

ExecBatCommand(p =>
{
    p(@"net use \\10.32.11.21\ERPProject yintai@123 /user:yt\ERPDeployer");
    // 这里连续写入的命令将依次在控制台窗口中得到体现
    p("exit 0");
});

注:执行完需要的命令后,最后需要调用 exit 命令退出控制台。这样做的目的是可以持续输入命令,知道用户执行退出命令 exit 0,而且退出命令必须是最后一条命令,否则程序会发生异常。

下面是批处理执行函数源码:

/// <summary>
/// 打开控制台执行拼接完成的批处理命令字符串
/// </summary>
/// <param name="inputAction">需要执行的命令委托方法:每次调用 <paramref name="inputAction"/> 中的参数都会执行一次</param>
private static void ExecBatCommand(Action<Action<string>> inputAction)
{
    Process pro = null;
    StreamWriter sIn = null;
    StreamReader sOut = null;
 
    try
    {
        pro = new Process();
        pro.StartInfo.FileName = "cmd.exe";
        pro.StartInfo.UseShellExecute = false;
        pro.StartInfo.CreateNoWindow = true;
        pro.StartInfo.RedirectStandardInput = true;
        pro.StartInfo.RedirectStandardOutput = true;
        pro.StartInfo.RedirectStandardError = true;
 
        pro.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
        pro.ErrorDataReceived += (sender, e) => Console.WriteLine(e.Data);
 
        pro.Start();
        sIn = pro.StandardInput;
        sIn.AutoFlush = true;
 
        pro.BeginOutputReadLine();
        inputAction(value => sIn.WriteLine(value));
 
        pro.WaitForExit();
    }
    finally
    {
        if (pro != null && !pro.HasExited)
            pro.Kill();
 
        if (sIn != null)
            sIn.Close();
        if (sOut != null)
            sOut.Close();
        if (pro != null)
            pro.Close();
    }
}

 

转载于:https://www.cnblogs.com/zhangchen/p/4544599.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值