2016-07-26 4 views
0

에 참고로 구조를 통과 : 자바방법 I이 함수를 호출 할 JNA

typedef struct 
{ 
    int a; 
    int b; 
} mystruct; 

등가 코드는 다음과 C에서 정의

extern "C" xxx_SERVICE_API int initSocket(socketStruct* skStruct); 

++ dll.socketStruct 아래 정의 구조는 :

public interface MyDllClass extends Library { 

    public static class MyStructure extends Structure { 

     public static class ByReference extends MyStructureRef 
     implements Structure.ByReference{ 


     } 
     @Override 
     protected List<String> getFieldOrder() {  

      return Arrays.asList(new String[]{"a","b"}); 
     } 
     public int a; 
     public int b; 
    } 
    //Native function 
    int initSocket (MyStructure.ByReference sks); 
    MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll", 
    MyDllClass.class); 
} 

이 기본 함수를 매개 변수로 참조하여 구조체를 호출 할 수 있습니까? 감사합니다.

답장을 보내 주셔서 감사합니다. 당신과 함께 따르면 난 내 구조에 생성자를 추가 네이티브 함수를 호출 할 때

MyStructure mySocket = new MyStructure(); 
MyDllClass.INSTANCE.initSocket(mySocket); 

이 코드 때문에 널 포인터 예외의 실패 :

public interface MyDllClass extends Library { 

    public static class MyStructure extends Structure { 

     public MyStructure() { 

      idModule = 0; 
      nbModules = 0; 
      nbQueueInModule = 3; 
      portList = new int[128]; 
      serverList = new char[128][20]; 
     } 
     @Override 
     protected List<String> getFieldOrder() {  

      return Arrays.asList(new String[]{"a","b","portList","serverList"}); 
     } 
     public int a; 
     public int b; 
     public int[] portList; 
     public char[][] serverList; 
    } 
    //Native function 
    int initSocket (MyStructure.ByReference sks); 
    MyDllClass INSTANCE = (MyDllClass)Native.loadLibrary("Mydll", 
    MyDllClass.class); 
} 

주에서 나는이 코드를 썼다. 아이디어가 있으십니까?

답변

1

모두 JNA Structure 매개 변수의 기본값은 참조 (struct*) 의미입니다.

구조체 멤버로 나타나는 경우 값 의미에 따라 기본적으로 나타납니다.

보조 동작을 원하는 곳에 사용할 수 있도록 ByReferenceByValue 태그 지정 인터페이스가 제공됩니다. 기본 동작이 원하는 경우 추가 타이핑이 필요하지 않습니다.

Pointer 생성자를 사용자가 정의한 Structure에 항상 제공해야하며 JNA에 의한 불필요한 메모리 할당을 피할 수 있습니다.