zoukankan      html  css  js  c++  java
  • JAVA调用动态链接库(dll)

       


    菜鸡爬坑 基础知识  
    因为某个东西的keygen我只会在win下生成!! 所以只能出此下策!!
    之前一直是android下用jni调用so文件,现在试下java在win平台下调用dll 首先还是老套路,直接在dll中弹出一个信息框具体流程如下:在Java中定义一个方法,在C++中实现这个方法,在方法内弹出信息框.
    首先在java中定义一个类 
    代码:
    package cn.ylca.dll;
    public class DllEntity {
    //本地方法
             public native void messagebox();
    }
    

    第二步 控制台到bin目录下
    使用javah生成DllEntity 的头文件


    生成文件内容如下
    代码:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class cn_ylca_dll_DllEntity */
    
    #ifndef _Included_cn_ylca_dll_DllEntity
    #define _Included_cn_ylca_dll_DllEntity
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class:     cn_ylca_dll_DllEntity
    * Method:    messagebox
    * Signature: ()V
    */
    JNIEXPORT void JNICALL Java_cn_ylca_dll_DllEntity_messagebox
      (JNIEnv *, jobject);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
    第三步新建一个dll的项目

    然后把刚才生成的内容复制到cpp文件   到这里还需要一个jni.h头文件  到jdk目录下 include 文件中复制一份 
    代码如下
    代码:
    // javadll.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    #include <stdio.h>
    #include <windows.h>
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
                                             )
    {
        return TRUE;
    }
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include "jni.h"
    /* Header for class cn_ylca_dll_DllEntity */
    
    #ifndef _Included_cn_ylca_dll_DllEntity
    #define _Included_cn_ylca_dll_DllEntity
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
    * Class:     cn_ylca_dll_DllEntity
    * Method:    messagebox
    * Signature: ()V
    */
            
    JNIEXPORT void JNICALL Java_cn_ylca_dll_DllEntity_messagebox
    (JNIEnv *env, jobject obj){//补上参数名字
    
            //到这里就不许要再说什么的了把! 
            MessageBox(NULL, "测试!", "demo", 0);
    }
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
    第四步就是如何在java中使用了  创建一个测试类来调用




    代码:
    public class Test {
            public static void main(String[] args) {
                    //加载指定路径的dll
                    System.load("D://javadll//Debug//javadll.dll");
                    //创建本地方法对象
                    DllEntity dllEntity = new DllEntity();
                    //调用方法
                    dllEntity.messagebox();
            }
       
    
  • 相关阅读:
    python logging模块
    mysql数据库的导出与导入
    requests请求高德地图api
    navicat连接阿里云ESC里的数据库
    ubantu+nginx+uwsgi+django部署
    linux小命令
    部署完的Django项目升级为HTTPS
    python常用模块
    python基础18——二分法&面向过程&匿名函数
    emmm......就当练习了系列15
  • 原文地址:https://www.cnblogs.com/zengkefu/p/5874836.html
Copyright © 2011-2022 走看看