2012-11-22 110 views
1

我是新手到JNA和麪臨的一個問題相對於指針的映射JNA:指向的數組的結構:無效指針

本地方法:

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

JNA方法:

  • INT PDUStartComPrimitive(INT HMOD,INT hCLL,INT CoPType,整數
    CoPDataSize,字節[] pCoPData,PDU_COP_CTRL_DATA.ByReference
    pCopCtrlData,指針pCoPTag,IntByReference phCoP) ;

天然結構(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映射:

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 { 
    }; 
} 

當我執行Java中的PDUStartComPrimitive方法,有一個看看underlyng DLL紀錄我請參閱PDU_COP_CTRL_DATA strucutre字段* pExpectedResponseArray,我得到Invalid * PDU_EXP_RESP_DATA指針。

我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); 

我懷疑這可能是因爲錯誤的JNA映射。你能幫我提出一些想法/建議嗎? 謝謝

回答

0

Structure內的原語數組被解釋爲內聯數組,這會導致聚合(數組)類型的字段。您的本地結構表明這些字段的指針類型,因此您需要使用指針,指針類型或NIO緩衝區。