Search Unity

How to get unity3d game exe window handle

Discussion in 'Editor & General Support' started by Handsome-Wisely, Jan 5, 2016.

  1. Handsome-Wisely

    Handsome-Wisely

    Joined:
    Mar 20, 2013
    Posts:
    102
    i want to get game *.exe window handle.

    i know i can getActiveHandle or enum all the desktop windows to find it.

    but i wanna get it directlry by scripts. i get it in my u3d scripts . not by another exe. is any API or net function.

    i want to like this:

    Code (CSharp):
    1. void Start()
    2. {
    3. IntPrt u3dGameWnd = /*...*/;
    4. }
    thanks very much.
     
  2. Handsome-Wisely

    Handsome-Wisely

    Joined:
    Mar 20, 2013
    Posts:
    102
    who knows. moderator thanks
     
  3. Handsome-Wisely

    Handsome-Wisely

    Joined:
    Mar 20, 2013
    Posts:
    102
    why dont you let him go!
     
  4. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,900
    You could do it like this:
    Code (csharp):
    1.  
    2. using System.Runtime.InteropServices;
    3. using System;
    4. ...
    5.  
    6. public class Test
    7. {
    8.         [DllImport("user32.dll", CharSet = CharSet.Ansi)]
    9.         public static extern IntPtr FindWindow(string strClassName, string strWindowName);
    10.  
    11.         public static IntPtr GetWindowHandle()
    12.         {
    13.              return FindWindow("UnityWndClass", null);
    14.         }
    15. }
    16.  
    17.  
    Of course this is not the best way, as it searches depending on the class window, so if you have two windows player running, it might return incorrect one. But this might give you a hint, how to do such stuff.

    P.S This won't work in Editor.
     
  5. JaredThomson

    JaredThomson

    Joined:
    Nov 15, 2013
    Posts:
    16
    Hey, @Tomas1856

    Unity really needs a way to do this reliably that doesn't depend on the game window name, to avoid conflicts.

    Multiplayer developers need a way to launch multiple copies of the game to different X,Y positions on the screen, and set their window titles uniquely so they can be possible thidentified. Sadly this isn't possible through the Unity API and if we need to implement this functionality ourselves we need the HWND to do so.

    I'll submit a bug report for this as well.

    Thanks
     
    Last edited: Feb 22, 2021
  6. JaredThomson

    JaredThomson

    Joined:
    Nov 15, 2013
    Posts:
    16
    Alright, I managed to scrape together a working solution.


    Code (CSharp):
    1. [DllImport("user32.dll")]
    2. static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
    3.  
    4. [DllImport("Kernel32.dll")]
    5. static extern int GetCurrentThreadId();
    6.  
    7. static IntPtr GetWindowHandle()
    8. {
    9.     IntPtr returnHwnd = IntPtr.Zero;
    10.     var threadId = GetCurrentThreadId();
    11.     EnumThreadWindows(threadId,
    12.         (hWnd, lParam) => {
    13.             if(returnHwnd == IntPtr.Zero) returnHwnd = hWnd;
    14.             return true;
    15.         }, IntPtr.Zero);
    16.     return returnHwnd;
    17. }
    I can then use both of these functions I want to use:


    Code (CSharp):
    1. [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
    2. public static extern IntPtr SetWindowPos(IntPtr hWnd, int hWndInsertAfter, int x, int Y, int cx, int cy, int wFlags);
    3. [DllImport("user32.dll", EntryPoint = "SetWindowText")]
    4. public static extern bool SetWindowText(System.IntPtr hwnd, System.String lpString);

    For any questions on what parameters to pass to those functions, see microsoft's documentation on them.

    Edit: User Bunny83 has a good note on using lambdas with yhese callbacks here: https://forum.unity.com/threads/how...-of-the-games-own-window.528444/#post-6798728

    Basically: Lambdas in native callbacks will break with il2cpp
     
    Last edited: Feb 22, 2021
    Threeyes likes this.
  7. JaredThomson

    JaredThomson

    Joined:
    Nov 15, 2013
    Posts:
    16
    Last edited: Feb 22, 2021
  8. baize97

    baize97

    Joined:
    Jun 23, 2020
    Posts:
    30
    Hi @Tomas1856, I tried to use user32.dll on Mac OSX, but it failed. Because it is Microsoft's API. I searched a lot of information and couldn't find a way to do the same job on Mac. What should I do on Mac OSX? For example, change the name of the window;
     
    00bushem likes this.
  9. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,900
    Sadly I don't know how to achieve this on OSX
     
    baize97 likes this.