调用DLL中的函数并使用数组作为参数

2024-10-06 11:52:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我从一个用C写的DLL调用一个函数。在DLL的文档中,它说函数的一个参数应该是:

an address of an array of 32-bit floating point numbers to be populated with results.

我对C不是很熟悉,有些东西告诉我这是一个C特性。我不太确定我该用什么作为我的论点。你知道吗

我正在使用ctypes。你知道吗

以下是DLL文档中的一个示例:

           float fResult;
           long lRetVal = D2R_GetSingleResult(
                             "E:\\Folder",
                             "E:\\Folder\\Proj1",
                             2001001, &fResult, 1, NULL, NULL );

Tags: of函数文档an参数addressbitfolder
2条回答

另一种方法是声明函数类型,以帮助ctypes推断所有内容本身:

D2R = ctypes.cdll.LoadLibrary("d2r.so")
D2R_GetSingleResult = D2R.D2R_GetSingleResult
D2R_GetSingleResult.argtypes = (ctypes.c_char_p, ctypes.c_char_p, ctypes.c_int, ctypes.POINTER(ctypes.c_float), ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p)
D2R_GetSingleResult.restype = ctypes.c_int
...
fResult = ctypes.c_float()
lRetVal = D2R_GetSingleResult("Folder", "Folder\\Proj1", 2001001, fResult, 1, None, None)

我需要如下定义数组参数:

(ctypes.c_float*1)()

谢谢@fukanchik的帮助,你让我走上了正确的道路!你知道吗

相关问题 更多 >