有与项目组需要用到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';
}
}

jna 使用实例,的更多相关文章

  1. JNA入门实例

    JNA(Java Native Access):建立在JNI之上的Java开源框架,SUN主导开发,用来调用C.C++代码,尤其是底层库文件(windows中叫dll文件,linux下是so[shar ...

  2. 最近学习工作流 推荐一个activiti 的教程文档

    全文地址:http://www.mossle.com/docs/activiti/ Activiti 5.15 用户手册 Table of Contents 1. 简介 协议 下载 源码 必要的软件 ...

  3. JNA的步骤、简单实例以及资料整理

    1.步骤 1.编写dll文件,放入项目的bin目录(在window上是dll文件,在Linux上是so文件,dll和so都是由C程序生成)  2.新建接口继承Library  3.加载对应的dll或者 ...

  4. [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PWideChar

    Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessWide(pvData: P ...

  5. [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar

    Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessAnsi(pvData: P ...

  6. java 用JNA方法调用C++动态链接库

    JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的JNI的基础之上的一个框架.非常强大.易用,功能上类似与.NET的P/Invoke.你只 ...

  7. JNA—JNI终结者

    JNA—JNI终结者 介绍 给大家介绍一个最新的访问本机代码的Java框架—JNA. JNA(Java Native Access)框架是一个开源的Java框架,是SUN公司主导开发的,建立在经典的J ...

  8. JNI的替代者—使用JNA访问Java外部功能接口

    摘自:http://www.cnblogs.com/lanxuezaipiao/p/3635556.html JNI的替代者-使用JNA访问Java外部功能接口 1. JNA简单介绍 先说JNI(Ja ...

  9. java 利用jna调用c#的dll

    一.需求阐述: 如果我们的项目利用c#开发,到了开发后期需要和java组进行合作,其中有一部分业务逻辑利用c#已经code completed,那么我们可能会考虑用java来调用现成的c#dll实现需 ...

随机推荐

  1. 解决codeforces访问慢的问题

    转载至:http://blog.csdn.net/wmx16835/article/details/38639451 前阵子打Codeforces(以下简称CF),每次比赛的时候都要等待网页刷新.不是 ...

  2. hdu 1094 A+B for Input-Output Practice (VI)

    A+B for Input-Output Practice (VI) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/327 ...

  3. [CSS][转载]内层div的margin-top影响外层div

    参考 内层div的margin-top影响外层div——引出外边距合并 div嵌套导致子区域margin-top失效不起作用的解决方法 我使用的是在外层的div中添加 border: 1px soli ...

  4. iis7 php urlrewrite 并隐藏index.php

    <rewrite> <rules> <rule name="OrgPage" stopProcessing="true"> ...

  5. ant+dbunit 导xml到mysql

    1.增加自己的data目录和xml数据文件 2.lib依赖 commons-logging-1.1.1.jar dbunit-2.4.8.jar junit-4.9b2.jar mysql-conne ...

  6. asp.net 配置二级域名的共享session,并实现sso单点登录

    公司最近做了一个新网站.原先网站的网址是www.xxxx.com.新做的网站要部署到info.xxxx.com.这两个网站要实现单点登录.而新老网站本身机构的原因,对于登录状态的判断,说白了就是对于s ...

  7. excel知识

    excel中导出文本中的制表符去除方法:

  8. ios7之后 根据UILabel的文字计算frame的方法

    ios7 新出来的根据label的文字和字体大小来确定label的宽高. 官方的方法是: - (CGRect)boundingRectWithSize:(CGSize)size options:(NS ...

  9. win10 删除资源管理器中的6个文件夹

    细心的朋友会发现,在Win10此电脑(计算机)中,除了我们最熟悉的磁盘外,还新增了视频.图片.文档.下载.音乐.桌面这6个文件夹.不少网友举觉得这6个文件夹其实并没什么用,想要去除删掉.那么Win10 ...

  10. mysql空间数据相关操作

    建表语句: CREATE TABLE ts.points ( name ) NOT NULL, location POINT NOT NULL, description ) ); 添加记录如下: IN ...