2014-07-11 3 views
1

아래의 메소드 및 해당 메소드에 대한 단위 테스트. 문제는 Load 메서드의 result 값을 반환 할 수 없다는 것입니다. 아래의 단위 테스트가 실패합니다! JNA Structure ByReference

나는

어디에 내 실수 ... 나는 인스턴스화 .ByReference "없이"LoadResults를 전달하는 시도 있도록 기본 JNA의 객체에 의해 기본적으로하는 ByRef 있다고 생각?

@Test 
public void testLoad() { 
    MY_Processor proc = new MY_Processor(); 
    // LoadResults result = new LoadResults(); 
    LoadResults.ByReference result = new LoadResults.ByReference(); 
    ByteByReference [] pathToFile = new ByteByReference[256]; 
    // fill pathToFile out ... 

    try { 
     proc.Load (pathToFile, result); 
     assertEquals(0, result.errorCode); 
     assertEquals(1, result.elaborationTime); 
     assertEquals(2, result.coreItem); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 



public Integer Load (ByteByReference[] pathToFile, 
          LoadResults.ByReference result) throws Exception { 

    // here result is correctly filled out ! 
    LoadResults result = null; 
    result = native.getResult (numCore); 
} 

네이티브 코드가 추가되었습니다.

UPDATE

// header 
typedef struct 
{ 
    int  errorCode; 
    int  elaborationTime; 
    int  coreItem; 
} LoadResults; 

//[in] path 
//[out] result 
int Load (char path[MY_BUFFER_DEFINE], LoadResults* result); 
// implementation ... 

LoadResults* getResult (int numCore) 
{ 
    // some check ... 

    LoadResults *localResult = new LoadResults(); 
    // fill out ... 

    return localResult; 
} 

가 네이티브 코드에 의해 노출 된 "무료"방법은하지만

:-) 내 문제에 포커스를 유지하기 위해 보이지 않았다

/업데이트

감사합니다!

O.

+0

ByteByReference '[]를'포인터 배열은 :

2) 문제는 부하 방법 I에 다른 하나의 입력에 전달 된 기준을 갱신이었다. 나는 그것이 당신이 찾고있는 것이 아닌지 의심 스럽다. – technomage

+0

문자열을 함수에 전달하려는 경우 Java'String'을 사용하십시오. – technomage

+0

문제는 잘 작동하는 첫 번째 매개 변수 (pathToFile)가 아니지만 두 번째 param (결과)은 출력 매개 변수입니다. – Kasper

답변

0

...

1) 나는 LoadResults.ByReference를 사용할 필요가 없습니다.

public Integer Load (ByteByReference[] pathToFile, LoadResults result) throws Exception  
{ 
    // that's the problem!!!! storing the value into another object with another "address" 
    // and not the original "results". 
    // result = native.getResult (numCore); 

    // solved with this: 
    native.getResult (numCore, result); 
} 
0

그런 다음 함수 내 해당 매개 변수를 재 할당, 매개 변수로 Structure을 전달하는 것이 문제입니다. 그 주장에 아무런 영향을 미치지 않을 것입니다.

Pointer p = mylib.getResult() 
MyStructure m = new MyStructure(p); 
// .... 
mylib.free(p); 

나는 당신이 당신의 경로가 아닌 기본 char의 고정 된 크기의 버퍼와 같은 원시 문자열 (const char*)을 전달 권하고 싶습니다 :

당신이 따라야 할 패턴이있다.

UPDATE

당신이 인수에 결과를 복사 할 경우, 당신은 예를 들어, 구조의 내용을 복사해야합니다

그냥 해결
public int Load(LoadResults arg) throws Exception { 
    // Effectively copy memory from result into arg 
    LoadResults result = native.getResult(numCore); 
    if (alternative_1) { 
     // Copy result's native memory into arg's memory, then sync Java fields 
     result.useMemory(arg.getPointer()); 
     result.write(); 
     arg.read(); 
    } 
    else { 
     // Sync result's native memory with arg's Java fields 
     Pointer p = arg.getPointer(); 
     arg.useMemory(result.getPointer()); 
     arg.read(); 
     arg.useMemory(p); 
    } 
} 
+0

감사합니다. 엄밀히 말하면 내 문제는 JNA가 아닙니다. 네이티브 코드와의 상호 작용은 잘 작동합니다.내 문제는 호출자에게'LoadResults' 객체를 반환 할 수 없다는 것입니다. – Kasper