之前公司有套C# AES加解密方案,但是方案加密用的是Rijndael类,而非AES的四种模式(ECB、CBC、CFB、OFB,这四种用的是RijndaelManaged类),Python下Crypto库AES也只有这四种模式,进而Python下无法实现C# AES Rijndael类加密效果了。

       类似于这种C# 能实现的功能而在Python下实现不了的,搜集资料有两种解决方案,第一种方式,使用IronPython 直接调用C# dll文件,教程网上很多,不在赘述了,这种方式有个缺点,用的是ironPython而非Python,只是集成了一些.net framework库的Python版本,更新维护少;第二种方式是,C# dll源码编译成Com组件,Python再调用COM组件Dll的方法。

       网上有很多Python调用COM dll教程,但大部分是C或C++编写的dll,很少有比较全面的讲解COM组件生成至调用过程,下面结合自己摸索多天的经历,简单介绍下如何生成COM组件,以及用Python如何调用COM dll组件,分享给大家。

       我也是小白 ……^ ^,高手请飘过,如有写的不对之处,还请多多包涵以指正...

 

1.如何生成C# COM组件

我用的是Microsoft visual studio 2010, 首先新建--项目--选择【类库】,名称自定义:ComToPython,点击【确定】

 

Python编译com组件 python com组件_com

重命名cs文件:ComToPython.cs,可自定义。 弹窗选择【是】

Python编译com组件 python com组件_python_02

 

COM可见性设置为 True:

 

Python编译com组件 python com组件_Python编译com组件_03

 

上面等同于以下项目属性设置:

 

Python编译com组件 python com组件_Python编译com组件_04

勾选“为COM互操作注册”:

Python编译com组件 python com组件_Python编译com组件_05

 

新建签名ComToPythonKey,取消勾选 “使用密码保护密钥文件”

 

Python编译com组件 python com组件_c#_06

编写接口类IMyClass,ComToPython类实现接口的三个方法,例如Add()方法就是我们想要实现的功能,返回a与b之和。

ComToPython类之前的[ClassInterface(ClassInterfaceType.None)]一定要有,否则Python调用时会报错。

[ProgId("ComToPython.Application")]指定Python调用COM时的名称,后面Python代码会看到。

1 using System;
 2 
 3 using System.Collections.Generic;
 4 
 5 using System.Linq;
 6 
 7 using System.Text;
 8 
 9 using System.Runtime.InteropServices;
10 
11  
12 
13 namespace ComToPython
14 
15 {
16 
17     [Guid("350779B9-8AB5-4951-83DA-4CBC4AD860F4")]
18 
19     public interface IMyClass
20 
21     {
22 
23         void Initialize();
24 
25         void Dispose();
26 
27         int Add(int x, int y);
28 
29     }
30 
31  
32 
33     [ClassInterface(ClassInterfaceType.None)]
34 
35     [Guid("16D9A0AD-66B3-4A8A-B6C4-67C9ED0F4BE4")]
36 
37     [ProgId("ComToPython.Application")]
38 
39     public class ComToPython: IMyClass
40 
41     {
42 
43         public void Initialize()
44 
45         {
46 
47             // nothing to do 
48 
49         }
50 
51  
52 
53         public void Dispose()
54 
55         {
56 
57             // nothing to do 
58 
59         }
60 
61  
62 
63         public int Add(int x, int y)
64 
65         {
66 
67             return x + y;
68 
69         }
70 
71     }
72 
73 }

 

GUID使用VS2010自带工具生成,工具--创建GUID,点击复制两个GUID分别放至两个类名之前

Python编译com组件 python com组件_c#_07

注:点击新建GUID,可复制新建后的GUID:

Python编译com组件 python com组件_dll_08

最后F6编译生成解决方案,在你的工程Debug目录下会有ComToPython.dll生成:

Python编译com组件 python com组件_python_09

 

最后一步注册COM组件至系统

开始菜单--打开VS 2010 自带CMD命令窗口(管理员权限)定位至ComToPython.dll文件夹下

执行:gacutil /i ComToPython.dll 添加dll至全局缓存

执行:regasmComToPython.dll 注册dll至系统

Python编译com组件 python com组件_c#_10

 

2.Python如何调用COM dll组件

 

我用的是Python 2.7,IDE用的PyCharm 2017.1,PyCharm新建--项目ComToPython,新建项目py文件ComToPython.py

设置--添加两个依赖库:

 

Python编译com组件 python com组件_dll_11

添加安装pywin32和comtypes依赖库,以对应后面的两种调用COM组件方式:

 

Python编译com组件 python com组件_dll_12

 

依赖装好后,Python安装目录site-packages目录下会有个win32com文件夹,双击C:\Python27\Lib\site-packages\win32com\client\下的makepy.py

选择ComToPython,点击OK

 

Python编译com组件 python com组件_com_13

 

然后拷贝上面VS2010生成的COM组件ComToPython.dll至PyCharm ComToPython项目文件夹下:

 

Python编译com组件 python com组件_python_14

 

编写python调用COM dll代码:

1 #!/usr/bin/env python
 2 
 3 # -*- coding: utf-8 -*-
 4 
 5 a=1
 6 
 7 b=2
 8 
 9  
10 
11 print "方法一:"
12 
13 from win32com.client import Dispatch
14 
15 dll = Dispatch("ComToPython.Application")
16 
17 result = dll.Add(a, b)
18 
19 print "a + b = " + str(result)
20 
21  
22 
23 print "方法二:"
24 
25 import comtypes.client
26 
27 dll = comtypes.client.CreateObject('ComToPython.Application')
28 
29 result = dll.Add(a, b)
30 
31 print "a + b = " + str(result)

 

 

运行代码,执行结果如下:

 

Python编译com组件 python com组件_Python编译com组件_15

 

以上就是Python调用C# COM Dll整个过程了

第一篇博文,多谢支持~~~