如何使用digi addp库与python

时间:2013-06-13 18:38:26

标签: python dll ctypes

我一直在尝试使用ctypes使用digi高级设备发现协议库和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

我搜索了很多关于如何处理从c函数返回的句柄,但是找不到多少关于它,我读了一段时间的ctypes文档,然后检查了头文件..

句柄在头文件中用

定义
typedef void* addp_handle_t;

所以我假设我必须将'restype'设置为'c_void_p',该函数总是返回'None' 它在头文件中指定它在发生错误时返回'None',否则它返回ADDP会话的句柄。

另一件事,这个dll不会按名称导出函数...我不得不或多或少地猜测参数中预期的字节是什么函数。

对此有何想法? 我在谷歌代码上找到了一个项目,但显然它并没有走得太远...... 如果您需要任何其他细节,请说

0 个答案:

没有答案