Here is a tiny note on how to invoke native exported DLL functions from Java code in Windows.
According to the Wiki: "Java Native Access (JNA) provides Java programs easy access to native shared libraries without using the Java Native Interface. JNA's design aims to provide native access in a natural way with a minimum of effort. Noboilerplate or generated glue code is required."
I wrote a DLL in FASM to make it more fun.
Lib.asm
1: format PE GUI 4.0 DLL
2: entry DllEntryPoint
3:
4: include 'win32ax.INC'
5:
6: section ".data" data readable writable
7: f1_msg db "This is Function 1",0
8: f2_msg db "This is Function 2",0
9:
10: section '.text' code readable executable
11:
12: proc DllEntryPoint hinstDLL,fdwReason,lpvReserved
13: mov eax,TRUE
14: ret
15: endp
16:
17: ; VOID Func1
18: proc Func1
19: invoke MessageBox, 0, addr f1_msg, 0, 0
20: ret
21: endp
22:
23: ; VOID Func2
24: proc Func2
25: invoke MessageBox, 0, addr f2_msg, 0, 0
26: ret
27: endp
28:
29: section '.idata' import data readable writeable
30: library user,'USER32.DLL'
31:
32: import user, MessageBox,'MessageBoxA'
33:
34: section '.edata' export data readable
35: export 'LIB.DLL', \
36: Func1,'Func1', \
37: Func2,'Func2'
38:
39: section '.reloc' fixups data readable discardable
40:
Load.java
1: import com.sun.jna.Library;
2: import com.sun.jna.Native;
3: import com.sun.jna.Platform;
4:
5: public class Load {
6:
7: public interface TestLib extends Library {
8: TestLib INSTANCE = (TestLib)
9: Native.loadLibrary("Lib",TestLib.class);
10:
11: void Func1();
12: void Func2();
13: }
14:
15: public static void main(String[] args) {
16: System.out.println("Start");
17: TestLib.INSTANCE.Func1();
18: TestLib.INSTANCE.Func2();
19: System.out.println("Done");
20: }
21: }
Download JNA and make sure that it is in your CLASSPATH environment variable.
Now, it's time to compile and run.
1: fasm Lib.asm
2: javac Load.java
3: jar cvfe Load.jar Load *.class
4: java -jar Load.jar
That's it. Pretty simple.
This is a great work by using DLL function of Java programming. By proper use of JavaScript coding, one can make various function depending upon knowledge and skills of the programmer.
ReplyDelete