乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 129|回复: 9

使用C# Late Binding Com类库

[复制链接]

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2017-11-26 19:17:00 | 显示全部楼层 |阅读模式
之前发过32位版本的后绑定Com库的方案,利用了PIA,但是64位AutoCad中桌子似乎偷懒了(常态),而且Cad库直接使用dynamic动态调用也是很麻烦,所以下面是解决方案
项目是VS2015编写的,。Net框架的版本为4.0,要注意几个问题:
1、代码使用了C#6.0中的新特性,较低版本的要改写,不过推荐使用VS2015+
2、主项目必须生成AnyCpu才能调用,原因未知。。。
3、没有做很详细的测试,欢迎大家参与测试。。。

本帖以下内容被隐藏保护;需要你回复后,才能看到!

游客,如果您要查看本帖隐藏内容请回复
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2017-11-27 14:13:00 | 显示全部楼层
2017/11/27
修改Assembly类加载不必要类型的Bug
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using ComTypes = System.Runtime.InteropServices.ComTypes;
  5. namespace NFox.Runtime.Com.Reflection
  6. {
  7.     ///
  8.     /// Com程序集,只有类型集合,类型别名、枚举等未处理
  9.     ///
  10.     public class Assembly
  11.     {
  12.         ///
  13.         /// 程序集字典
  14.         ///
  15.         private static Dictionary _openedAssemblies
  16.             = new Dictionary();
  17.         public static DirectoryInfo Root
  18.         {
  19.             get
  20.             {
  21.                 var assem = System.Reflection.Assembly.GetExecutingAssembly();
  22.                 var fi = new FileInfo(assem.Location);
  23.                 return fi.Directory.CreateSubdirectory("ComAssemblies");
  24.             }
  25.         }
  26.         ///
  27.         /// 获取IDispatch接口对象的类型信息
  28.         ///
  29.         /// IDispatch接口对象
  30.         /// 对象的类型信息
  31.         public static Type GetType(IDispatch obj)
  32.         {
  33.             AssemblyKey key;
  34.             ComTypes.ITypeInfo info;
  35.             obj.GetTypeInfo(0, 0, out info);
  36.             ComTypes.ITypeLib lib;
  37.             int id;
  38.             info.GetContainingTypeLib(out lib, out id);
  39.             
  40.             IntPtr ip;
  41.             lib.GetLibAttr(out ip);
  42.             var la = Utils.GetObject(ip);
  43.             key = new AssemblyKey(la.guid, la.wMajorVerNum, la.wMinorVerNum);
  44.             lib.ReleaseTLibAttr(ip);
  45.             if (!_openedAssemblies.ContainsKey(key))
  46.             {
  47.                 string name, doc, helpfile;
  48.                 int hc;
  49.                 lib.GetDocumentation(-1, out name, out doc, out hc, out helpfile);
  50.                 var dir = Root.CreateSubdirectory(name);
  51.                 var clsdir = dir.CreateSubdirectory(key.ClsId.ToString());
  52.                 var verdir = clsdir.CreateSubdirectory(key.Version);
  53.                 _openedAssemblies.Add(key, new Assembly(key, name, verdir));
  54.             }
  55.             return _openedAssemblies[key].GetType(id, info);
  56.         }
  57.         private Assembly(AssemblyKey key, string name, DirectoryInfo path)
  58.         {
  59.             Key = key;
  60.             Name = name;
  61.             _root = path;
  62.         }
  63.         public string Name { get; }
  64.         public AssemblyKey Key { get; }
  65.         private DirectoryInfo _root;
  66.         ///
  67.         /// 类型字典
  68.         ///
  69.         public Dictionary[i] Types { get; }
  70.             = new Dictionary[i]();
  71.         ///
  72.         /// 按索引获取Com程序集中的类型信息
  73.         ///
  74.         /// 索引
  75.         /// ITypeInfo接口
  76.         /// 对应索引的类型信息
  77.         public Type GetType(int id, ComTypes.ITypeInfo info)
  78.         {
  79.             if (!Types.ContainsKey(id))
  80.             {
  81.                 var name = $"{_root.FullName}/{id}.typ";
  82.                 if (File.Exists(name))
  83.                 {
  84.                     Types.Add(id, new Type(this, name));
  85.                 }
  86.                 else
  87.                 {
  88.                     Type type = new Type(this, info);
  89.                     type.Save(name);
  90.                     Types.Add(id, type);
  91.                 }
  92.             }
  93.             return Types[id];
  94.         }
  95.         public override string ToString()
  96.         {
  97.             return Name;
  98.         }
  99.     }
  100. }
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2017-11-26 19:19:00 | 显示全部楼层
下列代码对AutoCad2016 64位和AutoCad2010 32位均测试通过
  1.             var app = Comproxy.GetObject("AutoCad.Application");
  2.             var doc = app.ActiveDocument;
  3.             var util = doc.Utility;
  4.             var mspace = doc.ModelSpace;
  5.             dynamic obj, pt;
  6.             util.GetEntity(out obj, out pt);
  7.             double[] p1 = { 0, 0, 0 };
  8.             double[] p2 = { 0, 10, 0 };
  9.             double[] p3 = { 10, 0, 0 };
  10.             var objs =
  11.                 Comproxy.CreateArray(
  12.                     mspace.AddLine(p1, p2),
  13.                     mspace.AddLine(p2, p3),
  14.                     mspace.AddLine(p3, p1));
  15.             var r = mspace.AddRegion(objs);
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2017-11-30 16:42:00 | 显示全部楼层
2016 执行到红色这句错误
public static Type GetType(IDispatch obj)
        {
            AssemblyKey key;
            ComTypes.ITypeInfo info;
            obj.GetTypeInfo(0, 0, out info);
            ComTypes.ITypeLib lib;
            int id;
            info.GetContainingTypeLib(out lib, out id);
            IntPtr ip;
            lib.GetLibAttr(out ip);
            var la = Utils.GetObject(ip);
            key = new AssemblyKey(la.guid, la.wMajorVerNum, la.wMinorVerNum);
            lib.ReleaseTLibAttr(ip)
回复

使用道具 举报

32

主题

140

帖子

5

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
268
发表于 2017-11-26 20:07:00 | 显示全部楼层
狐哥雄壮!狐哥威武!
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2017-11-29 23:16:00 | 显示全部楼层
下载你的程序,直接运行,出现如下错误

thqy3l4kqn3.PNG

thqy3l4kqn3.PNG


请问是什么原因?
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2017-11-30 06:28:00 | 显示全部楼层
你的cad版本多少? 测试代码运行到哪一步?
回复

使用道具 举报

72

主题

2726

帖子

9

银币

社区元老

Rank: 75Rank: 75Rank: 75

铜币
3014
发表于 2017-11-30 19:19:00 | 显示全部楼层
你在窗体的代码里设个断点 看走到哪一步
回复

使用道具 举报

1

主题

5

帖子

1

银币

初来乍到

Rank: 1

铜币
9
发表于 2017-12-2 15:46:00 | 显示全部楼层
今天重新运行一下,又成功了,有点莫名其妙!
回复

使用道具 举报

2

主题

17

帖子

2

银币

初来乍到

Rank: 1

铜币
25
发表于 2018-1-17 17:51:00 | 显示全部楼层
想下载一个试试
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-3-13 02:23 , Processed in 0.618213 second(s), 77 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表