Access C structure from python

c, ctypes, python, python-3.x, structure

Solution

Your structure declaration is wrong. It should be:

class DEVINFO(Structure):
    _fields_ = [
        ("szDeviceName", c_char*wintypes.MAX_PATH),
        ("szPCSCName", c_char*wintypes.MAX_PATH),
        ("bPassedFilter", wintypes.BOOL),
        ("bUpdatePassed", wintypes.BOOL),
        ("dwUpdateOrder", wintypes.DWORD),
        ("dwPnp_ID", wintypes.DWORD),
        ("dwFWVersion", wintypes.DWORD),
        ("pDevExtension", POINTER(DEVEXTENSION))
    ]

You must also pass a pointer to the `DEVINFO` struct when you call `FIOCreateDeviceInfoList()`. I'd do it like this:

funcCreateList = lib.FIOCreateDeviceInfoList(byref(devInfo))

As @eryksun helpfully points out, adding

lib.FIOCreateDeviceInfoList.argtypes = [POINTER(DEVINFO)]

before the call to `FIOCreateDeviceInfoList()` will make `ctypes` perform runtime type checking.

Problem

I am to create a python script that will access a windows dll function. I was successful in accessing the dll and its functions. Now that, I have a c function as ``` FIOSCR331_API int FIOCreateDeviceInfoList (PDEVINFO pDevInfoSet) ``` the problem is the `PDEVINFO` structure. I must create a structure in python and access this structure. The C structure is as follows ``` typedef struct tagDEVINFO { char szDeviceName[MAX_PATH]; char szPCSCName[MAX_PATH]; BOOL bPassedFilter; BOOL bUpdatePassed; DWORD dwUpdateOrder; DWORD dwPnP_ID; DWORD dwFWVersion; PDEVEXTENSION pDevExtension; } DEVINFO, *PDEVINFO; ``` the C function is as follows ``` FIOSCR331_API int FIOCreateDeviceInfoList (PDEVINFO pDevInfoSet) { int nFIOStatus; do { if ( NULL == pDevInfoSet ) { printf("this is inside C code\n"); nFIOStatus = IDS_GENERIC_ERROR; //(200) break; } else printf ("\n%s ",pDevInfoSet->szPCSCName); }while(false); } ``` Now the Python code I implemented ``` class DEVINFO(Structure): _fields_ = [("szDeviceName",c_char_p), ("szPCSCName",c_char_p), ("bPassedFilter",c_bool), ("bUpdatePassed",c_bool), ("dwUpdateOrder",c_ulong), ("dwPnp_ID",c_ulong), ("dwFWVersion",c_ulong), ("pDevExtention",DEVEXTENSION)] lib = cdll.LoadLibrary('libFIOXXXXX.dll') print (lib) devInfo = DEVINFO() devInfo.szPCSCName = c_char_p(b"this is test") if devInfo is None: print("hi") else: print("britto") funcCreateList = lib.FIOCreateDeviceInfoList(devInfo) print (funcCreateList) ``` The Result I got is ``` britto this is inside C code 200 ``` The problem is the code goes into NULL condition always, i.e. devInfo is NULL. Why is that? NEWLY ADDED In the above python Structure DEVINFO, It contains another structure DEVEXTENSION. How will i be able to access the members of the DEVEXTENSION error?? ``` print (devInfo.pDevExtension.szName) ``` This throws: ``` AttributeError: 'LP_DEVEXTENSION" object has no attribute szName ```

Original source