Before the company has a set of C# AES encryption and decryption program, but the program encryption with the Rijndael class, rather than the four modes of AES (ECB, CBC, CFB, OFB, these four with the RijndaelManaged class), Python under the Crypto library AES is only the four modes, which can not be realized under the Python C# AES Rijndael class encryption effect under Python.
Similar to this C# can realize the function in Python can not be achieved under the collection of information there are two solutions, the first way, using IronPython directly call C# dll file, tutorials online, not to repeat, this way has a disadvantage, using ironPython rather than Python, just integrated with a number of . framework library Python version, update maintenance less; the second way is, C# dll source code compiled into Com components, Python and then call the COM component Dll method.
There are many Python call COM dll tutorials on the Internet, but most of them are written in C or C++ dll, very few of them are more comprehensive explanation of the COM component generation to the call process, the following combination of their own experience in fumbling for many days, a brief introduction to how to generate COM components, and how to use Python to call the COM dll component to share with you.
I'm a beginner too. ......^ ^, masters please drift through, if there is a wrong place to write, please bear with me to correct...
1.How to generateC# COMsubassemblies
I use Microsoft visual studio 2010, first of all new - project - select [library], name customization: ComToPython, click [OK].
Rename cs file:, customizable. Popup window to select [Yes]
COM visibility is set to True:
The above is equivalent to the following item property settings:
Check "Register for COM interoperability":
New signature ComToPythonKey, uncheck "Use password to protect key file"
Write the interface class IMyClass, ComToPython class implements the three methods of the interface, e.g., the Add() method is what we want to implement, returning the sum of a and b.
The [ClassInterface()] before the ComToPython class must be there, otherwise Python will report an error when calling it.
[ProgId("")] specifies the name of the COM when Python calls it, as seen later in the Python code.
using System; using ; using ; using ; using ; namespace ComToPython { [Guid("350779B9-8AB5-4951-83DA-4CBC4AD860F4")] public interface IMyClass { void Initialize(); void Dispose(); int Add(int x, int y); } [ClassInterface()] [Guid("16D9A0AD-66B3-4A8A-B6C4-67C9ED0F4BE4")] [ProgId("")] public class ComToPython: IMyClass { public void Initialize() { // nothing to do } public void Dispose() { // nothing to do } public int Add(int x, int y) { return x + y; } } }
GUID using VS2010 comes with tools to generate, Tools - Create GUID, click to copy the two GUIDs were placed before the two class names
Note: Click New GUID to copy the newly created GUID:
Finally F6 compiles to generate the solution, which will be generated in the Debug directory of your project:
Final step to register COM components to the system
Start menu - open VS 2010 with CMD command window (administrator privileges) locate under the folder
Execute: gacutil /i add dll to global cache
Execute: Register dll to system
How to callCOM dllsubassemblies
I'm using Python 2.7, IDE with PyCharm 2017.1, PyCharm new - project ComToPython, new project py file
Setup - add two dependent libraries:
Add the installation of the pywin32 and comtypes dependency libraries to correspond to the two later ways of calling COM components:
After the dependencies are installed, there will be a win32com folder in the site-packages directory of the Python installation directory, double-click the C:\Python27\Lib\site-packages\win32com\client\ folder.
Select ComToPython and click OK!
Then copy the COM component generated by VS2010 above to the PyCharm ComToPython project folder:
Write python code to call COM dll:
#!/usr/bin/env python # -*- coding: utf-8 -*- a=1 b=2 print "Method I:" from import Dispatch dll = Dispatch("") result = (a, b) print "a + b = " + str(result) print "Methodology II:" import dll = ('') result = (a, b) print "a + b = " + str(result)
Run the code and the execution results are as follows:
This is the whole process of calling C# COM Dll in Python.
This Python call C# Com dll component combat tutorial is all I have to share with you, I hope to be able to give you a reference, but also hope that you can support me more.