I am trying to pass structure to Win32API call in ruby.
How can I do this?
A structure is not a pointer. So I can't code like following:
require 'win32api'
get_std_handle = Win32API.new('kernel32.dll',"GetStdHandle",['I'],'I')
h = get_std_handle.call(-11) # STD_OUTPUT_HANDLE
# ['I','P'] is wrong. COORD is not a pointer. it's a structure.
set_console_cursor_position = Win32API.new('kernel32.dll',"SetConsoleCursorPosition",['I','P'],'I')
p = [10,10].pack('S*')
set_console_cursor_position.call(h,p)
puts "hello" # since above code is wrong, this will not printed at position 10,10
So the question is: How to pass structure (not a pointer) to Win32API call?
The folks at RubyDoc have listed code for the source of their Win32Console gem, which uses this, as
def SetConsoleCursorPosition( hConsoleOutput, col, row )
@SetConsoleCursorPosition ||= Win32API.new( "kernel32", "SetConsoleCursorPosition", ['l', 'p'], 'l' )
dwCursorPosition = (row << 16) + col
@SetConsoleCursorPosition.call( hConsoleOutput, dwCursorPosition )
end
Reading How to pass structures as parameters to Win32API in Ruby (Array#pack and Array#unpack) may also shed some light for you.
Unfortunately I'm not sure about the answer to your follow-up comment, maybe you could ask that as another question, or someone that knows could add another answer or comment here or indeed, feel free to edit this answer to add that information.