# Leveraging StdLib.Win32API class

**URL:** <https://www.ruby-forum.com/t/leveraging-stdlib-win32api-class/179603>\
**Category:** IronRuby\
**Created:** [November 30, 2009, 8:04am UTC](https://www.ruby-forum.com/t/leveraging-stdlib-win32api-class/179603 "2009-11-30T08:04:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [November 30, 2009, 8:04am UTC](https://www.ruby-forum.com/t/leveraging-stdlib-win32api-class/179603/1 "2009-11-30T08:04:37Z")

</div>

Hi all,

I’m working on using metasm ([http://metasm.cr0.org/](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,  
James

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [November 30, 2009, 7:54pm UTC](https://www.ruby-forum.com/t/leveraging-stdlib-win32api-class/179603/2 "2009-11-30T19:54:47Z")

</div>

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

Tomas

---

<div class="post-metadata">

**Author:** ![jameslabocki](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jameslabocki](https://www.ruby-forum.com/u/jameslabocki)\
**Post date:** [December 5, 2009, 3:31am UTC](https://www.ruby-forum.com/t/leveraging-stdlib-win32api-class/179603/3 "2009-12-05T03:31:59Z")

</div>

Hi Tomas,

I’ve been playing around with DynamicMethod and generating IL at  
runtime, with varying levels of success. Currently I’m using  
DynamicMethod to generate the IL to load arguments, load function  
address, calli and ret. My script gets access to the DynamicMethod  
object, which gets passed onto a helper CLR method along with arguments  
for some basic marshalling, before being passed onto  
DynamicMethod.Invoke.

I’ve noticed though that calling .Invoke() in this manner is a magnitude  
times slower than using the equivalant Win32API code. I figure I’m meant  
to use CreateDelegate instead of Invoke, though I’m not sure how this is  
meant to work since CreateDelegate expects a delegate type, whose  
signature isn’t known until runtime.

Regards,  
James

Tomas M. wrote:

> 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).
> 
> Tomas
