1

I've figured out how to handle pointer arguments to standard types like int using %apply in my interface file. I'm still having trouble using a function which takes a pointer to an array of structures as its argument.

I've read the Swig 3.0 documentation about unbound arrays, structs, and making your own typemaps, but the documentation also says in most cases you shouldn't need to create a typemap.

Could someone clear up for me if I really do need a typemap, or if there is another way of getting the below method working in python?

Relevant C Code Being Wrapped

typedef struct _DRIVE
{
    int nDriveId;
    DRIVE_TYPE nDriveType;
    DEV_STATUS sStatus;
    char strDriveName[64];
    char strSerialNumber[24];
    char strFirmwareRev[16];
    char strModelNumber[48];
    unsigned long long ullTotalCapacity;
    int nSMARTEnabled;
    DRIVERINFO Driver;

    union
    {
        SMARTATTRIBUTE smartAttributes[MAX_SUPPORTED_ATTRIBUTES];
        NVME_SMART_DATA sSmartData;
        SAS_SMART_DATA sSASSmartData;
    };

    SMARTTHRESHOLD smartThresholds[MAX_SUPPORTED_ATTRIBUTES];

    union
    {
        CLIENT_DRIVE_INFO ClientDriveInfo;
        NVME_DRIVE_INFO NVMEDriveInfo;
        SAS_DRIVE_INFO SASDriveInfo;
    };
} DRIVE, *PDRIVE;

The method with the pointer to an array of structures (pDrives).

STATUS GetAllDriveInfo(PDRIVE pDrives, int *nMaxDrives, DRIVE_TYPE type, STATUS *pDriveStatus);

Swig interface file:

%module api_python64
%include "typemaps.i"

%{
/* Include the headers in the wrapper code */
#include "API-STATUS.h"
#include "API.h"
%}

/* Typemaps */
%apply int *INOUT { int *nMaxDrives };

/* Parse the header files to generate wrappers */
%include "STATUS.h"
%include "API.h"

Currently anything I try passing in Python as the first argument gives me:

TypeError: in method 'GetAllDriveInfo', argument 1 of type 'PDRIVE'

  • use the `array_functions(struct _DRIVE, _DRIVEArray)` for generating functions for creating an array, setting and getting structures from the array. You need no fancy typemaps. – Jens Munk Jul 18 '16 at 19:24
  • Would you mind giving an example? .i file, using it in python etc.? – user1927913 Jul 19 '16 at 20:23
  • See e.g. http://stackoverflow.com/questions/11998369/swig-python-structure-array – Jens Munk Jul 20 '16 at 05:54

0 Answers0