How can a function in a DLL be called in Java?
The key is that the native function name parameter in Java is identical to the local function name parameter in the DLL.
This program is the SUM function in the call DLL in Java.
One, Java code part operation
1. New Project
2.dlltest.java Code
Package Com.aizizai.calldll;public class Dlltest {static {system.loadlibrary ("Hellodll");} public static native int sum (int a, int b);p ublic static void Main (string[] args) {System.out.println ("sum (2,5) =" + SUM ( 2, 5));}}
3. Generating the header file
Java Call DLL Test \src>javac Com/aizizai/calldll/dlltest.java
Java Call DLL Test \src>javah com.aizizai.calldll.DllTest
Second, VC uses the previous header file to generate the DLL. This article uses the VS2010.
1. Create a project
New Project--VC + +-Win32 Project--named Hellodll---Application type: DLL-->ok.
2. Add a header file
A. Put
/jdk/include/jni.h
/jdk/include/win32/jawt_md.h
/jdk/include/win32/jni_md.h
Copy to the project directory or to the include directory of VS.
B. Add in StdAfx.h
#include "Com_aizizai_calldll_dlltest.h"
3. Add a function implementation in the source file
Hellodll.cpp: Defines an export function for a DLL application. #include "stdafx.h" Jniexport jint jnicall java_com_aizizai_calldll_dlltest_sum (jnienv *env, Jclass SS, Jint A, Jint b) { return a+b;}
4. Building a DLL
Build---> Build DLL.
Can be seen in the debug directory of the project, Hellodll.dll file
Third, use
Copy the DLL file to the C:\Windows directory and run the Java program. Ok.
Program: http://download.csdn.net/detail/u012587637/8065041
Java Call DLL