CAD 二次开发 -- 自动加载开发的DLL

CAD二次开发可以采用写扩展DLL的方式实现。该DLL的函数可以被CAD调用。

但是调用前,必须用命令netload 将该dll加载到CAD。

其实可以修改注册表,当CAD软件启动后,自动加载扩展DLL。

为此,我写了一个函数,很方便的修改注册表,达到自动加载dll的目的!

该函数支持各个版本cad。

 

string[] GetRegSubDir(RegistryKey autoCad, string startFlag)
{
string[] subKey = autoCad.GetSubKeyNames();
return subKey.Where(o => o.StartsWith(startFlag)).ToArray();
}

 1 //strCadRegKey = "HLD_CAD_Import"; //唯一注册标识 可用公司名称和程序名称
 2 //strDll dll文件路径
 3 private bool WriteRegistryKey(string strDll,string strCadRegKey)
 4         {
 5             try
 6             {
 7                 RegistryKey localMachine = Registry.LocalMachine;
 8                 RegistryKey SOFTWARE = localMachine.OpenSubKey("SOFTWARE", true);
 9                 RegistryKey Autodesk = SOFTWARE.OpenSubKey("Autodesk", true);
10                 RegistryKey AutoCAD = Autodesk.OpenSubKey("AutoCAD", true);
11 
12                 int result = 0;
13                 foreach (string subDir in GetRegSubDir(AutoCAD, "R"))
14                 {
15                     try
16                     {
17                         RegistryKey CadVersion = AutoCAD.OpenSubKey(subDir, true);
18 
19                         string AcadVersion = GetRegSubDir(CadVersion, "ACAD-").FirstOrDefault();
20                         RegistryKey ACAD = CadVersion.OpenSubKey(AcadVersion, true);
21 
22                         RegistryKey Applications = ACAD.OpenSubKey("Applications", true);
23 
24                         //删除旧有记录
25                         try
26                         {
27                             Applications.DeleteSubKeyTree(strCadRegKey);
28                         }
29                         catch (Exception ex)
30                         { }
31 
32                         RegistryKey MXCAD = Applications.CreateSubKey(strCadRegKey);
33                         MXCAD.SetValue("LOADCTRLS", 0x02);
34                         MXCAD.SetValue("LOADER", strDll);
35                         MXCAD.SetValue("MANAGED", 0x01);
36                         result++;
37                     }
38                     catch (Exception ex)
39                     {
40                         if (showMessage)
41                             System.Windows.Forms.MessageBox.Show(string.Format("注册时出错!{0}", ex.Message));
42                     }
43                 }
44 
45                 if (result > 0 && showMessage)
46                     System.Windows.Forms.MessageBox.Show(string.Format("注册成功!"));
47                 return true;
48             }
49             catch (Exception ex)
50             {
51               
52                 return false;
53             }
54         }

技术交流联系qq 13712486

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值