[IronPython] ctypes .net interaction (casting a pointer to a .net object)

Dino Viehland dinov at microsoft.com
Sat Apr 30 20:52:16 CEST 2011

Sebastien wrote:
> In my effort to be able to use the GetRunningObjectTable in ironpython, I
> succeeded in recovering a pointer to a IBindCtx object by using the ctypes
> library.
> Is it possible to then "cast" this pointer (or transform it) to a IBindCtx
> Ironpython type on which I could call other methods ?

You should be able to call Marshal.GetObjectForIUnknown such as:

from System import IntPtr
from System.Runtime.InteropServices import Marshal
obj = Marshal.GetObjectForIUnknown(IntPtr(ptr))

>From there you should be able to do IBindCtx.GetRunningObjectTable(obj).
If the COM object supports automation or provides type info then you could 
just do obj.GetRunningObjectTable().

> And more generally, is it possible to use both the native .net objects in ipy
> and the ctypes library in ipy (and so transform from one world to the other) ?

I think the answer to this is generally yes.  In particular for COM objects you can 
because of the CLR and IronPython's support for COM interop.  If you're dealing 
with other plain old structures and you have a .NET version of them you'll just
need to copy all of the fields yourself .  

More information about the Ironpython-users mailing list