JNA映射自己编写的DLL

原始发表时间:2009-07-03

 

学习过程 - 思维导图:


    使用VS 2008开发自己的DLL,用于创建桌面快捷方式,而后通过Java调用DLL来实现快捷方式创建的功能。
    JNA版本为 3.1.0 b0
    JDK 1.6.0

    本文旨在说明JNA可以如何实现DLL的调用,暂不给出快捷方式创建的实现,因为这个调用实际上是属于c、win32api、shell编程的范畴。

文章大纲
1.新建项目
2.编写代码
3.编译生成W32DLL.dll
4.JNA映射自己编写的DLL文件(遇到问题及解决方法)
5.小结

1.新建项目
    在VS中【新建项目】,项目类型选择Windows项目,如下图:

    设置项目的程序类型为“DLL”,并且设置为空项目。

2.编写代码
    编写头文件和cpp代码:
----------------  DLLcode.h  ----------------
#ifdef DLLDIR_EX
    #define DLLDIR __declspec(dllexport) // export dll info
#else
    #define DLLDIR __declspec(dllimport) // import dll info
#endif

extern "C"{
    void DLLDIR DLLfun1(char*);
    int DLLDIR DLLfun2(int);
};

extern int DLLDIR DLLArg;

class DLLDIR DLLclass{
public :
    DLLclass();
    ~DLLclass();

    int add(int, int);
    int sub(int, int);
    int Arg;
    static int go(int a){
        return a;
    };
};

----------------  DLLcode.cpp  ----------------
#include "DLLCode.h"
#include <iostream>

using namespace std;
void DLLfun1(char* a){
    cout<<a<<endl;
};
int DLLfun2(int a){return a<<1;};
int DLLArg = 100;
DLLclass::DLLclass(){};
DLLclass::~DLLclass(){};

int DLLclass::add(int a, int b){
    return a + b;
};
int DLLclass::sub(int a, int b){
    return a - b;
};

3.编译生成W32DLL.dll

    编写代码后,在VS主菜单中依次选择“生成”->“生成解决方案”。
    项目目录为 D:\develop\Net_space\W32DLL\,所以来到debug目录(路径为D:\develop\Net_space\W32DLL\Debug\)中找到生成的dll文件W32DLL.dll

4.JNA映射自己编写的DLL文件(遇到问题及解决方法)
Java程序如下:

package demo.win32api;

import java.io.UnsupportedEncodingException;

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

/**
 * Updated at 上午10:49:20, on 2009-7-3<br>
 *
 * @author caesar
 */
public class TestCustomWin32DLL {

    /**
     * Updated at 上午10:49:20, on 2009-7-3<br>
     *
     * @param args
     * @author caesar
     * @throws UnsupportedEncodingException
     */
    public static void main(String[] args) throws UnsupportedEncodingException {
        NativeLibrary.addSearchPath("W32DLL", "D:/develop/Net_space/W32DLL/Debug");
        W32DLL lib = (W32DLL) Native.loadLibrary("W32DLL", W32DLL.class);
        System.out.println(lib);
        try {
            int res = lib.DLLfun2(1);
            System.out.println("DLLfun2: " + res);
            res = lib.add(1, 2);
            System.out.println("add: " + res);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

interface W32DLL extends Library {
    int add(int a, int b);
    int DLLfun2(int a);
}

    执行上面的Java程序后,控制台输出如下内容:
Proxy interface to Native Library <D:\develop\Net_space\W32DLL\Debug\W32DLL.dll@54722560>
DLLfun2: 2
Exception in thread "main" java.lang.UnsatisfiedLinkError: Error looking up function 'add': Õ?»µ½?¶¨µ
    at com.sun.jna.Function.<init>(Function.java:129)
    at com.sun.jna.NativeLibrary.getFunction(NativeLibrary.java:250)
    at com.sun.jna.Library$Handler.invoke(Library.java:191)
    at demo.win32api.$Proxy0.add(Unknown Source)
    at demo.win32api.TestCustomWin32DLL.main(TestCustomWin32DLL.java:33)

    不明白,映射普通的C函数,JNA执行无误,但是在映射c++的方法时会出现无法找到函数的问题。难道JNA只能映射C函数,而不能处理C++的OO特性?google得到文章一篇

原文链接:http://markmail.org/message/oygj6642zfsexcz7

Google网页快照链接:http://203.208.39.132/search?q=cache:vKtda_84QGQJ:markmail.org/message/oygj6642zfsexcz7+jna+c%2B%2B&cd=5&hl=zh-CN&ct=clnk&gl=cn&client=firefox-a&st_usg=ALhdy2_TQaf9DJb4klQOt17Oa0H4T23Twg

注: 请原谅我写了那么冗长的链接,主要是因为原文链接很可能无法访问,所以后来通过Google的网页快照才得以见到原文的副本。


帖子部分内容如下:

JNA doesn't "do" C++ , at least not automatically. While C++ methods may be exported from a shared library, several layers of translation are needed before you can access C++ objects and methods transparently. You need to interpret and generate mangled function names before you can create an object, and then parse and dispatch virtual function tables before you can invoke any methods on it. It'd be cool to have some C++ layers on top of JNA , but I currently have no reason to make them, so it's not likely to happen.

There are other offerings that claim to provide good C++ integration (JNIEasy comes to mind, although "easy" might be a bit of a stretch). I can't think of any free projects offhand, but there's a list in the OTHERS file in the JNA root dir.

I don't know how well Swig handles wrapping objects and object methods, or if it just exports a functional interface. If the latter, you could just as well wrap your library in a few key 'extern "C"' functions and use JNA .

……………………


大意是说:JNA无法处理C++,作者的建议是在C程序包装并调用C++,而后JNA来调用这些C函数,从而使用一些需要用到C++特性的功能。

5.小结
    JNA无法处理C++,那么使用C函数调用来封装C++层面的代码,让JNA调用C函数,来使用一些C++层面的特性。
    至此已经做好使用JNA来实现调用shell创建桌面快捷方式的一切前期准备。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值