class testDll(object): def __init__(self): self.dll = CDLL("testa.dll")1、python传入指针,DLL将指针赋值,传回python: 1.1python部分代码:
def transToHexFromBytearray(dataarray): '''从bytearray转换成HEX''' return "".join(["%.2X" % c for c in dataarray]) def transToBytearrayFromHex(hexStr, length=None): '''从HEX字符串转换成bytearray''' return bytes.fromhex(hexStr) def testa(self,testInt, testStr): testStr = transToBytearrayFromHex(testStr)#输入 returnInt = c_int() #返回 returnData = create_string_buffer(256) #创建返回固定大小的字符缓存 ret = self.dll.TransferChannel(testStr, testInt, byref(returnInt),byref(returnData)) return ret, returnInt, transToHexFromBytearray(returnData.raw)1.2C语言接口代码:
extern "C" testa int testa(char *testStr, int testInt, int &returnInt, char *returnData);1.3调用:
#1.加载dll testDll=testDll() #调用方法 ret, returnInt,returnData=tstDll.testa(1,"AABBCC")2.传入空结构体并返回
2.1python部分代码:
class OemInfo(Structure): _fields_ = [("oemId", c_char * 21), ("carType", c_char * 2)] def getOemList(self): msg = create_string_buffer(128) lens = c_int(1) oemInfo1 = OemInfo() # 初始化结构体 ret = self.dll.testStructure(byref(oemInfo1), byref(lens), byref(msg)) if (ret != 0): print(str(msg.value)) return ret, None, None, None return ret, oemInfo1, lens, msg
2.2C语言接口代码:
typedef struct _tag_OEM_INFO { char oemId[21]; char carType[2]; }OemInfo; extern "C" testa int testStructure(OemInfo *arrOem, int &len, char* msg);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)