comparison java/jdc/examples/UnixLibCExample.java @ 22:4ee8d6aa7721

- java binding cleanup, exposed free functions (not very oo, this entire binding) - java binding examples
author cslag
date Sat, 09 Apr 2016 18:18:34 -0500
parents
children
comparison
equal deleted inserted replaced
21:40a2c4198016 22:4ee8d6aa7721
1 import org.dyncall.DC;
2
3 // This example tries to make use of each call in the binding.
4 class UnixLibCExample
5 {
6 public static void main(String args[])
7 {
8 long lib = DC.loadLibrary("/usr/lib/libc.so");
9 System.out.format("libc.so handle: %x\n", lib);
10
11 long f0 = DC.find(lib, "strlen");
12 System.out.format("found strlen at: %x\n", f0);
13
14 long f1 = DC.find(lib, "strstr");
15 System.out.format("found strstr at: %x\n", f1);
16
17 long f2 = DC.find(lib, "strncmp");
18 System.out.format("found strncmp at: %x\n", f2);
19
20 long vm = DC.newCallVM(4096);
21 DC.reset(vm);
22 System.out.format("dcGetError() = %d\n", DC.getError(vm));
23 DC.mode(vm, 1234567); // bogus mode, should set error
24 System.out.format("dcGetError() = %d (after trying to set bogus mode)\n", DC.getError(vm));
25 DC.mode(vm, DC.C_DEFAULT); // good mode, should clear error
26 System.out.format("dcGetError() = %d (after setting valid mode)\n", DC.getError(vm));
27
28 // String param.
29 DC.argString(vm, "This is a Java string of 33 chars");
30 int r0 = DC.callInt(vm, f0);
31 System.out.format("strlen(\"This is a Java string of 33 chars\") = %d\n", r0);
32
33 // String param and string/pointer return.
34 DC.reset(vm);
35 DC.argString(vm, "This is a Java string");
36 DC.argString(vm, "Java");
37 String r1 = DC.callString(vm, f1); // will return a copy of the output string, not like a pointer in C
38 System.out.format("strlen(\"This is a Java string\", \"Java\") = %s\n", r1);
39 long r1l = DC.callPointer(vm, f1); // test returning the pointer as long
40 System.out.format("strlen(\"This is a Java string\", \"Java\") = 0x%x (returned pointer, pointless here, but serves as example)\n", r1l);
41
42 DC.reset(vm);
43 DC.argString(vm, "This is a Java string");
44 DC.argString(vm, "This is");
45 DC.argInt(vm, 6);
46 int r2 = DC.callInt(vm, f2);
47 System.out.format("strlen(\"This is a Java string\", \"This is\", 6) = %d\n", r2);
48 DC.reset(vm);
49 DC.argString(vm, "This is a Java string");
50 DC.argString(vm, "This is");
51 DC.argInt(vm, 8);
52 r2 = DC.callInt(vm, f2);
53 System.out.format("strlen(\"This is a Java string\", \"This is\", 8) = %d\n", r2);
54
55 //DC.reset(vm);
56 //DC.argDouble(vm, 2.);
57 //DC.argDouble(vm, 10.);
58 //double r1 = DC.callDouble(vm, f1);
59 //System.out.format("pow(2., 10.) = %f\n", r1);
60
61 // Done, cleanup.
62 DC.freeCallVM(vm);
63 DC.freeLibrary(lib);
64 }
65 }
66