jna 使用实例,

 2023-09-15 阅读 23 评论 0

摘要:  有与项目组需要用到C++的一个模块, 需要将一个2维数组传到dll 里面 ,返回一个字符串, 恶心了1天终于完成了, 记录一下,同时也希望能给你带来帮助. redis使用实例。java 代码如下, package test;import java.util.Arrays;import com.sun.jna.Library; import com.

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

 

redis使用实例。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 代码如下

jpa教程。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 = 0;for (int i = 1; i<count; i++){for (int j = count - 1; j >= i; j--){if (p[j]>p[j - 1]){temp = p[j];p[j] = p[j - 1];p[j - 1] = temp;}}}
}int test(int *p, char* count){int conversion = 2;count[0 * conversion] = 'm';count[1 * conversion] = 'j';count[2 * conversion] = 'o';count[3 * conversion] = 'r';count[4 * conversion] = 'c';count[5 * conversion] = 'e';count[6 * conversion] = 'n';printf("char size is d% " , sizeof('m'));p[0] = 0;p[1] = 1;return 0;
}
int sum(int  i1, int  i2){return i1 + i2;
}void sort2(int **p, int count ,char* chars)
{try{int conversion = 2;printf("conversion is %d \n", conversion);printf("**p is %d \n", sizeof(** p));chars[0 * conversion] = 'm';chars[1 * conversion] = 'j';chars[2 * conversion] = 'o';chars[3 * conversion] = 'r';chars[4 * conversion] = 'c';chars[5 * conversion] = 'e';chars[6 * conversion] = 'n';int *temp  ;for (int i = 1; i<count; i++){printf("temp[0] is %d ,temp[1] is %d \n", p[i][0], p[i][1]);for (int j = count - 1; j >= i; j--){if (p[j][0]>p[j - 1][0]){temp = p[j];p[j] = p[j - 1];p[j - 1] = temp;}}}}catch (std::exception& e){std::cerr << "exception caught: " << e.what() << '\n';}
}

v函数的使用方法及实例。 

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/2/65394.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息