Return array, ctypes

0

I am a really newbie using ctypes. I am loading a windows dll file, and trying to read the array GetDfuFileInfo returns. I actually ended up on loading the file in ctypes because I dont know how to pass a dfu_file* pointer to the GetDfuFileInfo function. Is this even possible?

C++ functions example usage:

dfu_file* TheFile = ReadDfuFile(const char*);
dfu_file_info* Info = GetDfuFileInfo(dfu_file*);

Python code:

lib = ctypes.WinDLL('dfulib.dll')
func = lib.ReadDfuFile
func.restype = ctypes.c_void_p
func.argtypes = [ctypes.c_char_p]
func("file.dvu")

edit

Also tried this:

lib = ctypes.WinDLL('dfulib.dll')
func = lib.ReadDfuFile
func.restype = ctypes.c_void_p
func.argtypes = [ctypes.c_char_p]

FileInfo = lib.GetDfuFileInfo
FileInfo.argtypes = [ctypes.c_void_p]
FileInfo.restype = None

s = func(r'file.dvu')
s = ctypes.cast(s, ctypes.c_char_p)

print FileInfo(s.value)

Produces the exception:

WindowsError: exception: access violation reading 0x00000016

Any kind of help will be appreciated.

python
c++
ctypes
asked on Stack Overflow Jun 19, 2014 by Lucas • edited Jun 19, 2014 by Lucas

1 Answer

-1

Sample code looks, This code invokes GetFileAttributesA function from kernel32.dll

>>> from ctypes import *
>>> windll.LoadLibrary("kernel32.dll")
<WinDLL 'kernel32.dll', handle 77260000 at 2241ad0>
>>> windll.kernel32
<WinDLL 'kernel32', handle 77260000 at 225c2f0>
>>> windll.kernel32.GetFileAttributesA("c:/test/1.txt")
32

You can refer to https://docs.python.org/2/library/ctypes.html for details

answered on Stack Overflow Jun 19, 2014 by PankajM

User contributions licensed under CC BY-SA 3.0