private static bool EnumWindowsCallback(IntPtr testWindowHandle, IntPtr includeChildren) { string title = MyEnumWindows.GetWindowTitle(testWindowHandle); if (MyEnumWindows.TitleMatches(title)) { try { uint id; GetWindowThreadProcessId(testWindowHandle, out id); Process p = Process.GetProcessById((int)id); // For the moment, I found no workaround to get main Unity processes only. if (p.ProcessName == "Unity") { MyEnumWindows.windowTitles.Add(title); } } catch { } } if (includeChildren.Equals(IntPtr.Zero) == false) { MyEnumWindows.EnumChildWindows(testWindowHandle, MyEnumWindows.EnumWindowsCallback, IntPtr.Zero); } return(true); }
public static string GetWindowTitle(IntPtr windowHandle) { uint SMTO_ABORTIFHUNG = 0x0002; uint WM_GETTEXT = 0xD; int MAX_STRING_SIZE = 32768; IntPtr result; string title = string.Empty; IntPtr memoryHandle = Marshal.AllocCoTaskMem(MAX_STRING_SIZE); Marshal.Copy(title.ToCharArray(), 0, memoryHandle, title.Length); MyEnumWindows.SendMessageTimeout(windowHandle, WM_GETTEXT, (IntPtr)MAX_STRING_SIZE, memoryHandle, SMTO_ABORTIFHUNG, (uint)1000, out result); title = Marshal.PtrToStringAuto(memoryHandle); Marshal.FreeCoTaskMem(memoryHandle); return(title); }
private void UpdateProcesses() { //var pp = Process.GetProcesses(); //int max = 10000; //foreach (var item in pp) //{ // if (item.MainModule.FileName.Contains("Unity.exe") == true) // { // int i = 0; // while (!item.HasExited && ++i < max) // { // item.Refresh(); // if (item.MainWindowHandle.ToInt32() != 0) // { // //item.MainWindowHandle; // item.Refresh(); // Debug.Log(item + " " + item.MainWindowHandle + " " + item.Handle + " -" + item.MainWindowTitle); // break; // } // } // } //} //return; new Thread(() => { List <string> processes = MyEnumWindows.GetWindowTitles(false); // TODO If processes has 0, it failed. Need to find a workaround. if (processes.Count > 0) { this.unityProcessesDetected.Clear(); for (int i = 0; i < processes.Count; i++) { if (processes[i].Contains("Unity ") == true) { this.unityProcessesDetected.Add(processes[i]); } } EditorApplication.delayCall += this.Repaint; } }).Start(); }