Java calls a dynamic-link library (DLL)

Source: Internet
Author: User


Basic knowledge of vegetable chicken crawl Pit
Because of the keygen of something I will only build under WIN!! So only this is the worst!!
Before has been the android under the call so file with JNI, now try Java in the win platform to call the DLL first or the old routine, directly in the DLL popped a message box the concrete flow is as follows: Define a method in Java, implement this method in C + +, pop the message box inside the method.
First define a class in Java
Code:
Package Cn.ylca.dll;public class Dllentity {//local method public native void MessageBox ();}

Second step console to bin directory
Using Javah to generate Dllentity header files


The resulting file contents are as follows
Code:
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class cn_ylca_dll_dllentity */#if Ndef _included_cn_ylca_dll_dllentity#define _included_cn_ylca_dll_dllentity#ifdef __cplusplusextern "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
Third step to create a new DLL project

Then copy the content you just generated to the CPP file and you'll need a jni.h header file to copy it to the include file in the JDK directory.
The code is as follows
Code:
 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 __cplusplusextern  " C " {#endif/** class:     cn_ylca_dll_dllentity* method:     messagebox* Signature:  () V*/        jniexport  void jnicall java_cn_ylca_dll_dllentity_messagebox (jnienv *env, jobject obj) {// Fill in the parameter name         //here, you're not allowed to say anything else!          messagebox (null,  "Test!",  "demo",  0);} #ifdef  __cplusplus} #endif #endif
The fourth step is how to use the creation of a test class in Java to invoke




Code:
public class Test {public static void main (string[] args) {//load DLL for the specified path system.load                ("D://javadll//debug//javadll.dll");                Create local Method Object Dllentity dllentity = new dllentity ();        Call Method Dllentity.messagebox (); }

Java calls a dynamic-link library (DLL)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.