I am trying to call a WinDll from Python, which generally works fine for simple functions. My issue is that I need to pass in a pointer to a complex structure, which is not working. I am not able to define the arguments of the WinDll function.
The function declaration in c and the data types look as followed:
typedef struct VBLFileStatistics_t
{
    DWORD     mStatisticsSize;                   /* sizeof (VBLFileStatistics) */
    BYTE      mApplicationID;                    /* application ID */
    BYTE      mApplicationMajor;                 /* application major number */
    BYTE      mApplicationMinor;                 /* application minor number */
    BYTE      mApplicationBuild;                 /* application build number */
    ULONGLONG mFileSize;                         /* file size in bytes */
    ULONGLONG mUncompressedFileSize;             /* uncompressed file size in bytes */
    DWORD     mObjectCount;                      /* number of objects */
    DWORD     mObjectsRead;                      /* number of objects read */
} VBLFileStatistics;
( BOOL)   BLGetFileStatistics( HANDLE hFile, VBLFileStatistics* pStatistics);
Loading and getting the handle works fine.
import sys
import ctypes 
from ctypes import wintypes
GENERIC_READ         = wintypes.DWORD(0x80000000) 
INVALID_HANDLE_VALUE = -1 
class VBLFileStatistics(ctypes.Structure):
        _fields_ = [
        ("mStatisticsSize", wintypes.DWORD),
        ("mApplicationID", wintypes.BYTE),
        ("mApplicationMajor", wintypes.BYTE),
        ("mApplicationMinor", wintypes.BYTE),
        ("mApplicationBuild", wintypes.BYTE),
        ("mFileSize", ctypes.c_ulonglong),
        ("mUncompressedFileSize", ctypes.c_ulonglong),
        ("mObjectCount", wintypes.DWORD),
        ("mObjectsRead", wintypes.DWORD)]
lib = ctypes.WinDLL('myDll.dll')
#( HANDLE) BLCreateFile( LPCSTR lpFileName, DWORD dwDesiredAccess);
lib.BLCreateFile.restype  = wintypes.HANDLE
lib.BLCreateFile.argtypes = [wintypes.LPCSTR, wintypes.DWORD]
handle = lib.BLCreateFile(b'test.blf',GENERIC_READ)
    if(ctypes.c_int(handle).value == INVALID_HANDLE_VALUE):
        print("Error occured reading the file!!!")
        sys.exit()
#( BOOL)   BLGetFileStatistics( HANDLE hFile, VBLFileStatistics* pStatistics);
lib.BLGetFileStatistics.restype  = wintypes.BOOL
lib.BLGetFileStatistics.argtypes = [wintypes.HANDLE, ctypes.pointer(VBLFileStatistics)]
stats_obj = VBLFileStatistics()
success = lib.BLGetFileStatistics(handle, ctypes.pointer(stats_obj))
I am getting the following error message:
Traceback (most recent call last):
  File "parser.py", line 104, in <module>
    lib.BLGetFileStatistics.argtypes = [wintypes.HANDLE, ctypes.pointer(VBLFileStatistics)]
TypeError: _type_ must have storage info
I also tried setting argtypes to a void pointer  (wintypes.LPCVOID) and then passing a pointer to stats_obj for BLGetFileStatistics, which did not cause an error, but also did not work, since the stats_obj was still empty after the call.
User contributions licensed under CC BY-SA 3.0