我一直在尝试使用 ctypes 将 digi Advanced Device Discovery 协议库与 python 一起使用。上下文:Windows 7 x64 python 2.7.5 dll 库
这是我当前的代码:
guid = (0xbf6db409,0xc83d,0x44a3,0xa3,0x6d,0x21,0x79,0x7d,0x2f,0x73,0xf9)
class ADDP():
from ctypes import Structure
class GUID(Structure):
from ctypes.wintypes import DWORD,WORD,BYTE
_fields_ = [("Data1",DWORD),
("Data2",WORD),
("Data3",WORD),
("Data4",BYTE * 8)]
def __init__(self, guid):
from ctypes import windll, c_void_p, c_byte, pointer,c_char,POINTER
from ctypes.wintypes import HANDLE
import ctypes
self.dll = windll.LoadLibrary("D:\\Lib\\addp.dll")
self.guid = self.GUID()
self.guid.Data1 = guid[0]
self.guid.Data2 = guid[1]
self.guid.Data3 = guid[2]
self.guid.Data4 = (c_byte * 8)(guid[3],guid[4],guid[5],guid[6],guid[7],guid[8],guid[9],guid[10])
addpopen = self.dll[1]
addpopen.argtypes = [POINTER(self.GUID),]
addpopen.restype = c_void_p
#print addpopen.restype
self.handler = addpopen(pointer(self.guid))
if self.handler == None:
raise RuntimeError()
self.opened = False
else:
self.opened = True
def isOpen(self):
return self.opened
def Discover(self):
from ctypes import c_int
srch = self.dll[6]
srch.restype = c_int
print srch(self.handler,10,10)
def Close(self):
close = self.dll[3]
close.restype = None
self.opened = False
#print close(self.handler)
conn = ADDP(guid)
#print conn.handler
conn.Discover()
#conn.Close()
print conn.handler
我搜索了很多关于如何处理从 ac 函数返回的句柄,但找不到太多关于它的信息,我阅读了一段时间的 ctypes 文档,然后也检查了头文件..
句柄在头文件中定义
typedef void* addp_handle_t;
所以我假设我必须将'restype'设置为'c_void_p',该函数总是返回'None'它在头文件中指定它在发生错误时返回'None',否则它返回ADDP会话的句柄。
另一件事,这个dll不按名称导出函数......我不得不或多或少地猜测参数中的预期字节是什么函数。
对此有什么想法吗?我在谷歌代码上找到了一个项目,但显然它并没有走远......如果您需要任何其他详细信息,请说