Skip to content
Newer
Older
100644 38 lines (29 sloc) 1.08 KB
8b22412 @dblock The rest of the documentation.
dblock authored May 23, 2011
1 Dynamically Typed Languages
2 ===========================
3
4 Languages such as Jython or JRuby may find it more convenient to access the `NativeLibrary` and Function classes directly rather than establishing a dedicated interface.
5 Here's a brief example of using JNA from JRuby:
6
7 require 'java'
8
9 module Libc
10 @@lib = com.sun.jna.NativeLibrary.getInstance("c")
11 @@ptr_funcs = [ 'fopen', 'malloc', 'calloc' ]
12 def self.method_missing(meth, *args)
13 if @@ptr_funcs.include?(meth.to_s)
14 @@lib.getFunction(meth.to_s).invokePointer(args.to_java)
15 else
16 @@lib.getFunction(meth.to_s).invokeInt(args.to_java)
17 end
18 end
19 O_RDONLY = 0
20 O_WRONLY = 1
21 O_RDWR = 2
22 end
23
24 Libc.puts("puts from libc")
25 Libc.printf("Hello %s, from printf\n", "World")
26
27 file = Libc.open("/dev/stdout", 1, Libc::O_WRONLY)
28 n = Libc.write(file, "Test\n", 5)
29 puts "Wrote #{n} bytes via Libc"
30
31 path = "/dev/stdout"
32 fp = Libc.fopen(path, "w+")
33 Libc.fprintf(fp, "fprintf to %s via stdio\n", path)
34 Libc.fflush(fp)
35 Libc.fclose(fp)
36
37
Something went wrong with that request. Please try again.