WindowsHandler.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using UnityEngine;
  9. namespace WindowsHandler
  10. {
  11. public class User32API
  12. {
  13. private static Hashtable processWnd = null;
  14. static User32API()
  15. {
  16. if (processWnd == null)
  17. {
  18. processWnd = new Hashtable();
  19. }
  20. }
  21. [StructLayout(LayoutKind.Sequential)]
  22. public struct ProcessEntry32
  23. {
  24. public uint dwSize;
  25. public uint cntUsage;
  26. public uint th32ProcessID;
  27. public IntPtr th32DefaultHeapID;
  28. public uint th32ModuleID;
  29. public uint cntThreads;
  30. public uint th32ParentProcessID;
  31. public int pcPriClassBase;
  32. public uint dwFlags;
  33. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
  34. public string szExeFile;
  35. }
  36. //得到窗口客户区大小
  37. [DllImport("user32.dll")]
  38. public static extern int GetClientRect(IntPtr hwnd, out Rect lpRect);
  39. //得到窗口大小和坐标
  40. [DllImport("user32.dll")]
  41. public static extern int GetWindowRect(IntPtr hwnd, out Rect lpRect);
  42. /// <summary>
  43. /// 一个窗口区域的结构体
  44. /// </summary>
  45. public struct Rect
  46. {
  47. public int Left;
  48. public int Top;
  49. public int Right;
  50. public int Bottom;
  51. }
  52. public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
  53. [DllImport("user32.dll")]
  54. public static extern bool ShowWindow(IntPtr hwnd, int nCmdShow);
  55. [DllImport("user32.dll")]
  56. public static extern long GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
  57. [DllImport("KERNEL32.DLL ")]
  58. public static extern IntPtr CreateToolhelp32Snapshot(uint flags, uint processid);
  59. [DllImport("KERNEL32.DLL ")]
  60. public static extern int CloseHandle(IntPtr handle);
  61. [DllImport("KERNEL32.DLL ")]
  62. public static extern int Process32First(IntPtr handle, ref ProcessEntry32 pe);
  63. [DllImport("KERNEL32.DLL ")]
  64. public static extern int Process32Next(IntPtr handle, ref ProcessEntry32 pe);
  65. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  66. private static extern int SendMessage(int hWnd, int Msg, int wParam, string lParam);
  67. [DllImport("user32", EntryPoint = "IsWindow")]
  68. private static extern bool IsWindow(IntPtr hWnd);
  69. [DllImport("user32.dll")]
  70. public static extern int SetParent(IntPtr hWndChild, IntPtr hWndParent);
  71. [DllImport("user32.dll", ExactSpelling = true, CharSet = CharSet.Auto)]
  72. public static extern IntPtr GetParent(IntPtr hWnd);
  73. [DllImport("user32.dll", EntryPoint = "SetWindowPos")]
  74. public static extern bool SetWindowPos(IntPtr hWnd, int hWndInsertAfter,
  75. int X, int Y, int cx, int cy, uint uFlags);
  76. [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
  77. public static extern void SetLastError(uint dwErrCode);
  78. [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
  79. public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
  80. [DllImport("user32.dll", EntryPoint = "FindWindow")]
  81. public static extern IntPtr FindWindow(string IpClassName, string IpWindowName);
  82. public static IntPtr GetHandleByProcessName(string ProcessName)
  83. {
  84. List<ProcessEntry32> list = new List<ProcessEntry32>();
  85. IntPtr handle = CreateToolhelp32Snapshot(0x2, 0);
  86. IntPtr hh = IntPtr.Zero;
  87. if ((int)handle > 0)
  88. {
  89. ProcessEntry32 pe32 = new ProcessEntry32();
  90. pe32.dwSize = (uint)Marshal.SizeOf(pe32);
  91. int bMore = Process32First(handle, ref pe32);
  92. while (bMore == 1)
  93. {
  94. IntPtr temp = Marshal.AllocHGlobal((int)pe32.dwSize);
  95. Marshal.StructureToPtr(pe32, temp, true);
  96. ProcessEntry32 pe = (ProcessEntry32)Marshal.PtrToStructure(temp, typeof(ProcessEntry32));
  97. Marshal.FreeHGlobal(temp);
  98. list.Add(pe);
  99. UnityEngine.Debug.LogError(pe.szExeFile);
  100. if (pe.szExeFile == ProcessName)
  101. {
  102. bMore = 2;
  103. hh = GetCurrentWindowHandle(pe.th32ProcessID);
  104. ShowWindow( hh,2);
  105. Application.Quit();
  106. break;
  107. }
  108. bMore = Process32Next(handle, ref pe32);
  109. }
  110. }
  111. return hh;
  112. }
  113. [MonoPInvokeCallback]
  114. public static IntPtr GetCurrentWindowHandle(uint proid)
  115. {
  116. IntPtr ptrWnd = IntPtr.Zero;
  117. uint uiPid = proid;
  118. object objWnd = processWnd[uiPid];
  119. if (objWnd != null)
  120. {
  121. ptrWnd = (IntPtr)objWnd;
  122. if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
  123. {
  124. return ptrWnd;
  125. }
  126. else
  127. {
  128. ptrWnd = IntPtr.Zero;
  129. }
  130. }
  131. bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
  132. // 枚举窗口返回 false 并且没有错误号时表明获取成功
  133. if (!bResult && Marshal.GetLastWin32Error() == 0)
  134. {
  135. objWnd = processWnd[uiPid];
  136. if (objWnd != null)
  137. {
  138. ptrWnd = (IntPtr)objWnd;
  139. }
  140. }
  141. return ptrWnd;
  142. }
  143. [MonoPInvokeCallback]
  144. public static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
  145. {
  146. uint uiPid = 0;
  147. if (GetParent(hwnd) == IntPtr.Zero)
  148. {
  149. GetWindowThreadProcessId(hwnd, ref uiPid);
  150. if (uiPid == lParam) // 找到进程对应的主窗口句柄
  151. {
  152. processWnd.Add(uiPid, hwnd); // 把句柄缓存起来
  153. SetLastError(0); // 设置无错误
  154. return false; // 返回 false 以终止枚举窗口
  155. }
  156. }
  157. return true;
  158. }
  159. }
  160. }
  161. public class MonoPInvokeCallbackAttribute : Attribute
  162. {
  163. public MonoPInvokeCallbackAttribute() { }
  164. }