Search code examples
javajna

ProJNA Data type mapping


Question below has been modified to narrow down the issue,

I am trying to use a native dll from Java using JNA. I am new to both Java and JNA. For a specific call to one of the dll exported functions, it returns invalid parameters.

Here are the structs ,

PDU_RSC_DATA is a struct which references another struct PDU_PIN_DATA,

typedef struct {
UNUM32 BusTypeId; 
UNUM32 ProtocolId;
UNUM32 NumPinData;
PDU_PIN_DATA *pDLCPinData;
} PDU_RSC_DATA;

This is how the PDU_PIN_DATA struct looks,

typedef struct {
UNUM32 DLCPinNumber; 
UNUM32 DLCPinTypeId; 
} PDU_PIN_DATA;

Corresponding JNA Mapping,

public class PDU_RSC_DATA extends Structure{ 

public int BusTypeId; 
public int ProtocolId; 
public int NumPinData; 
public Pointer pDLCPinData; 

}

public class PDU_PIN_DATA extends Structure implements Structure.ByReference{

public int DLCPinNumber;
public int DLCPinTypeId;

public PDU_PIN_DATA(){

}

public PDU_PIN_DATA(Pointer ptr_PDU_PIN_DATA) {
    super(ptr_PDU_PIN_DATA);
    read();
}

}

Native method -> Modified for easy readability,

int _methodXYZ(PDU_RSC_DATA* obj);

Java Invocation -> showing only required snippet,

PDU_RSC_DATA obj = new PDU_RSC_DATA();
_methodXYZ(obj);

Please let me know if the above mapping of struct* within the struct is correct. From the DLL log I notice that the PDU_PIN_DATA pDLCPinData is having an invalid value.


Solution

  • The DLL specification was incorrect. PDU_PIN_DATA *pDLCPinData was a pointer to arrays of structs instead of pointer to struct. thats why technomage's suggestion din work. Its an issue from my end. Thanks again technomage.

    Here is how I assign values to the struct array and it works fine. I have modified the PDU_RSC_DATA structure in the question as well.

        PDU_RSC_DATA pRscData = new PDU_RSC_DATA();
        pRscData.BusTypeId = 1;
        pRscData.ProtocolId = 7;
        pRscData.NumPinData = 2; 
    
        PDU_PIN_DATA pDLCPinData = new PDU_PIN_DATA();
    
        PDU_PIN_DATA[] pDLCPinDataArray= (PDU_PIN_DATA[])pDLCPinData.toArray(pRscData.NumPinData);
    
        pDLCPinDataArray[0].DLCPinNumber = 6;
        pDLCPinDataArray[0].DLCPinTypeId = 1;
        pDLCPinDataArray[1].DLCPinNumber = 14;
        pDLCPinDataArray[1].DLCPinTypeId = 2;
    
        pDLCPinData.autoWrite();
    
        Pointer ptr_pDLCPinData = pDLCPinData.getPointer();
        pRscData.pDLCPinData = ptr_pDLCPinData;