jna对指针、字符串参数传递的使用

C/C++部分代码:

#include 
extern "C" __declspec(dllexport) void PT(char* a, float *b);

void PT(char* a, float *b){
	printf("%s , %f",a,*b);
}

JAVA部分代码:

import java.nio.FloatBuffer;

import com.sun.jna.Library;
import com.sun.jna.Native;
import com.sun.jna.Pointer;

public interface TestJNA extends Library {
	public abstract void PT(Pointer ip,Pointer b);
	TestJNA INSTANCE = (TestJNA) Native.loadLibrary("C_Part", TestJNA.class);
}
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;


public class TEST {

	/**  
     * @param args  
     */  
    public static void main(String[] args) {  
    	  
        Pointer p1 = new Memory(15);
		String ip="192.168.1.101";
		p1.setString(0, ip);
		float t1=-6;
		int size=Native.getNativeSize(Float.class);
		Pointer p2 = new Memory(size);
		p2.setFloat(0, t1);
		TestJNA.INSTANCE.PT(p1,p2);
    }  
	
}

运行结果:

jna对指针、字符串参数传递的使用_第1张图片

代码GITHUB源码链接:https://github.com/XiaoYaoNet/JNA

你可能感兴趣的:(JAVA)