我试图解决以下问题好几天了。我的JNA程序被挂起,而特定本机函数的执行和调试显示程序控制在JNA Source中的特定位置丢失。这是详细信息。 我在DLL中有三个本机函数,我试图用JNA访问它。 它们如下
DllExport long calculatePayment(...,Protection protectionArr[PROTECTION_SIZE],
Others othersArr[OTHERS_SIZE],RC rcArr[RC_SIZE], ...);
DllExport long bufferCalculatePayment(const char *inputBuffer, char **OutputBuffer);
DllExport long fileCalculatePayment(const char *InputFile, const char *OutputFile);
对应的Java接口映射如下
public interface DLLInterface extends Library {
public DLLInterface INSTANCE = (DLLInterface) Native.loadLibrary("DLLInterface",
DLLInterface.class);
/* Others Structure is as follows.. protection and rc structure is similar with their
own initialize methods*/
public static final NativeLong othersSize = new NativeLong(20);
class Others extends Structure {
public static class ByValue extends Others implements Structure.ByValue {
}
public double amt = 0.00;
public NativeLong flag = new NativeLong();
public byte[] details = new byte[50]; //
public byte[] mDetails = new byte[50];
public NativeLong flag2 = new NativeLong();
public static Structure[] initialize() {
Others result = new Others();
Others[] resultArr = (Others[]) result.toArray(othersSize);
return resultArr;
}
public void setDetails(String dataIn) {
Arrays.fill(details, (byte) 0);
if (dataIn != null) {
String data = dataIn;
if (data.length() > 49) {
data = data.substring(0, 49);
}
byte[] bytes = data.getBytes();
System.arraycopy(bytes, 0, details, 0, bytes.length);
}
}
//other set functions here
@Override
protected List getFieldOrder() {
return Arrays.asList("amt", "flag", "details", "mDetails", "flag2");
}
}
//Protection and RC sturcutre are similar. Both structures contain fields
of the type byte[] and double.
//Prototypes for native functions
NativeLong calculatePayment(.....Structure[] protectionArr, Structure[]
othersStruct , Structure[] rcArr,.....) ;
Nativelong bufferCalculatePayment(Strubg inputBuffer, PointerByReference
OutputBuffer);
Natuvelong fileCalculatePayment(String InputFile, String OutputFile);
}
//In main function I am calling these functions as follows -
public static void main(String args[]){
DLLInterface.Others[] othersArr = (DLLInterface.Others[])
DLLInterface.Others.initialize();
DLLInterface.RC[] rcArr = (DLLInterface.RC[]) DLLInterface.RC.initialize();
DLLInterface.Protection[] protectionArr = (DLLInterface.Protection[])
DLLInterface.Protection.initialize(); //static method
...
//passing values to structures
...
//few native functions calls goes here (they get executed successfully
NativeLong y =
DLLInterface.INSTANCE.calculatePayment(....protectionArr,othersArr,rcArr, ...);
String inputBuffer = ""<?xml version= \"1.0\" ?><ROOT><REQ><TRANSACT>
<ID>12345</ID><METHOD>COMPOUND</METHOD><AMT>50000</AMT><TERM>360</TERM></TRANSACT>
</REQ></ROOT>";
NativeLong b = DLLInterface.INSTANCE.bufferCalculatePayment(inputBuffer,pRef);
DLLInterface.INSTANCE.fileCalculatePayment("InFile","OutFile");
}
对于所有三个函数,程序被挂起..无论何时终止,所以我尝试调试代码。然后我发现在调用之后 对象result = invoke(args,nativeType,allowObjects),程序控制在跟随for循环后丢失。我列出了几行源代码 Function.java文件源 - JNA源
public Object invoke(Class returnType, Object[] inArgs, Map options){
.......
result = resultConverter.fromNative(result, context);
....
// Sync all memory which might have been modified by the native call
if (Structure.ByReference[].class.isAssignableFrom(inArg.getClass()))
{ Class type = inArg.getClass().getComponentType();
Structure[] ss = (Structure[])inArg;
for (int si=0;si < ss.length;si++) {
Pointer p =array.getPointer(Pointer.SIZE * si);
ss[si] =Structure.updateStructureByReference(type, ss[si], p);
} //////*///////////////////////*/program control gets lost after this
}.........}
任何猜测可能是什么问题?原生函数是否有问题或参数映射不正确。 非常感谢任何帮助..