Getting an integer fromRhino.Display.RhinoView.Handle in Python

I am trying to get a Python integer from the Rhino.Display.RhinoView.Handle object, but I cannot figure out how to do it. I think this is one of those easy C# to Python things, but I don’t know C# so this has become a hurdle.

So far, I have not been able to provide the correct question to the search engines.

Thanks for your help.

import Rhino
import System

print( Rhino.Display.RhinoView.Handle)         # <property 'IntPtr Handle'>
print( type(Rhino.Display.RhinoView.Handle) )  # <class 'CLR.PropertyObject'>


#With the kind support of Bing & Google AI summaries I have tried things like:
#handleAsInteger = int(Rhino.Display.RhinoView.Handle)
#handleAsInteger = Rhino.Display.RhinoView.Handle.Handle.ToInt64()
#handleAsInteger = System.Convert.ToInt32(Rhino.Display.RhinoView.Handle)
#handleAsInteger = Rhino.Display.RhinoView.Handle.ToInt64()
#handleAsInteger = Rhino.Display.RhinoView.Handle.Value()
#handleAsInteger = Rhino.Display.RhinoView.Handle.value

Hi @Henry_Wede,

Why do you need an int?

From Google:

An IntPtr is a platform-specific integer designed to hold a pointer or handle, so its size is 32 bits on a 32-bit system and 64 bits on a 64-bit system. An int in C# is always a 32-bit integer.

– Dale

Hello Dale,

Yes, I have spent a couple hours trying to find a solution to make the conversion. My failed attempts following examples from Google are shown in comments above. I tried to solve my own problem before asking for help.

I want the handle as an integer so that I can try to embed the window in a PySide application. I have done this before with other applications.

Can you get the raw bytes or address for the pointer?

It doesn’t seem like it. At least, not through the normal bytes function.

Some of the Internet says that a PropertyObject should have a ToInt32 method - but this object does not. Not sure if this is something that the Rhino folks had to include or if it just happens. Either way, it is not there.

BTW, searching for “CLR.PropertyObject” produced one other post on a pythonnet forum with a similar question. Of course, nobody answered that question :slight_smile:

I thought that I would try to revive this thread in case anyone else has an idea of how to turn a

Rhino.Display.RhinoView.Handle

which is a <class ‘CLR.PropertyObject’> into a Python integer. This is so that I can grab a Rhino window and embed it into a PySide program. My failed attempts are at the top of this thread.

I have been able to grab a window from Rhino using a brute force approach, but it really is not the goal because the PySide program does not have a reference to “the” Rhino or the document. The program cannot interact with the window even though it is embedded.

Here is Rhino missing its window… so sad.

The PySide app looking successful, but not really.

I am hopeful that once I get the proper window handle from RhinoCommon that the PySide methods will work properly. I don’t expect it to work correctly the first time, but right now I am stuck at the beginning.

Thanks for your help.

Hi @Henry_Wede,

This works:

#! python 3
import Rhino
import pywintypes
import win32gui

window_handle = Rhino.RhinoApp.MainWindowHandle()
integer_handle = window_handle.ToInt64()
py_handle = pywintypes.HANDLE(integer_handle) 
text = win32gui.GetWindowText(py_handle)
print(text)

– Dale