希望可以聪明点 2017-08-23 01:56 采纳率: 0%
浏览 2568
已结题

python 调用dll如何返回多个值

import scipy.io as scio
import os
import ctypes
import datetime
import numpy as np

#读取dll文件
#这里的地址目录是HessianFilter.dll所在的文件夹
start = datetime.datetime.now()
cur_path = os.path.dirname(r'C:\Users\Administrator\Desktop\code_and_data\code\HessianFilter\')
dll_path = os.path.join(cur_path,'HessianFilter.dll')
print dll_path
dll = ctypes.windll.LoadLibrary(dll_path)

#读取mat文件,Mat文件所在的文件夹中读取
matPath = r'C:\Users\Administrator\Desktop\code_and_data\data\Nodule19664.mat'
imgData = scio.loadmat(matPath)

#从mat文件中我们取到了整个三维数组
imgDataArray = imgData['imagetest1']

#得到数组文件的参数:宽,高,层数
widthSrc,heightSrc,sliceNumSrc = imgDataArray.shape

#声明一个三维的c_float类型的数组,用于存放mat数据,并将数据转化为c_float
imgDataArray_p = (((ctypes.c_float*sliceNumSrc)*heightSrc)*widthSrc)()

for i in range(widthSrc):
for j in range(heightSrc):
for k in range(sliceNumSrc):
imgDataArray_p[i][j][k] = ctypes.c_float(imgDataArray[i][j][k])

imgDataP = ctypes.POINTER(ctypes.c_float)(imgDataArray_p)
print '---------->'
#需要再声明两个返回值
HessianDot = (ctypes.c_float*(widthSrc*heightSrc*sliceNumSrc))()
HessianLine = (ctypes.c_float*(widthSrc*heightSrc*sliceNumSrc))()

HessianDot_p = ctypes.POINTER(ctypes.c_float)(HessianDot)
HessianLine_p = ctypes.POINTER(ctypes.c_float)(HessianLine)

#定义一个常数
sigma = ctypes.c_float(8)

#定义一个三维数组
imgSize = [widthSrc,heightSrc,sliceNumSrc]
imageSize = (ctypes.c_float*len(imgSize))(*imgSize)

#定义一个指向三维数组的指针
imageSizeP = ctypes.POINTER(ctypes.c_float)(imageSize)

#这个就是调用dll中的函数了
dll.RunHessianMultiThread(ctypes.byref(imgDataArray_p),sigma,ctypes.byref(imageSizeP),ctypes.byref(HessianDot_p),ctypes.byref(HessianLine_p),4)
print '--调用后-点数据--'
print HessianDot_p
print HessianDot_p[0:24]
#print HessianDot_p.contents
print '--调用后-线数据--'
print HessianLine_p
print HessianLine_p[0:24]
#print HessianLine_p.contents
print '<---------Over------------->\n\n'
print datetime.datetime.now()-start

  • 写回答

1条回答

  • threenewbee 2017-08-23 02:32
    关注

    一般来说函数要返回多个值,要么参数中使用指针,要么返回结构体或者数组

    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型