简体   繁体   English

JNA:指向结构数组的指针:无效的指针

[英]JNA : Pointer to array of Struct : Invalid Pointer

I am a newbie to JNA and facing a problem with respect to Pointer mapping 我是JNA的新手,因此在指针映射方面遇到问题

Native Method: 本机方法:

  • EXTERNC T_PDU_ERROR PDUStartComPrimitive(UNUM32 hMod,UNUM32 hCLL,T_PDU_COPT CopType,UNUM32 CoPDataSize,UNUM8 EXTERNC T_PDU_ERROR PDUStartComPrimitive(UNUM32 hMod,UNUM32 hCLL,T_PDU_COPT CopType,UNUM32 CoPDataSize,UNUM8
    *pCopData,PDU_COP_CTRL_DATA *pCopCtrlData, void *pCopTag, UNUM32 *phCoP) * pCopData,PDU_COP_CTRL_DATA * pCopCtrlData,无效* pCopTag,UNUM32 * phCoP)

JNA Method: JNA方法:

  • int PDUStartComPrimitive(int hMod, int hCLL, int CoPType, int int PDUStartComPrimitive(int hMod,int hCLL,int CoPType,int
    CoPDataSize, byte[] pCoPData, PDU_COP_CTRL_DATA.ByReference CoPDataSize,字节[] pCoPData,PDU_COP_CTRL_DATA.ByReference
    pCopCtrlData, Pointer pCoPTag, IntByReference phCoP); pCopCtrlData,指针pCoPTag,IntByReference phCoP);

Native Structure(s): 本机结构:

typedef struct{
UNUM32 Time;
SNUM32 NumSendCycles;
SNUM32 NumReceiveCycles;
UNUM32 TempParamUpdate;
PDU_FLAG_DATA TxFlag;
UNUM32 NumPossibleExpectedResponses;
PDU_EXP_RESP_DATA *pExpectedResponseArray;
}PDU_COP_CTRL_DATA;

typedef struct{
UNUM32 ResponseType;
UNUM32 AcceptanceId;
UNUM32 NumMaskPatternBytes;
UNUM8 *pMaskData;   
UNUM8 *pPatternData;    
UNUM32 NumUniqueRespIds;
UNUM32 *pUniqueRespIds; 
}PDU_EXP_RESP_DATA;

JNA Mapping: JNA映射:

public class PDU_COP_CTRL_DATA extends Structure{       
    public int time;        
    public int numSendCycles;       
    public int numReceiveCycles;        
    public int tempParamUpdate;     
    public PDU_FLAG_DATA txFlag;        
    public int numPossibleExpectedResponses;        
    public Pointer pExpectedResponseArray;      
    public static class ByReference extends PDU_COP_CTRL_DATA implements     Structure.ByReference {
    };
}

public class PDU_EXP_RESP_DATA extends Structure{        
     public int responseType;        
     public int acceptanceId;
     public int numMaskPatternBytes;
     public byte[] pMaskData= new byte[1];
     public byte[] pPatternData = new byte[1];
     public int numUniqueRespIds;
     /* Array containing unique response identifiers. Only responses with a unique response identifier found in this array are considered, when trying to match them to this expected response. */
     public Pointer pUniqueRespIds;  
     public static class ByReference extends PDU_EXP_RESP_DATA implements Structure.ByReference {
     };
}   

When I execute the PDUStartComPrimitive method in Java and have a look at the underlyng dll log I see that for the PDU_COP_CTRL_DATA strucutre field *pExpectedResponseArray I get Invalid *PDU_EXP_RESP_DATA pointer. 当我在Java中执行PDUStartComPrimitive方法并查看底层dll日志时,我看到PDU_COP_CTRL_DATA结构字段* pExpectedResponseArray的无效* PDU_EXP_RESP_DATA指针。

My JNA code to setup the PDUStartComPrimitive execution 我的JNA代码来设置PDUStartComPrimitive执行

        byte[] sendata = new byte[requestData.length() / 2];

        int byteIndex = 0;
        for (String byteString : requestData.split(" ")) {
            sendata[byteIndex] = Byte.parseByte(byteString, 16);
            byteIndex++;
        }
        /* Setting of the expected responses ends */

        PDU_COP_CTRL_DATA.ByReference objCopCtrlData = new PDU_COP_CTRL_DATA.ByReference();
        objCopCtrlData.numPossibleExpectedResponses = 1;
        objCopCtrlData.numReceiveCycles = 1;
        objCopCtrlData.numSendCycles = 1;
        objCopCtrlData.tempParamUpdate = 0;
        objCopCtrlData.time = 0;


        PDU_EXP_RESP_DATA expRespStruct = new PDU_EXP_RESP_DATA();

        expRespStruct.acceptanceId = 0;
        expRespStruct.numMaskPatternBytes = 1;
        expRespStruct.numUniqueRespIds = 0;
        expRespStruct.pUniqueRespIds = new Pointer(0);
        expRespStruct.responseType = 0;

        byte[] mskByte = byte int[] { 0 };
        byte[] patternByte = new byte[] { 0 };
        expRespStruct.pMaskData = mskByte;
        expRespStruct.pPatternData = patternByte;

        PDU_EXP_RESP_DATA[] refArr = (PDU_EXP_RESP_DATA[]) expRespStruct.toArray(1);
        refArr[0] = expRespStruct;

        expRespStruct.autoWrite();

        objCopCtrlData.pExpectedResponseArray = expRespStruct.getPointer();

        PDU_FLAG_DATA.ByValue objTxFlagData = new PDU_FLAG_DATA.ByValue();          
        objCopCtrlData.txFlag = objTxFlagData;

        String strComAction = "SEND_RECV";
        Pointer apiTag = new NativeString(strComAction, true).getPointer();
        IntByReference phCop = new IntByReference();

        int errorCode = PDUStartComPrimitive(1, 1,0x8004, sendata.length,sendata, objCopCtrlData, apiTag, phCop);

I have a suspicion this maybe because of wrong JNA mapping. 我怀疑这可能是因为错误的JNA映射。 Could you please help me with any ideas/suggestions. 您能否提出任何想法/建议来帮助我。 Thanks 谢谢

Primitive arrays within a Structure are interpreted as inline arrays, which results in a field of aggregate (array) type. Structure中的原始数组被解释为内联数组,这导致聚合(数组)类型的字段。 Your native structure indicates a pointer type for those fields, so you need to use Pointer, PointerType, or NIO Buffer. 您的本机结构指示这些字段的指针类型,因此您需要使用Pointer,PointerType或NIO Buffer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM