Ruby/DL SendInput

Hi,

I want to use Ruby/DL to call the Win32 user32.dll SendInput method (http://tinyurl.com/jrxm). Unfortunately this method takes some complex arguments; several nested structs, including a union and a pointer to an array of a certain struct etc. How can I use Ruby/DL to call this method?

Regards,

Peter

Did you try the Win32API module?

You code would look something like this:

require 'Win32API'

sendInput = Win32API.new("user32", "SendInput", ['I', 'P', 'I'], 'I')

# you'll have to pack the INPUT string in the appropiate manner
input = some_array.pack(some_fmt)

sendInput.call(num_inputs, input, sizeofINPUT)

···

On 5/8/05, Peter C. Verhage <usenet2@nospam.no-nonsense.org> wrote:

Hi,

I want to use Ruby/DL to call the Win32 user32.dll SendInput method
(http://tinyurl.com/jrxm\). Unfortunately this method takes some complex
arguments; several nested structs, including a union and a pointer to an
array of a certain struct etc. How can I use Ruby/DL to call this method?

Regards,

Peter

Peter C. Verhage wrote:

I want to use Ruby/DL to call the Win32 user32.dll SendInput method (http://tinyurl.com/jrxm\). Unfortunately this method takes some complex arguments; several nested structs, including a union and a pointer to an array of a certain struct etc. How can I use Ruby/DL to call this method?

I successfully called SendInput to take a screen snapshot via ruby-dl2-0.6.
A program I used is available at the following URL.

http://rubyforge.org/cgi-bin/viewcvs.cgi/*checkout*/dlcookbook/win32/sendinput.rb?rev=HEAD&cvsroot=dlcookbook&content-type=text/plain

Regards,

···

--
Takaaki Tateishi <ttate@ttsky.net>

Logan Capaldo wrote:

Did you try the Win32API module?

You code would look something like this:

require 'Win32API'

sendInput = Win32API.new("user32", "SendInput", ['I', 'P', 'I'], 'I')

# you'll have to pack the INPUT string in the appropiate manner
input = some_array.pack(some_fmt)

sendInput.call(num_inputs, input, sizeofINPUT)

I tried this when I saw the original email, but couldn't get it to work. Mapping those funky Windows types is tricky. But anyhow, here is the (non-working) code:

require 'Win32API'

SendInput = Win32API.new("user32", "SendInput", ['I','P','I'], 'I')
GetLastError = Win32API.new("kernel32", "GetLastError", , 'L')

INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2

KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_SCANCODE = 0x0003

# The Left and Right Windows keys
VK_LWIN = 0x5B
VK_RWIN = 0x5C

def send_key(key)
   keyboard_input = [INPUT_KEYBOARD, key[0], 0, 0, 0, 0].pack('LCCLLL')
   SendInput.call(1, keyboard_input, keyboard_input.size())
end

res = send_key('R')
puts "Got result: #{res}"
puts "GetLastError: #{GetLastError.call}"
__END__

If anyone gets this working, I'd like to hear about it.

Ryan

Takaaki Tateishi wrote:

I successfully called SendInput to take a screen snapshot via ruby-dl2-0.6.
A program I used is available at the following URL.

Code looks great! Seems really easy to map the C types to Ruby. Is there a binary version available for the One-Click-Installer (Ruby 1.8.x)? I tried the various ZIP files of Ruby-DL 2.0.6 but one needs MingW and the other one needs Cygwin.

Thanks,

Peter

Ryan Leavengood wrote:

If anyone gets this working, I'd like to hear about it.

Thanks for the responses / example code. Will try and see if I can get this to work this evening. Will keep you posted.

Regards,

Peter