[Win32utils-devel] JRuby, JNA, Windows

Daniel Berger djberg96 at gmail.com
Thu Jan 24 00:09:00 EST 2008


Hi all,

Here's a short example of how to interface with JRuby and the Windows 
API I thought I'd share:

require 'java'

# LoadLibrary()
Kernel32 = com.sun.jna.NativeLibrary.getInstance("kernel32")

# GetProcAddress()
GetCurrentDirectoryA = Kernel32.getFunction('GetCurrentDirectoryA')

# You can also do -> buf = java.nio.ByteBuffer.allocate(256)
buf = Array.new(256).to_java(:byte)

GetCurrentDirectoryA.invokeInt([256, buf].to_java)
buf = java.lang.String.new(buf)
p buf.to_s.strip

It's a little clunkier than MRI at the moment, because Java strings are 
immutable. We're passing by reference, so we have to pass a ByteBuffer 
instead, and convert back and forth. Charles tells me that this will be 
hopefully be more seamless in the future. You could always monkey patch 
the String class, too.

But, on the whole, not bad. I'll probably do a little writeup about it 
on the O'Reilly blog soon.

Regards,

Dan


More information about the win32utils-devel mailing list