This post originated from an RSS feed registered with Ruby Buzz
by Daniel Berger.
Original Post: Revamping Win32API
Feed Title: Testing 1,2,3...
Feed URL: http://djberg96.livejournal.com/data/rss
Feed Description: A blog on Ruby and other stuff.
In the CVS repo for the Win32 Utils project you'll find a project called "windows-api". It's my attempt to cleanup and revamp the Win32API package. The first iteration didn't do much more than some basic code cleanup and add instance variables for various bits of information that were previously unreachable.
Certain things still bothered me, though. The sorts of things that only start to bother you after prolonged, heavy use. First, I've realized that the majority of the functions that I define are coming from kernel32, yet there's no way to set a default library. Second, about half the functions return BOOL, with the rest returning some form of long (usually a HANDLE), or are void functions, yet there's no way to set a default return value. Lastly, the idiom of assigning the function name to a constant of the same name one call at a time is very tedious. I've decided that I want something that will automatically create that constant for me.
My second iteration dealt with two of these issues. So now the interface looks like this:
Function = Windows::API.new('Function', prototype='V', return='I', dll='kernel32')
That's better than what we've got currently, but it still means manually assigning constants, one method call at a time, not to mention manually defining an equivalent method:
def Function(arg1, arg2, ...)
Function.call(arg1, arg2, ...)
end
I've been kinda mulling over what a better interface might look like. Perhaps it was fate that I stumbled across Win32-API-Interface today. I think Sascha has some good ideas. Using her API for ideas, now I'm thinking of adding something like this:
module Foo
Windows::API.create do
:kernel32 => [
['GetTempPath', 'LP', 'N' ],
['GetCurrentProcessId', 'V', 'L', 'get_pid']
],
:user32 => [
['GetCursorPos', 'P', 'I']
],
end
end
# and later...
include Foo
path = GetTempPath(arg1, arg2)
pid = get_pid
This adds one extra clever bit as well. The last array argument would be an auto generated alias that would be exported instead of (or in addition to) the normal constant.