JNA를 사용하여 기본 lib에 액세스하고 있습니다. 이것은 JNA에서 처음으로 작업 한 것이므로 c/C++에 대한 경험이 없습니다. 응용 프로그램을 작동시킬 수 있었고 올바른 결과가 반환되고 System.out.println()
을 사용하여 표시되었습니다. 이것은 내 코드의 마지막 줄입니다. 값이 콘솔에 표시되고 Java 충돌이 발생합니다 (예 : 대화 상자 Java Platform SE binary has stopped working
이 닫히거나 디버그 할 수있는 옵션과 함께 표시됩니다.Java JNA : 응용 프로그램 완료 후 JRE가 충돌 함
후 콘솔이 보여줍니다
Java Result: -1073741819
내 코드 : C에서 함수의
public static void main(String[] args) {
FloatByReference result = new FloatByReference();
//EDIT: changed to NativeLongByReference as per suggested answer
NativeLongByReference numContrib = new NativeLongByReference();
// This is a struct that needs to be passed by reference
MyDll.PContribution.ByReference contrib = new MyDll.PContribution.ByReference();
NativeLong err = MyDll.INSTANCE.calcResult("myValue", result, numContrib, contrib);
// I only care about result and not the other out-parameters
System.out.println(result.getValue());
//crash here
}
(고화질)는 :
typedef long (CALCRESULT)(const char*, float*, long*, HANDLE*);
뭐가 잘못 이죠? 응용 프로그램이 종료되기 전에 정리를 수행해야합니까?
편집 :
루프에서 메소드 호출을 실행할 수 있으며 작동합니다. 종료 될 때만 추락합니다.
EDIT2 :
MyDll 여기 코멘트 코드 당으로 다음 struct
의
public interface MyDll extends Library {
public static class PContribution extends Structure {
public static class ByReference extends PContribution implements Structure.ByReference {
public byte[] Class = new byte[10];
public byte[] Type = new byte[6];
public byte[] Description = new byte[40];
public byte[] Comment = new byte[10];
public float Value;
}
protected List<String> getFieldOrder() {
return Arrays.asList(new String[] { "Class", "Type", "Description", "Comment", "Value" });
}
}
MyDll INSTANCE = (MyDll) Native.loadLibrary("MyDll", MyDll.class);
NativeLong calcResult(String smi, FloatByReference result, NativeLongByReference numContrb, PContribution contrib);
}
정의 :
typedef struct {
char Class[10];
char Type[6];
char Description[40];
char Comment[10];
float Value;
} PContribution;
편집 3 :
젠장. 사용 가능한 문서가 dll의 이전 버전 용이라는 것을 알아 냈습니다. 이전 dll을 사용하면 모든 것이 작동합니다. 이제 공급자로부터 새 버전의 문서를 받아야합니다.
EDIT 4 :
그것은 오래된 DLL로 작동하지만 응용 프로그램은 지속적으로 65533 반복 (전화) 후 충돌합니다. 각 호출은 정확히 동일한 매개 변수를 사용합니다.
java.lang.Error: Invalid memory access
at com.sun.jna.Native.invokeInt(Native Method)
at com.sun.jna.Function.invoke(Function.java:390)
at com.sun.jna.Function.invoke(Function.java:323)
at com.sun.jna.Library$Handler.invoke(Library.java:236)
at com.sun.proxy.$Proxy0.calcLogP(Unknown Source)
실습의 요점은 수천 번의 호출을 신속하게 수행 할 수 있다는 것입니다.
'MyDll'에 대한 클래스 정의도 게시하십시오. 'calcResult()'에 대한 선언은 여기에 관련있다. [최소한의 완전하고 검증 가능한 예제를 만드는 방법] (http://stackoverflow.com/help/mcve) –
편집을 참조하십시오. 나는 dll을 공유 할 수 없기 때문에 실행 가능한 예제를 만들 수 없습니다. –
나는 dll에 문제가있는 것 같았다. 가능한 원인에 대해 좀 더 자세히 설명하도록 내 답변을 업데이트하십시오. –