[Python.NET] Trying to constructu a System.IntPtr object

Mark McMahon mark.mcmahon at eur.autodesk.com
Fri Jul 29 22:02:14 CEST 2005

Hi Brian,

Well it seems to work :-) (or at least doesn't give me any errors.

Though it seems like Control.FromHandle needs to be in the same process
as the control :-(

Thanks anyway

-----Original Message-----
From: Brian Lloyd [mailto:brian at zope.com] 
Sent: Friday, July 29, 2005 3:38 PM
To: Mark McMahon; pythondotnet at python.org
Subject: RE: [Python.NET] Trying to constructu a System.IntPtr object

> I am stumped by this one - googled, even read the Readme!!
> I want to call System.Forms.Control.FromHandle() to get a control from
> handle. But I am getting that handle as Python int instance but
> FromHandle requires a System.IntPtr.
> I would have guess that there would be automatic conversion from
> int to .Net IntPtr.
> >>> CLR.System.IntPtr(23)
> Traceback (most recent call last):
>   File "<stdin>", line 1, in ?
> TypeError: 'int' value cannot be converted to System.IntPtr
> So I try converting it to Int32 first... (same for Int64)
> ...

ooo - you've found a hole in the type conversion logic, methinks.

In the interim, you can use the following sneaky hack:

  from CLR.System import IntPtr, Int32

  i = Int32(32)
  p = IntPtr.op_Explicit(i)

  # now p is an intptr...

Brian Lloyd        brian at zope.com
V.P. Engineering   540.361.1716              
Zope Corporation   http://www.zope.com 

More information about the PythonDotNet mailing list