jna 使用实例,

  有与项目组需要用到C++的一个模块, 需要将一个2维数组传到dll 里面 ,返回一个字符串, 恶心了1天终于完成了, 记录一下,同时也希望能给你带来帮助.

java 代码如下,

package test;

import java.util.Arrays;

import com.sun.jna.Library;
import com.sun.jna.Memory;
import com.sun.jna.Native;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.PointerByReference; /**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Aug 25, 2014 5:04:25 PM
* @version 1
*/
public class Project1Service { public interface Project1 extends Library {
/**
* 当前路径是在项目下,而不是bin输出目录下。
*/
Project1 INSTANCE = (Project1) Native.loadLibrary("Project1",
Project1.class); public Integer sum(Integer i1, Integer i2); public Integer test(int[] is, char[] cs); public Integer sort(int[] is, Integer count); public Integer sort2(Pointer[] is, int count, char[] cs); } /**
*
*/
public Project1Service() {
// TODO Auto-generated constructor stub
} /**
* @param args
*/
public static void main(String[] args) {
try { sort2();
} catch (Throwable e) {
e.printStackTrace();
}
// TODO Auto-generated method stub // sum_fun();
// sort();
// test();
} /**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Aug 25, 2014 9:26:54 PM
* @version 1
*/
private static void sort2() { char[] cs = { '0', '0', '0', '0', '0', '0', '0', '0', '0' };
Pointer[] byReferences = new Pointer[10];
for (int i = 0; i < 10; i++) {
PointerByReference byReference = new PointerByReference();
Memory mymem = new Memory(4 * 2);
// mymem.read(0, new int[] { 1, 2 }, 0, 2);
mymem.setInt(4, 20);
mymem.setInt(0, 10);
byReference.setPointer(mymem);
byReferences[i] = byReference.getPointer();
}
Project1.INSTANCE.sort2(byReferences, 10, cs);
System.out.println(Arrays.toString(cs)); } /**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Aug 25, 2014 9:26:54 PM
* @version 1
*/
private static void sort2_2() {
int[][] is = { { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 },
{ 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 }, { 12, 12 },
{ 12, 12 } };
char[] cs = { '0', '0', '0', '0', '0', '0', '0', '0', '0' }; PointerByReference byReference = new PointerByReference();
Memory mymem = new Memory(4 * 2 * 10);
for (int i = 0; i < 10; i++) {
mymem.read(i, new int[] { 1, 2 }, 0, 2);
}
byReference.setPointer(mymem);
// Project1.INSTANCE.sort2(byReference.getPointer(), 10, cs);
System.out.println(Arrays.toString(cs)); } /**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Aug 25, 2014 7:43:45 PM
* @version 1
*/
private static void test() {
int[] is = { 12, 30, 39, 4, 5, 6, 10, 7, 8, 9 };
char[] cs = { '0', '0', '0', '0', '0', '0', '0', '0', '0' };
System.out.println(is);
String str = "str";
Integer count = Project1.INSTANCE.test(is, cs);
System.out.println(Arrays.toString(is));
System.out.println(Arrays.toString(cs));
System.out.println("str =" + str);
System.out.println(is.length);
} /**
*
*
* @author mjorcen
* @email mjorcen@gmail.com
* @dateTime Aug 25, 2014 6:44:21 PM
* @version 1
*/
private static void sort() {
int[] is = { 1, 2, 3, 4, 5, 6, 10, 7, 8, 9 };
System.out.println(Arrays.toString(is));
Integer count = Project1.INSTANCE.sort(is, is.length);
System.out.println(is.length); } private static void sum_fun() {
Integer count = Project1.INSTANCE.sum(1, 2);
System.out.println("count = " + count);
} }

ddl 代码如下

h>

#pragma once
extern "C"_declspec(dllexport) void sort(int *p, int count);
extern "C"_declspec(dllexport) int test(int *p, char* count);
extern "C"_declspec(dllexport) int sum(int i1, int i2);
extern "C"_declspec(dllexport) void sort2(int **p, int count, char* chars);
#include <stdio.h>
#include <exception> // std::exception
#include <iostream> // std::cerr
class j_test
{ public:
j_test();
~j_test();
void sort(int *p, int count);
void sort2(int **p, int count, char* chars);
int sum(int i1, int i2);
int test(int *p, char* count);
};

cpp>

#include "j_test.h"

j_test::j_test()
{
} j_test::~j_test()
{
} void sort(int *p, int count)
{
int temp = ;
for (int i = ; i<count; i++)
{
for (int j = count - ; j >= i; j--)
{
if (p[j]>p[j - ])
{
temp = p[j];
p[j] = p[j - ];
p[j - ] = temp;
}
}
}
} int test(int *p, char* count){
int conversion = ;
count[ * conversion] = 'm';
count[ * conversion] = 'j';
count[ * conversion] = 'o';
count[ * conversion] = 'r';
count[ * conversion] = 'c';
count[ * conversion] = 'e';
count[ * conversion] = 'n';
printf("char size is d% " , sizeof('m'));
p[] = ;
p[] = ;
return ;
}
int sum(int i1, int i2){ return i1 + i2;
} void sort2(int **p, int count ,char* chars)
{
try{ int conversion = ;
printf("conversion is %d \n", conversion);
printf("**p is %d \n", sizeof(** p));
chars[ * conversion] = 'm';
chars[ * conversion] = 'j';
chars[ * conversion] = 'o'; chars[ * conversion] = 'r';
chars[ * conversion] = 'c';
chars[ * conversion] = 'e'; chars[ * conversion] = 'n'; int *temp ;
for (int i = ; i<count; i++)
{
printf("temp[0] is %d ,temp[1] is %d \n", p[i][], p[i][]);
for (int j = count - ; j >= i; j--)
{ if (p[j][]>p[j - ][])
{
temp = p[j]; p[j] = p[j - ];
p[j - ] = temp; }
}
}
}
catch (std::exception& e)
{
std::cerr << "exception caught: " << e.what() << '\n';
}
}
上一篇:Android开发之ADT中无Annotation Processin的解决办法


下一篇:spring整合mybatis、hibernate、logback配置