# Is ruby effective for experimenting with Win32 API?

**URL:** <https://www.ruby-forum.com/t/is-ruby-effective-for-experimenting-with-win32-api/148422>\
**Category:** Ruby\
**Created:** [September 12, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/is-ruby-effective-for-experimenting-with-win32-api/148422 "2008-09-12T17:42:30Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siegfried\_Heintze](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/96bed5/32.png) [@Siegfried\_Heintze](https://www.ruby-forum.com/u/Siegfried_Heintze)\
**Post date:** [September 12, 2008, 5:42pm UTC](https://www.ruby-forum.com/t/is-ruby-effective-for-experimenting-with-win32-api/148422/1 "2008-09-12T17:42:30Z")

</div>

I need to understand some User32.dll functions and I’m making an  
elaborate  
GUI so I can easily experiment with different parameters. This is taking  
a  
long time. I’m new to python and ruby and I’m thinking it would sure be  
nice  
to have an interpreter I can type a few lines of code into and test  
things.

(1) Would CRuby be a good choice for this? How about iron ruby? How  
about  
JRuby (probably not).  
(2) What about callbacks? Which rubys can handle callbacks? I read on  
msdn  
that you cannot use managed code to call SetWindowsHook and supply a  
call  
back (with two exceptions). I guess that means I cannot use iron ruby.  
What  
about C-ruby for playing with hooks?  
(3) How easy it it define the C structs, enums and integer constants  
necessary to call the windows API functions?  
(4) Here is the code I’m struggling with presently (it is just not  
cooperating: no errors just wrong results! I expect to be able give it a  
scan code, get a virtual key code, change the arguments, give it the  
virtual  
key code and get the original scan code back again).

```
    public const UInt32 MAPVK_VK_TO_VSC = 0, MAPVK_VSC_TO_VK = 1,

```

MAPVK\_VK\_TO\_CHAR = 2, MAPVK\_VSC\_TO\_VK\_EX = 3, MAPVK\_VK\_TO\_VSC\_EX = 4;  
public const UInt32 KLF\_ACTIVATE = 1, KLF\_SUBSTITUTE\_OK = 2,  
KLF\_REORDER = 8, KLF\_REPLACELANG = 0x10, KLF\_NOTELLSHELL = 0x80,  
KLF\_SETFORPROCESS = 0x00100, KLF\_SHIFTLOCK = 0x10000, KLF\_RESET =  
0x40000000;  
[DllImport(“user32.dll”)] static extern IntPtr  
LoadKeyboardLayout(string pwszKLID, uint Flags);  
[DllImport(“user32.dll”)] static extern bool  
UnloadKeyboardLayout(IntPtr hkl);  
[DllImport(“user32.dll”)] static extern uint  
MapVirtualKeyEx(uint  
uCode, uint uMapType, IntPtr dwhkl);  
private void Compute()  
{  
IntPtr hkl = LoadKeyboardLayout(sLangId\_, KLF\_ACTIVATE |  
KLF\_SUBSTITUTE\_OK | KLF\_REPLACELANG);  
uResult\_ = MapVirtualKeyEx(uCode\_, uMapType\_, hkl);  
UpdateOutput();  
}  
Would it be easy to execute this in the CRuby interpreter or am I better  
off  
sticking with C#?

Thanks!  
Siegfried
