python调用C# Com 的dll

5 篇文章 0 订阅

dll源程序 可查看 文章<<c++调用c# dll>>

http://blog.csdn.net/xugangjava/article/details/7067413

首先 运行 工具 makepy.py

这样就可以查看 C# dll的 com导出的 py文件了


查看


# -*- coding: mbcs -*-
# Created by makepy.py version 0.5.00
# By python version 2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit (Intel)]
# From type library 'XGSharpLib.tlb'
# On Sun Jan 15 10:36:30 2012
""""""
makepy_version = '0.5.00'
python_version = 0x20504f0

import win32com.client.CLSIDToClass, pythoncom, pywintypes
import win32com.client.util
from pywintypes import IID
from win32com.client import Dispatch

# The following 3 lines may need tweaking for the particular server
# Candidates are pythoncom.Missing, .Empty and .ArgNotFound
defaultNamedOptArg=pythoncom.Empty
defaultNamedNotOptArg=pythoncom.Empty
defaultUnnamedArg=pythoncom.Empty

CLSID = IID('{B38EF2FA-4639-40BC-B97C-7908CED04FF9}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 8
LCID = 0x0

from win32com.client import DispatchBaseClass
class IComXGSharpLib(DispatchBaseClass):
	CLSID = IID('{D1965A94-0271-4C48-8AF6-2A56E256808B}')
	coclass_clsid = IID('{2302D874-18FE-4281-B329-9517F1BC8311}')

	def EncryptMd5(self, orignPwd=defaultNamedNotOptArg):
		# Result is a Unicode object
		return self._oleobj_.InvokeTypes(2, LCID, 1, (8, 0), ((8, 1),),orignPwd
			)

	def EncryptSHA1(self, orignPwd=defaultNamedNotOptArg):
		# Result is a Unicode object
		return self._oleobj_.InvokeTypes(1, LCID, 1, (8, 0), ((8, 1),),orignPwd
			)

	_prop_map_get_ = {
	}
	_prop_map_put_ = {
	}

class _Object(DispatchBaseClass):
	CLSID = IID('{65074F7F-63C0-304E-AF0A-D51741CB4A8D}')
	coclass_clsid = IID('{2302D874-18FE-4281-B329-9517F1BC8311}')

	def Equals(self, obj=defaultNamedNotOptArg):
		return self._oleobj_.InvokeTypes(1610743809, LCID, 1, (11, 0), ((12, 1),),obj
			)

	def GetHashCode(self):
		return self._oleobj_.InvokeTypes(1610743810, LCID, 1, (3, 0), (),)

	# Result is of type _Type
	def GetType(self):
		ret = self._oleobj_.InvokeTypes(1610743811, LCID, 1, (13, 0), (),)
		if ret is not None:
			# See if this IUnknown is really an IDispatch
			try:
				ret = ret.QueryInterface(pythoncom.IID_IDispatch)
			except pythoncom.error:
				return ret
			ret = Dispatch(ret, u'GetType', '{BCA8B44D-AAD6-3A86-8AB7-03349F4F2DA2}')
		return ret

	_prop_map_get_ = {
		"ToString": (0, 2, (8, 0), (), "ToString", None),
	}
	_prop_map_put_ = {
	}
	# Default property for this class is 'ToString'
	def __call__(self):
		return self._ApplyTypes_(*(0, 2, (8, 0), (), "ToString", None))
	def __unicode__(self, *args):
		try:
			return unicode(self.__call__(*args))
		except pythoncom.com_error:
			return repr(self)
	def __str__(self, *args):
		return str(self.__unicode__(*args))
	def __int__(self, *args):
		return int(self.__call__(*args))

from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'XGSharpLib.Security'
class Security(CoClassBaseClass): # A CoClass
	CLSID = IID('{2302D874-18FE-4281-B329-9517F1BC8311}')
	coclass_sources = [
	]
	coclass_interfaces = [
		IComXGSharpLib,
		_Object,
	]
	default_interface = IComXGSharpLib

IComXGSharpLib_vtables_dispatch_ = 1
IComXGSharpLib_vtables_ = [
	(( u'EncryptSHA1' , u'orignPwd' , u'pRetVal' , ), 1, (1, (), [ (8, 1, None, None) , 
			(16392, 10, None, None) , ], 1 , 1 , 4 , 0 , 28 , (3, 0, None, None) , 0 , )),
	(( u'EncryptMd5' , u'orignPwd' , u'pRetVal' , ), 2, (2, (), [ (8, 1, None, None) , 
			(16392, 10, None, None) , ], 1 , 1 , 4 , 0 , 32 , (3, 0, None, None) , 0 , )),
]

_Object_vtables_dispatch_ = 1
_Object_vtables_ = [
	(( u'ToString' , u'pRetVal' , ), 0, (0, (), [ (16392, 10, None, None) , ], 1 , 2 , 4 , 0 , 28 , (3, 0, None, None) , 0 , )),
	(( u'Equals' , u'obj' , u'pRetVal' , ), 1610743809, (1610743809, (), [ (12, 1, None, None) , 
			(16395, 10, None, None) , ], 1 , 1 , 4 , 0 , 32 , (3, 0, None, None) , 0 , )),
	(( u'GetHashCode' , u'pRetVal' , ), 1610743810, (1610743810, (), [ (16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 36 , (3, 0, None, None) , 0 , )),
	(( u'GetType' , u'pRetVal' , ), 1610743811, (1610743811, (), [ (16397, 10, None, "IID('{BCA8B44D-AAD6-3A86-8AB7-03349F4F2DA2}')") , ], 1 , 1 , 4 , 0 , 40 , (3, 0, None, None) , 0 , )),
]

RecordMap = {
}

CLSIDToClassMap = {
	'{D1965A94-0271-4C48-8AF6-2A56E256808B}' : IComXGSharpLib,
	'{2302D874-18FE-4281-B329-9517F1BC8311}' : Security,
	'{65074F7F-63C0-304E-AF0A-D51741CB4A8D}' : _Object,
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
	'{D1965A94-0271-4C48-8AF6-2A56E256808B}' : 'IComXGSharpLib',
	'{65074F7F-63C0-304E-AF0A-D51741CB4A8D}' : '_Object',
}


NamesToIIDMap = {
	'_Object' : '{65074F7F-63C0-304E-AF0A-D51741CB4A8D}',
	'IComXGSharpLib' : '{D1965A94-0271-4C48-8AF6-2A56E256808B}',
}



调用 dll中的 MD5加密方法



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
要在Python调用C#DLL,你可以使用Python的`ctypes`库。但是,直接调用C#DLL可能会有一些限制,因为C#Python使用不同的运行时环境。为了解决这个问题,你可以将C#DLL封装为一个可以在Python中使用的COM组件或者使用其他中间件。 以下是一种可能的方法: 1. 创建一个C#类库项目,并将需要暴露给Python的功能封装在该项目中。 2. 在C#项目中生成一个COM调用程序集(COM Callable Wrapper)。 3. 将生成的COM程序集导出为一个DLL文件。 4. 在Python中使用`ctypes`库加载该DLL文件,并调用其中的函数。 这里是一个简单的示例: ```c# using System; using System.Runtime.InteropServices; namespace YourNamespace { public class YourClass { public int Add(int a, int b) { return a + b; } } } ``` 1. 在Visual Studio中创建一个新的C#类库项目,将上述代码添加到类库中。 2. 生成该项目,并确保在生成选项中选择了"Register for COM interop"。 3. 在生成目录中找到生成的DLL文件(例如`YourNamespace.dll`)。 接下来,在Python中使用`ctypes`库加载并调用DLL: ```python import ctypes # 加载DLL your_dll = ctypes.WinDLL('YourNamespace.dll') # 如果是64位系统,使用ctypes.WinDLL('YourNamespace.dll') # 如果是32位系统,使用ctypes.CDLL('YourNamespace.dll') # 设置函数返回类型 your_dll.Add.restype = ctypes.c_int # 调用函数 result = your_dll.Add(2, 3) print(result) # 输出: 5 ``` 请注意,这只是一个简单的示例。在实际应用中,你可能需要处理更复杂的数据类型和参数传递方式。此外,你还需要确保系统中已安装了.NET Framework,并且PythonC#代码都是在相同的平台下编译的(例如都是32位或64位)。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值