Ruby/DL SendInput

  • Thread starter Peter C. Verhage
  • Start date
P

Peter C. Verhage

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
 
L

Logan Capaldo

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)
 
R

Ryan Leavengood

Logan said:
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
 
G

Guest

Ryan said:
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
 
T

Takaaki Tateishi

Peter said:
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/viewcv...AD&cvsroot=dlcookbook&content-type=text/plain

Regards,
 
P

Peter C. Verhage

Takaaki said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top