Thanks for your help :)
windows xp x86, jdk 1.6.20
hellojna dll project
hellojna.h
extern "C" _declspec(dllexport) int swapvalue(int* a, int* b); extern "C" _declspec(dllexport) int rf_select1(unsigned char *_Size); extern "C" _declspec(dllexport) int rf_select2(unsigned char *_Size,long icdev); extern "C" _declspec(dllexport) int rf_select3(long icdev, unsigned char *_Size);
hellojna.cpp
#include "stdafx.h"
#include "hellojna.h"
int rf_select1(unsigned char *_Size) {
_Size[0] = 'a';
return 12;
}
int rf_select2(unsigned char *_Size, long icdev) {
_Size[0] = 'a';
return 12;
}
int rf_select3(long icdev, unsigned char *_Size) {
_Size[0] = 'a';
return 12;
}
HelloJna.java
public interface HelloJna extends Library {
HelloJna instance = (HelloJna) Native.loadLibrary("hellojna", HelloJna.class);
int rf_select1(byte[] abd);
int rf_select2(byte[] abd, long dev);
int rf_select3(long dev, byte[] abd);
}
Test.java
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
HelloJna jna = HelloJna.instance;
byte[] buf = new byte[]{'s', 'u', 'p', 'e', 'r', ' ', 'm', 'a', 'n', '\0'};
int ret = jna.rf_select1(buf);
System.out.println("buf=" + new String(buf) + ",ret=" + ret);
buf[0] = 'I';
ret = jna.rf_select2(buf, 77);
System.out.println("77 buf=" + new String(buf) + ",ret=" + ret);
buf[0] = 'E';
ret = jna.rf_select3(99, buf);
System.out.println("99 buf=" + new String(buf) + ",ret=" + ret);
}
}
console.out:
buf=auper man , ret=12
77 buf=auper man ,ret=12
the screen capter:
why rf_select1 and rf_select2 is right but rf_select3 is error ??
the differences is just the paramters order??
in the rf_select3 _Size is null
buf[0] = 'E';
ret = jna.rf_select3(99, buf);
2010-09-15 00:13 add info:
I found it's work good when HelloJna.java change rf_select3(long dev, byte[] abd) to rf_select3(NativeLong dev, byte[] abd)
public interface HelloJna extends Library {
HelloJna instance = (HelloJna) Native.loadLibrary("hellojna", HelloJna.class);
int rf_select1(byte[] abd);
int rf_select2(byte[] abd, long dev);
int rf_select3(NativeLong dev, byte[] abd);
}