大家好,我需要用python来调用C#的.dll,一直在研究http://blog.csdn.net/xugangjava/article/details/7202356这篇文章,现在碰到了一个问题,请大家帮忙出出主意,谢谢。
首先是这篇文章中的最末尾,命令行中有一条“d.EncryptMd5('dsaf')”(如图1)。请问这个"d."是从何而来?之前也没有看到任何类例化成了"d"?
图1:
其次是我在调用由C# COM生成的.py文件中的方法时,出现了“CoClassBaseClass”类能正常例化,而“DispatchBaseClass”无法正常例化的情况(如图2和图3),请问这是为什么?如何才是正确的调用方法?
图2:
图3:
以下是用makepy转化C# COM生成的.py:
# -*- coding: mbcs -*-
# Created by makepy.py version 0.5.01
# By python version 2.7.4 (default, Apr 6 2013, 19:54:46) [MSC v.1500 32 bit (Intel)]
# From type library 'testCom.tlb'
# On Wed Apr 13 16:58:28 2016
''
makepy_version = '0.5.01'
python_version = 0x20704f0
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('{95A0DC02-9D6B-4A40-A78A-C65357E749F5}')
MajorVersion = 1
MinorVersion = 0
LibraryFlags = 8
LCID = 0x0
from win32com.client import DispatchBaseClass
class IMyClass(DispatchBaseClass):
CLSID = IID('{88DB152F-BC26-43C9-BED0-B6CDE14D85CF}')
coclass_clsid = None
def Add(self, x=defaultNamedNotOptArg, y=defaultNamedNotOptArg):
return self._oleobj_.InvokeTypes(1610743808, LCID, 1, (3, 0), ((3, 1), (3, 1)),x
, y)
_prop_map_get_ = {
}
_prop_map_put_ = {
}
def __iter__(self):
"Return a Python iterator for this object"
try:
ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
except pythoncom.error:
raise TypeError("This object does not support enumeration")
return win32com.client.util.Iterator(ob, None)
class _ComTest(DispatchBaseClass):
CLSID = IID('{8069ED53-4C67-3611-BB38-AB9721C851EB}')
coclass_clsid = IID('{20CEDF1F-BB34-4D68-B657-3884B6E84C4C}')
_prop_map_get_ = {
}
_prop_map_put_ = {
}
def __iter__(self):
"Return a Python iterator for this object"
try:
ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
except pythoncom.error:
raise TypeError("This object does not support enumeration")
return win32com.client.util.Iterator(ob, None)
class _Object(DispatchBaseClass):
CLSID = IID('{65074F7F-63C0-304E-AF0A-D51741CB4A8D}')
coclass_clsid = IID('{20CEDF1F-BB34-4D68-B657-3884B6E84C4C}')
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))
def __iter__(self):
"Return a Python iterator for this object"
try:
ob = self._oleobj_.InvokeTypes(-4,LCID,3,(13, 10),())
except pythoncom.error:
raise TypeError("This object does not support enumeration")
return win32com.client.util.Iterator(ob, None)
from win32com.client import CoClassBaseClass
# This CoClass is known by the name 'testCom.ComTest'
class ComTest(CoClassBaseClass): # A CoClass
CLSID = IID('{20CEDF1F-BB34-4D68-B657-3884B6E84C4C}')
coclass_sources = [
]
coclass_interfaces = [
_ComTest,
_Object,
]
default_interface = _ComTest
IMyClass_vtables_dispatch_ = 1
IMyClass_vtables_ = [
(( u'Add' , u'x' , u'y' , u'pRetVal' , ), 1610743808, (1610743808, (), [
(3, 1, None, None) , (3, 1, None, None) , (16387, 10, None, None) , ], 1 , 1 , 4 , 0 , 28 , (3, 0, None, None) , 0 , )),
]
_ComTest_vtables_dispatch_ = 1
_ComTest_vtables_ = [
]
_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 = {
'{20CEDF1F-BB34-4D68-B657-3884B6E84C4C}' : ComTest,
'{88DB152F-BC26-43C9-BED0-B6CDE14D85CF}' : IMyClass,
'{8069ED53-4C67-3611-BB38-AB9721C851EB}' : _ComTest,
'{65074F7F-63C0-304E-AF0A-D51741CB4A8D}' : _Object,
}
CLSIDToPackageMap = {}
win32com.client.CLSIDToClass.RegisterCLSIDsFromDict( CLSIDToClassMap )
VTablesToPackageMap = {}
VTablesToClassMap = {
'{88DB152F-BC26-43C9-BED0-B6CDE14D85CF}' : 'IMyClass',
'{8069ED53-4C67-3611-BB38-AB9721C851EB}' : '_ComTest',
'{65074F7F-63C0-304E-AF0A-D51741CB4A8D}' : '_Object',
}
NamesToIIDMap = {
'IMyClass' : '{88DB152F-BC26-43C9-BED0-B6CDE14D85CF}',
'_ComTest' : '{8069ED53-4C67-3611-BB38-AB9721C851EB}',
'_Object' : '{65074F7F-63C0-304E-AF0A-D51741CB4A8D}',
}