zoukankan      html  css  js  c++  java
  • JNA调用DLL(入门):让你一眼就学会

     

    DLL(Dynamic Link Library)文件,是基于C语言的动态链接库文件,就是一些封装好的方法,打成dll格式包,供别人调用

    JNA是一种能够使Java语言使调用DLL的一种技术,

    首先,实现一个Hello World,让咱们第一次使用者增强信心 

    1、下载jna.jar,添加到工程

     2、代码

    import com.sun.jna.Library;
    import com.sun.jna.Native;
    
    public class Test {
        
        // 定义接口CLibrary,继承自com.sun.jna.Library  
        public interface CLibrary extends Library{
            // msvcrt为dll名称,msvcrt目录的位置为:C:WindowsSystem32下面
            CLibrary INSTANCE = (CLibrary) Native.loadLibrary( "msvcrt" , CLibrary.class);
            // printf为msvcrt.dll中的一个方法. 
            void printf(String format, Object... args);
        }
        
    
        public static void main(String[] args) {
            // 调用printf打印信息
            CLibrary.INSTANCE.printf("Hello,World");
            
        }
    }

    可以看到,interface接口里做了两件事

    1、加载dll文件

    2、声明dll里面的方法,供别人调用

    JNA数据类型——Java数据类型 对应

    Native Type Size Java Type Common Windows Types
    char 8-bit integer byte BYTE, TCHAR
    short 16-bit integer short WORD
    wchar_t 16/32-bit character char TCHAR
    int 32-bit integer int DWORD
    int boolean value boolean BOOL
    long 32/64-bit integer NativeLong LONG
    long long 64-bit integer long __int64
    float 32-bit FP float  
    double 64-bit FP double  
    char* C string String LPTCSTR
    void* pointer Pointer LPVOID, HANDLE, LPXXX
  • 相关阅读:
    A Simple PlugIn Library For .NET
    (转).NET 一次查询多表,填充DataSet并指定表名(DataSet指定DataTable名称的技巧)
    Database Schema Create
    C++中关于指针入门的最好的文章
    oleDbCommand访问Excel
    the best simple c++
    c++连接数据库
    plugin framework 1
    c# invoke c++
    摩根士丹利赐与新浪增持评级
  • 原文地址:https://www.cnblogs.com/Donnnnnn/p/7426759.html
Copyright © 2011-2022 走看看