这是module.py script
,它加载包含io.c
和io.h
文件的erf_utils_io_D.dll
我成功地加载了库并传递了ctype参数,如下所示
c_int, c_float, POINTER(c_int), POINTER(c_Float)
模块.py
//python code module.py
import sys
from ctypes import *
#load the required library and make sure the folder where erf_utils_io_D.dll is present
dll = CDLL('D:\\erf_utils_python\\erf_utils_io.dll')
getContourResults = dll.getContourResults
class Utility(object):
def __init__(self):
print('i am inside init')
self.stagename = "post"
self.stateids = (c_int * 1) (2934)
self.stateidcount = 1
self.entityidcount = 1
self.entityid = (c_int * 1) (1)
self.entitytype = "FPM"
self.variablecount = 1
self.ores = [1.0]
self.filename ='allinone_RESULT.erfh5'
#This is how char** is treted in python for variablegroup
self.variablegroup = ["FPM_Mach_Number"]
self.string_length = len(self.variablegroup)
self.select_type = (c_wchar_p * self.string_length)
self.select = self.select_type()
for key, item in enumerate(self.variablegroup):
self.select[key] = item
#This is how char** is treated infor variable
self.variable = ["FPM_Mach_Number"]
self.var_len = len(self.variable)
self.var_type = (c_wchar_p * self.var_len)
self.variable_list = self.var_type()
for key, item in enumerate(self.variable):
self.variable_list[key] = item
def run(self):
getContourResults.argtypes = (POINTER(c_char_p), POINTER(c_char_p), c_int, POINTER(c_int),
c_int, POINTER(c_int), POINTER(c_char), c_int, self.select_type ,
self.var_type, POINTER(c_float))
getContourResults.restype = (c_int)
err = getContourResults(self.filename, self.stagename, self.stateidcount,
self.stateids, self.entityidcount,self.entityid, self.entitytype, self.variablecount, self.select, self.variable_list, self.ores)
reader = Utility()
reader.run()
code.cpp看起来像这样
extern "C"
{
#endif
__declspec(dllexport) int getContourResults(char* iFilename, char* iStagename, int iStateidCnt, int* Stateids,
int iEntityIdCount, int* iEntityids, char* iEntityType,
int iVariablecnt, char** iVariablegroup, char** ivariable,
float* oResults);
}
请告诉我如何将参数从python脚本传递到io.c中的方法
没有迹象表明该功能的实现,所以我做了一些假设。通过这个例子,您应该能够适应您的实际用例。主要修复方法是
.argtypes
更正和使用c_char_p
代替c_wchar_p
以及一个简单的帮助函数,用于将列表转换为ctypes数组以下是关于类型的一些说明:
c_char_p
==char*
在C中。传递一个字节字符串,例如b'string'
李>c_wchar_p
==wchar_t*
在C中。传递一个Unicode字符串,例如'string'
李>POINTER(c_char_p)
==char**
在C中POINTER(c_float)
==float*
在C中。创建存储(c_float()
)并传递byref
。以带有.value
成员的Python整数形式访问返回的浮点李>测试.cpp
test.py
输出:
相关问题 更多 >
编程相关推荐