JAVA call dynamic link library DLL-JNative Learning

Source: Internet
Author: User

JAVA calls the dynamic link library DLL using the following methods: JNI (Java Native Interface), JInvoke, and JNative (Java to native interface). JNI is the method provided by JAVA itself. It is unknown from where JInvoke comes from, it seems that you still need to register and pay for it. 64-bit JDK and JIR environments cannot run, and Windows XP installed in VMware also has problems. JNative is open-source, however, the number of related documents is very small, and it seems that the update has been stopped. JNative Source Code address: Unknown /.

 

File "new" JAVA project:

Enter the project name and click "Finish ":

Right-click and choose new class:

Enter the package name and class name, check "public static void main (String [] args)", and click "Finish ":

Open the project file directory:

Copy the DLL file "EhfscliaxDll. dll" to this directory,

Create a new "lib" folder and copy JNative. jar to this directory:

Configure the compilation directory:

Click Add External JARs to Add the jar package:

Helper class code:

 

Package com. ehfscliax; import org. xvolks. jnative. JNative; import org. xvolks. jnative. type; import org. xvolks. jnative. exceptions. nativeException; import org. xvolks. jnative. pointers. pointer; public class Helper {/*** @ param args * @ throws NativeException */public static void main (String [] args) throws NativeException {JNative. setLoggingEnabled (true); try {JNative getUrl = new JNative (EhfscliaxDll. dll, getUrl); // create the JNative object getUrl of the getUrl method. setRetVal (Type. STRING); // set the return value type to StringgetUrl. setParameter (0, 127.0.0.1); // set the getUrl parameter value required by the method in sequence. setParameter (1, 10087); getUrl. setParameter (2,123); getUrl. invoke (); // call the method System. out. println (getUrl. getRetVal (); // output return value} catch (IllegalAccessException e) {e. printStackTrace ();} try {JNative getPlayUrl = new JNative (EhfscliaxDll. dll, getPlayUrl); getPlayUrl. setRetVal (Type. INT); // the return value type defined in DLL is const wchar_t *. If the return value type is set to String, only the first character String mgrIp = 127.0.0.1; Pointer ptr = Pointer can be obtained. createPointerFromString (mgrIp); getPlayUrl. setParameter (0, ptr); getPlayUrl. setParameter (1, 10087); getPlayUrl. setParameter (2,123); getPlayUrl. invoke (); // personally understand that getRetValAsInt is the pointer address for getting the returned value. The getUnicodeMemoryAsString method reads the String url = JNative from the memory of the pointer address. getUnicodeMemoryAsString (getPlayUrl. getRetValAsInt (); System. out. println (url);} catch (IllegalAccessException e) {e. printStackTrace ();}}}
Execution result:

 

 

22:48:46, [DEBUG] [org. xvolks. jnative. JNative] [loadLibrary]: Successfully loaded library 'ehfscliaxdll. dll ', functionName = getUrl: hModule = 268435456 http: // Chinese 127.0.0.1: 100872014-5-25 22:48:46, [DEBUG] [org. xvolks. jnative. pointers. memory. memoryBlockFactory] [setPreferredMemoryType]: Using org. xvolks. jnative. pointers. memory. heapMemoryBlock memory reservation strategyhttp ://-? 17001: 100872014-5-25 22:48:46, [DEBUG] [org. xvolks. jnative. JNative] [unLoad]: Unloading native library 'ehfscliaxdll. dll'
The Return Value Type of the getUrl method is const char *, and the result is normal. The return value type of getPlayUrl is const wchar_t *. Although the return value is read through getUnicodeMemoryAsString, it is not the first character, however, Chinese garbled characters have not been solved.
Download the learning project source code: http://download.csdn.net/detail/testcs_dn/7400203

 

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.