[Ironruby-core] Leveraging StdLib.Win32API class

Tomas Matousek Tomas.Matousek at microsoft.com
Mon Nov 30 13:54:19 EST 2009

I wouldn't recommend using Win32API. It's not well designed interop library. 

What you need is calli IL instruction. It takes a pointer to a function and arguments and calls the function. C# doesn't support this so you need some helpers. You can either emit them at run-time and call them via delegates (that's what our implementation of Win32API does), or you can write them in IL, if you know the signatures you need, compile them to an assembly (ilasm ILHelpers.il /dll /out=ILHelpers.dll) and use the assembly directly from Ruby (see attached file).


-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of James Leskovar
Sent: Sunday, November 29, 2009 11:05 PM
To: ironruby-core at rubyforge.org
Subject: [Ironruby-core] Leveraging StdLib.Win32API class

Hi all,

I'm working on using metasm (http://metasm.cr0.org/) with IronRuby in order to generate and ultimately execute code at runtime. At the moment, I've been successful in using metasm to get back a string containing the encoded x86 instruction opcodes. The next step would be to allocate memory for the code, copy over the encoded instructions, mark the region as executable, and get back an IntPtr.

Which brings me to Win32API. I think using Win32API would probably be the easiest way in order to get my code running, though currently there's no way to directly specify the function pointer of the function to be called (initializer only supports module and export name.) I've noticed that the Win32API CLR class seems to have a Function property, but no way to utilize it from ruby code.

Are there any suggestions about how I could use the Win32API class, or any alternate solutions?

Thanks in advance,
Posted via http://www.ruby-forum.com/.
Ironruby-core mailing list
Ironruby-core at rubyforge.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: ILHelpers.il
Type: application/octet-stream
Size: 1002 bytes
Desc: ILHelpers.il
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20091130/4a425001/attachment.obj>

More information about the Ironruby-core mailing list