简体   繁体   English

有关从已加载的DLL调用函数的基本Python问题

[英]Basic Python questions about calling functions from a loaded DLL

I am asking this questions after many hours of searching and trying various examples, but I can not seem to call a function from a loaded DLL. 经过数小时的搜索和尝试各种示例后,我要问这个问题,但似乎无法从已加载的DLL调用函数。 I think if someone could show me one example, I could understand what I am doing wrong, and make some progress. 我认为,如果有人可以给我一个例子,我就能理解我做错了什么,并取得一些进展。

First, using Python 3.3.3, I can load the DLL, as such: 首先,使用Python 3.3.3,我可以这样加载DLL:

import ctypes
ftdi=ctypes.cdll.LoadLibrary('C:\\Python33\\DLLs\\FTCJTAG.dll')

And I can call a function that does not require any parameters, and get a number back that makes sense. 我可以调用一个不需要任何参数的函数,并获取一个有意义的数字。 But after many tries, I can not figure out how to pass a parameter to a function (ie, pointer, strings, numbers). 但是经过多次尝试,我无法弄清楚如何将参数传递给函数(即指针,字符串,数字)。

For example, from FTCJTAG.h , we have this: 例如,从FTCJTAG.h ,我们有:

FTCJTAG_API
FTC_STATUS WINAPI JTAG_GetDllVersion(LPSTR lpDllVersionBuffer, DWORD dwBufferSize);

And from the DLL programmers guide: 并从DLL程序员指南中:

FTC_STATUS JTAG_GetDllVersion(LPSTR lpDllVersionBuffer, DWORD dwBufferSize)

This function returns the version of this DLL.
Parameters

lpDllVersionBuffer Pointer to the buffer that receives the version of this DLL. 
The string will be NULL terminated.

dwBufferSize Length of the buffer created for the device name string. Set buffer 
length to a minimum of 10 characters.

Return Value

Returns FTC_SUCCESS if successful, otherwise the return value will be one of the
following error codes:

FTC_NULL_DLL_VERSION_BUFFER_POINTER
FTC_DLL_VERSION_BUFFER_TOO_SMALL*

So, I try this: 因此,我尝试这样做:

version_string = ctypes.c_char * 10
string_ptr=version_string()
ftdi.JTAG_GetDllVersion(string_ptr,ctypes.c_long(10))

And I get this back: 我得到这个:

Traceback (most recent call last):
File "C:\Python33\Scripts\FTDI.py", line 5, in <module>
ftdi.JTAG_GetDllVersion(string_ptr,ctypes.c_long(10))
ValueError: Procedure called with not enough arguments (8 bytes missing) or wrong
calling convention

I have tired many different ways to pass a pointer to the function, or the length of the string, and still no success. 我已经厌倦了将指针传递给函数或字符串长度的许多不同方法,但仍然没有成功。

Can someone please provide an example of how to pass these parameters to this function? 有人可以举例说明如何将这些参数传递给该函数吗? I am sure once I see a working example, I can figure out how to call the rest of the functions in this DLL. 我确定一旦看到一个可行的示例,就可以弄清楚如何调用此DLL中的其余函数。

Thanks for your time! 谢谢你的时间!

EDIT: 编辑:

@ Daniel: Thanks for the suggestion to use 'windll' instead of 'cdll', as now the function call above executes without error. @ Daniel:感谢您建议使用“ windll”而不是“ cdll”,因为现在上面的函数调用可以正确执行。 However, if I try to call another function, I am getting a different error (and one I have also searched long and hard to solve): 但是,如果我尝试调用另一个函数,则会遇到另一个错误(并且我也搜索了很长时间,并且很难解决):

FTC_STATUS WINAPI JTAG_GetNumDevices(LPDWORD lpdwNumDevices);

To call this function, I am doing this: 要调用此函数,我正在这样做:

x = ctypes.c_ulong
x_ptr = x()
ftdi.JTAG_GetNumDevices(x_ptr)

And the error is this: 错误是这样的:

Traceback (most recent call last):
File "C:\Python33\Scripts\FTDI.py", line 9, in <module>
ftdi.JTAG_GetNumDevices(x_ptr)
OSError: exception: access violation writing 0x00000000

What I gather is I am not passing the proper address of the pointer to the function, but again, after much searching, not finding an answer. 我所收集的是,我没有将指针的正确地址传递给函数,而是经过大量搜索后仍未找到答案。 I am sure it is gong to be something simple . 我敢肯定,做些简单的事情是很容易的。 . . :) :)

Thanks again for your time! 感谢你的宝贵时间!

The documentation for ctypes says that that exception is also raised if you're trying to call it with the wrong calling convention. ctypes文档指出,如果尝试使用错误的调用约定来调用该异常,也会引发该异常。 So you might need to load it with windll instead of cdll . 因此,您可能需要使用windll而不是cdll进行加载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM