Get Window Handle

(Ultimately I’m trying to change the title bar, but for a unity app running multiple instances on the same machine, and each app needs a title to match its purpose.)

I’m trying to get the windows Handle, so I can change the title.

All the posts I’ve found on this require 1 of two things. (neither work for me)

A) That the window will have focus.

  • via [DllImport(“user32.dll”, EntryPoint = “GetActiveWindow”)]
  • Cannot guarantee, I’m starting up to 4 instances at the same time. Only 1 will be in the fore front.

B) That you already know the title and will find the window by it.

  • via [DllImport(“user32.dll”, EntryPoint = “FindWindow”)]
  • Cannot guarantee. up to 4 windows will have the same title.

I’m looking for a solution to get the window handle for from the instance trying to get the handle.

  • Thanks!

Best way I can think of, since Unity tries to hide the fact that there is such a thing as a window, and Windows itself can’t assume that your process has only one window, is to enumerate all top-level windows, checking to see if their process ID is equal to the current process ID. As soon as you find a match, you know that it is the only match, since your process shouldn’t have any other windows. To do this, you’d use EnumWindows(), GetCurrentProcessId(), and GetWindowThreadProcessId().