关于从加载的DLL调用函数的基本Python问题 [英] Basic Python questions about calling functions from a loaded DLL

查看:1777
本文介绍了关于从加载的DLL调用函数的基本Python问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在多个小时的搜索和尝试各种示例后,我提出这个问题,但是似乎不能从加载的DLL调用函数。我想如果有人能给我一个例子,我可以明白我做错了什么,并取得一些进展。



首先,使用Python 3.3.3,我可以加载DLL,因此:

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

我可以调用函数不需要任何参数,并获得一个有意义的数字。但经过很多尝试,我无法弄清楚如何将参数传递给函数(即指针,字符串,数字)。



例如,从 FTCJTAG.h ,我们有:

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

从DLL程序员指南:

  FTC_STATUS JTAG_GetDllVersion(LPSTR lpDllVersionBuffer,DWORD dwBufferSize)

此函数返回此DLL的版本。
参数

lpDllVersionBuffer指向接收此DLL版本的缓冲区的指针。
字符串将为NULL终止。

dwBufferSize为设备名称字符串创建的缓冲区长度。将缓冲区
的长度设置为至少10个字符。

返回值

如果成功,返回FTC_SUCCESS,否则返回值将是以下错误代码中的
之一:

FTC_NULL_DLL_VERSION_BUFFER_POINTER
FTC_DLL_VERSION_BUFFER_TOO_SMALL *

所以,我尝试这样:

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

我得到这个:

 追溯(最近的最后一次呼叫):
文件C:\Python33\Scripts\FTDI.py,第5行,< module>
ftdi.JTAG_GetDllVersion(string_ptr,ctypes.c_long(10))
ValueError:没有足够的参数调用(没有8个字节)或错误的过程
调用约定

我已经厌倦了许多不同的方法来传递一个指针到函数,或者字符串的长度,仍然没有成功。 >

有人可以提供一个如何传递这些参数到这个函数的例子?我确定一旦我看到一个工作的例子,我可以弄清楚如何调用这个DLL中的其他功能。



感谢你的时间!



编辑:



@ Daniel:感谢您使用'windll'而不是'cdll'的建议,如现在的功能上面的调用执行没有错误。但是,如果我尝试调用另一个函数,我会收到一个不同的错误(我也搜索了很长时间才能解决):

  FTC_STATUS WINAPI JTAG_GetNumDevices(LPDWORD lpdwNumDevices); 

要调用此函数,我在这样做:

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

错误是这样的:

 追溯(最近的call last):
文件C:\Python33\Scripts\FTDI.py,第9行在< module>
ftdi.JTAG_GetNumDevices(x_ptr)
OSError:异常:访问冲突写入0x00000000

我收集的是我没有传递指向该函数的正确地址,但是经过多次搜索之后,没有找到答案。我相信这是一个简单的锣。 。 。 :)



再次感谢您的时间!

解决方案

一个href =http://docs.python.org/3/library/ctypes.html#calling-functions =nofollow> ctypes的文档表示,如果你是试图用错误的调用约定来调用它。所以你可能需要加载 windll 而不是 cdll


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. 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:

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_API
FTC_STATUS WINAPI JTAG_GetDllVersion(LPSTR lpDllVersionBuffer, DWORD dwBufferSize);

And from the DLL programmers guide:

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.

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. 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. So you might need to load it with windll instead of cdll.

这篇关于关于从加载的DLL调用函数的基本Python问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆