how to get the OS window handle in the game?


I need to get the window handle for the game window in BGE using Python. Does the API documentation have anything that will help me get the window handle?

Thanks for any help.


What do you mean the ‘window handle’?
The thing up the top you can click and drag to move the window around the screen? Why do you want that?

It seems unlikely that the BGE has a generic way to access a “window handle”. What that object is would be different between Windows, Linux, and MacOS. If you really want it then I suspect that you will have to write a native library that will use OS calls to get the equivalent of the window’s handle. You would have to write a custom version of that for each OS and probably wrap it in some kind of custom Python object to abstract it.

Since nobody seems to know what he’s talking about, I’ll explain the question. In programming, a “handle” is unique ID. All kinds of things have handles; pretty much anything that there are multiples of, like windows, programs, objects, etc. You can modify these things directly, but only if you have their handle. Think of it like an address. It points you in the right direction. For some reason, cyrfer wants to get this number. I’m not sure what good it will do him in the BGE, but that’s essentially what he’s askind for. Unfortunately, I don’t have the answer to this question, but hopefully this clears things up for some people.

You should read my post more carefully.

My point was that the BGE already has an abstraction to hide the difference between different windowing systems. I specifically noted that it was likely that if one wanted the “window handle” then one would have to write a native C/C++ library that accessed that data directly (because Win32/Linux/OSX system APIs are C/C++ compatible.) I know that on Windows it is trivial to access the “HWND” for the current process if you can call into the Win32 API. The problem is that would only work on Windows systems and a completely different set of APIs would have to be called on Linux and OSX to get and manipulate the handle. So it still seems unlikely that Blender provides an API that will access the window handle because each OS provides a completely different set of APIs to manipulate that handle.

The answer still is:

  • If you only care about 1 OS then write a C/C++ routine that calls the native system libraries to access the window handle.
  • If you want it to work on all Blender supported OSs then you have to write native libraries that access the window handle for each system and also write some kind of new abstraction to make all the system APIs work in a similar fashion.

Thanks for trying. Yes, doing OS native things in an abstraction layer enforced with Python bindings is tricky. In the end, I used the built-in Python module ‘ctypes’ to gain access to OS native objects.

The Windows API to retrive a handle to the current window is the function ‘GetActiveWindow’.