Skip to content

Instantly share code, notes, and snippets.

@mattbenic
Last active June 11, 2023 14:40
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mattbenic/908483ad0bedbc62ab17 to your computer and use it in GitHub Desktop.
Save mattbenic/908483ad0bedbc62ab17 to your computer and use it in GitHub Desktop.
Example of using Win32 API to get specific window instance in Unity. This is useful for when running multiple instances of the same application, as the window handle can then be used to correctly position and size the different windows for multi screen use.
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.Text;
public class SpecificInstanceOfGameExample : MonoBehaviour
{
#region DLL Imports
private const string UnityWindowClassName = "UnityWndClass";
[DllImport("kernel32.dll")]
static extern uint GetCurrentThreadId();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumThreadWindows(uint dwThreadId, EnumWindowsProc lpEnumFunc, IntPtr lParam);
#endregion
#region Private fields
private static IntPtr windowHandle = IntPtr.Zero;
#endregion
#region Monobehavior implementation
/// <summary>
/// Called when this component is initialized
/// </summary>
void Start()
{
uint threadId = GetCurrentThreadId();
EnumThreadWindows(threadId, (hWnd, lParam) =>
{
var classText = new StringBuilder(UnityWindowClassName.Length + 1);
GetClassName(hWnd, classText, classText.Capacity);
if (classText.ToString() == UnityWindowClassName)
{
windowHandle = hWnd;
return false;
}
return true;
}, IntPtr.Zero);
Debug.Log(string.Format("Window Handle: {0}", windowHandle));
}
void OnGUI()
{
GUILayout.Label("Window Handle: " + windowHandle);
}
#endregion
}
@mattbenic
Copy link
Author

Changed to use GetClassName instead of GetWindowText because GetWindowText returns an empty string on some versions of Windows with certain security settings (so far GetClassName doesn't seem to have this problem). This is also better because it doesn't require the user to set a window name.

@krooq
Copy link

krooq commented Oct 7, 2021

Hey man, I gave this a go and I get a segfault.
Is there anything special you had to do to get it working?
I just copied my C:\Windows\System32\user32.dll and C:\Windows\System32\kernel32.dll to my Assets folder.

Mind you, it appears to work, i.e. I see the debug GUI showing a window handle, but crashes immediately after.

@mattbenic
Copy link
Author

Hi there, no I don't recall having to do anything specific. It was approximately a million years ago :) But I don't think you should need to copy those dlls either, since most systems should have System32 on PATH? I would actually think including those dlls in your build might actually be risky, you can't be guaranteed that they would be correct for all machines the game gets installed on.

@AdamBuchweitz
Copy link

Thanks for this!

@mattbenic
Copy link
Author

@AdamBuchweitz glad it helped someone 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment