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();
        }
//strCadRegKey = "HLD_CAD_Import"; //唯一注册标识 可用公司名称和程序名称
//strDll dll文件路径
private bool WriteRegistryKey(string strDll,string strCadRegKey)
        {
            try
            {
                RegistryKey localMachine = Registry.LocalMachine;
                RegistryKey SOFTWARE = localMachine.OpenSubKey("SOFTWARE", true);
                RegistryKey Autodesk = SOFTWARE.OpenSubKey("Autodesk", true);
                RegistryKey AutoCAD = Autodesk.OpenSubKey("AutoCAD", true);

                int result = 0;
                foreach (string subDir in GetRegSubDir(AutoCAD, "R"))
                {
                    try
                    {
                        RegistryKey CadVersion = AutoCAD.OpenSubKey(subDir, true);

                        string AcadVersion = GetRegSubDir(CadVersion, "ACAD-").FirstOrDefault();
                        RegistryKey ACAD = CadVersion.OpenSubKey(AcadVersion, true);

                        RegistryKey Applications = ACAD.OpenSubKey("Applications", true);

                        //删除旧有记录
                        try
                        {
                            Applications.DeleteSubKeyTree(strCadRegKey);
                        }
                        catch (Exception ex)
                        { }

                        RegistryKey MXCAD = Applications.CreateSubKey(strCadRegKey);
                        MXCAD.SetValue("LOADCTRLS", 0x02);
                        MXCAD.SetValue("LOADER", strDll);
                        MXCAD.SetValue("MANAGED", 0x01);
                        result++;
                    }
                    catch (Exception ex)
                    {
                        if (showMessage)
                            System.Windows.Forms.MessageBox.Show(string.Format("注册时出错!{0}", ex.Message));
                    }
                }

                if (result > 0 && showMessage)
                    System.Windows.Forms.MessageBox.Show(string.Format("注册成功!"));
                return true;
            }
            catch (Exception ex)
            {
              
                return false;
            }
        }

猜你喜欢

转载自www.cnblogs.com/belx/p/9256751.html