我在使用DLL函数和Python时遇到麻烦。我需要为我的应用程序使用C编写的特定DLL。我无法修改DLL,但是我具有结构的定义。所以C结构是这样的:
import cgitb
cgitb.enable()
print('Content-type: text/html\r\n')
print('\r\n')
import Image, ImageDraw
import sys
import math, random
from itertools import product
from ufarray import *
ModuleNotFoundError: No module named 'Image'
args = ("No module named 'Image'",)
msg = "No module named 'Image'"
name = 'Image'
path = None
with_traceback = <built-in method with_traceback of ModuleNotFoundError
该函数的原型为:
typedef struct {
int length;
int* val;
//to access value at a row => val[row]
} Array1dInt, **Array1dIntHandle;
我已经在Python中定义了这样的类:
void __declspec(dllexport) modifywithHandle(Array1dIntHandle);
当我想使用DLL函数时,我会这样:
class Array1dInt(Structure):
_fields_=[("length",c_int),("val",c_int)]
def handle(self):
"""Return a pointer. Use this function when you need the Handle of the array structure"""
return addressof(pointer(self))
def __init__(self,vec):
"""init initialize an object of the class Array1dInt. Takes a python array as input"""
self.length=c_long(len(vec))
self.vector=np.ctypeslib.as_ctypes(np.array(vec))
self.val=c_long(addressof(self.vector))
在其他内容中,我应该在控制台中看到长度和元素。 DLL函数正确检索长度,但无法读取向量。我用指针或addressof尝试了不同的解决方案,但似乎没有任何效果。我注意到length和val不会转换为ctypes对象,而vector是。 假设DLL函数的编码正确,在Python中我在做什么错? 谢谢您的帮助