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