JRuby meets the Windows API
With the addition of Java Native Access (JNA) to JRuby, systems
programmers using JRuby now have greater flexibility in terms of
interfacing with underlying operating system.
Some Ruby users are familiar with the ‘Win32API’ library that ships as
part of the Ruby standard library. That library lets you interface with
the Windows API by defining function pointers from specific DLL’s that
you later call. With JRuby’s JNA interface you can now interface with
Windows in a similar fashion.
Getting Started
The first thing we need to do is require the ‘java’ library.
require 'java'
With that out of the way, we’ll then want to get an instance of the
DLL we need to export functions from. I’ll use Kernel32 for our
purposes.
Kernel32 = com.sun.jna.NativeLibrary.getInstance('kernel32')
If you’re familiar with the Windows API, we’ve effectively called the LoadLibrary()
function which maps the kernel32 Windows module into our address space,
wrapped by our ‘Kernel32′ variable. From here we can define any
functions that the kernel32.dll exports. For demonstration purposes
I’ll define the GetCurrentDirectoryA() function:
GetCurrentDirectoryA = Kernel32.getFunction('GetCurrentDirectoryA')
This effectively calls the Windows GetProcAddress() function behind the scenes, returning a function pointer address, wrapped in our ‘GetCurrentDirectoryA’ variable.
Read full article here
Read the full article here
|