日期:2014-05-20  浏览次数:20644 次

JNA,回调函数实现中莫名其妙的问题,大家给看看吧
写的程序如下:
public class Test1 {	
public static void main(String[] args){ 
if (CLibrary.INSTANCE.Init((ByReference)null,new StatusCallBack1(),6010)){
System.out.println("初始化成功1");
}
MyThread mytd= new MyThread();
Thread demo=new Thread(mytd);
demo.start();
}
}

其中:
public interface CLibrary extends Library {
CLibrary INSTANCE = (CLibrary)Native.loadLibrary("IOCP_DLL",CLibrary.class);
    void ShutDown();
    boolean Init(ByReference pthis,StatusCallBack pcallback,int port);
    void CloseClient(int sockClient);
    int GetBufferLen(int sockClient);
    void MoveBuffer(int sockClient,Pointer str,int num);
}

public interface StatusCallBack extends Callback {
void invoke(int sockClient,int code);
}
class StatusCallBack1 implements StatusCallBack {
int long1=0;
public static String toStringEveryByte(byte b) {
int num1, num2;
char char1, char2;
String tostring = "";
num1 = (b & 0xf0) >> 4;
num2 = (b & 0x0f);
char1 = t2s(num1);
char2 = t2s(num2);
tostring += char1;
tostring += char2;
return tostring;
}
public static char t2s(int tennum) {
char c[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A',
'B', 'C', 'D', 'E', 'F' };
byte b;
b = (byte) tennum;
return (c[b & 0x0f]);
}
public static int s2t(String s_string) {
return Integer.parseInt(s_string.trim(), 16);
}

public void invoke(int sockClient, int code) 
   {
if (code==1)
{
System.out.println(sockClient + "终端连入");
}
else if (code==2)
{
System.out.println(sockClient +"终端断开");
}
else if (code==4)
{
int long1=0;
ByteBuffer bb = ByteBuffer.allocate(100);
Memory mymem1 = new Memory(100);
long1=CLibrary.INSTANCE.GetBufferLen(sockClient);
System.out.println("数据上送"+long1);
CLibrary.INSTANCE.MoveBuffer(sockClient, mymem1, long1);