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