Вопрос

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top