WinApi.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using PTMedicalInsurance.Variables;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Diagnostics;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12. namespace PTMedicalInsurance.Common
  13. {
  14. class WinApi
  15. {
  16. public static IntPtr GetWindowHandle(string name)
  17. {
  18. Process[] procs = Process.GetProcessesByName(name);
  19. if (procs.Length != 0)
  20. {
  21. return procs[0].MainWindowHandle;
  22. }
  23. return IntPtr.Zero;
  24. }
  25. private bool IsWindowExist(IntPtr handle)
  26. {
  27. return (!(GetWindow(new HandleRef(this, handle), 4) != IntPtr.Zero) && IsWindowVisible(new HandleRef(this, handle)));
  28. }
  29. [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
  30. public static extern IntPtr GetWindow(HandleRef hWnd, int uCmd);
  31. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  32. public static extern bool IsWindowVisible(HandleRef hWnd);
  33. /// <summary>
  34. /// 获取应用程序窗口句柄
  35. /// </summary>
  36. /// <param name="processId"></param>
  37. /// <returns></returns>
  38. private IntPtr GetWindowHandle(int processId)
  39. {
  40. var windowHandle = IntPtr.Zero;
  41. EnumThreadWindowsCallback windowsCallback = new EnumThreadWindowsCallback(FindMainWindow);
  42. EnumWindows(windowsCallback, IntPtr.Zero);
  43. //保持循环
  44. GC.KeepAlive(windowsCallback);
  45. bool FindMainWindow(IntPtr handle, IntPtr extraParameter)
  46. {
  47. int num;
  48. GetWindowThreadProcessId(new HandleRef(this, handle), out num);
  49. if (num == processId && IsWindowExist(handle))
  50. {
  51. windowHandle = handle;
  52. return true;
  53. }
  54. return false;
  55. }
  56. return windowHandle;
  57. }
  58. public delegate bool EnumThreadWindowsCallback(IntPtr hWnd, IntPtr lParam);
  59. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  60. public static extern bool EnumWindows(EnumThreadWindowsCallback callback, IntPtr extraData);
  61. [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
  62. public static extern int GetWindowThreadProcessId(HandleRef handle, out int processId);
  63. public static IntPtr GetWindowsHandle(string WindowName)
  64. {
  65. IntPtr hwnd = IntPtr.Zero;
  66. Process[] procs = Process.GetProcessesByName(WindowName);
  67. if (procs.Length != 0)
  68. {
  69. hwnd = procs[0].MainWindowHandle;
  70. //MessageBox.Show("??" + procs[0].Id.ToString() + "??" + procs[0].MainWindowHandle.ToString());
  71. //Global.writeLog("GetWindowsHandle=>ProcessID:" + procs[0].Id + ",ManagedThreadId:" + Thread.CurrentThread.ManagedThreadId.ToString());
  72. //Global.writeLog("GetWindowsHandle=>CurrentProcessID:" + Process.GetCurrentProcess().Id);
  73. }
  74. //Process ps = procs[0];
  75. //Global.writeLog("Start");
  76. //for (int i = 0; i < ps.Threads.Count; i++)
  77. //{
  78. // var thread = ps.Threads[i];
  79. // string str = string.Empty;
  80. // str = str + "标识符(" + i.ToString() + "):"+ thread.Id.ToString() + System.Environment.NewLine;
  81. // //str = str + "标识符:" + thread. + System.Environment.NewLine;
  82. // str = str +"基本优先级:" + thread.BasePriority.ToString() + System.Environment.NewLine;
  83. // str = str +"当前优先级:" + thread.CurrentPriority.ToString() + System.Environment.NewLine;
  84. // str = str +"内存地址:" + thread.StartAddress.ToInt32() + System.Environment.NewLine;
  85. // str = str +"启动时间:" + thread.StartTime.ToString() + System.Environment.NewLine;
  86. // str = str +"使用时间:" + thread.UserProcessorTime.ToString() + System.Environment.NewLine;
  87. // str = str + "当前状态:";
  88. // switch (thread.ThreadState)
  89. // {
  90. // case System.Diagnostics.ThreadState.Initialized:
  91. // str = str +"线程已经初始化但尚未启动";
  92. // break;
  93. // case System.Diagnostics.ThreadState.Ready:
  94. // str = str +"线程准备在下一个可用的处理器上运行";
  95. // break;
  96. // case System.Diagnostics.ThreadState.Running:
  97. // str = str +"当前正在使用处理器";
  98. // break;
  99. // case System.Diagnostics.ThreadState.Standby:
  100. // str = str +"线程将要使用处理器";
  101. // break;
  102. // case System.Diagnostics.ThreadState.Terminated:
  103. // str = str +"线程已完成执行并退出";
  104. // break;
  105. // case System.Diagnostics.ThreadState.Transition:
  106. // str = str +"线程在可以执行钱等待处理器之外的资源";
  107. // break;
  108. // case System.Diagnostics.ThreadState.Unknown:
  109. // str = str +"状态未知";
  110. // break;
  111. // case System.Diagnostics.ThreadState.Wait:
  112. // str = str +"正在等待外围操作完成或者资源释放";
  113. // break;
  114. // default:
  115. // break;
  116. // }
  117. // if (thread.ThreadState == System.Diagnostics.ThreadState.Wait)
  118. // {
  119. // str = str + System.Environment.NewLine +"等待原因:";
  120. // switch (thread.WaitReason)
  121. // {
  122. // case ThreadWaitReason.EventPairHigh:
  123. // str = str +"线程正在等待事件对高";
  124. // break;
  125. // case ThreadWaitReason.EventPairLow:
  126. // str = str +"线程正在等待事件对低";
  127. // break;
  128. // case ThreadWaitReason.ExecutionDelay:
  129. // str = str +"线程执行延迟";
  130. // break;
  131. // case ThreadWaitReason.Executive:
  132. // str = str +"线程正在等待计划程序";
  133. // break;
  134. // case ThreadWaitReason.FreePage:
  135. // str = str +"线程正在等待可用的虚拟内存页";
  136. // break;
  137. // case ThreadWaitReason.LpcReceive:
  138. // str = str +"线程正在等待本地过程调用到达";
  139. // break;
  140. // case ThreadWaitReason.LpcReply:
  141. // str = str +"线程正在等待对本地过程调用的回复到达";
  142. // break;
  143. // case ThreadWaitReason.PageIn:
  144. // str = str +"线程正在等待虚拟内存页到达内存";
  145. // break;
  146. // case ThreadWaitReason.PageOut:
  147. // str = str +"线程正在等待虚拟内存页写入磁盘";
  148. // break;
  149. // case ThreadWaitReason.Suspended:
  150. // str = str +"线程执行暂停";
  151. // break;
  152. // case ThreadWaitReason.SystemAllocation:
  153. // str = str +"线程正在等待系统分配";
  154. // break;
  155. // case ThreadWaitReason.Unknown:
  156. // str = str +"线程因位置原因而等待";
  157. // break;
  158. // case ThreadWaitReason.UserRequest:
  159. // str = str +"线程正在等待用户请求";
  160. // break;
  161. // case ThreadWaitReason.VirtualMemory:
  162. // str = str +"线程正在等待系统分配虚拟内存";
  163. // break;
  164. // default:
  165. // break;
  166. // }
  167. // }
  168. // Global.writeLog("ThreadsInfo:" + procs[0].Id.ToString() + "_" + procs[0].ProcessName, "Count:" + ps.Threads.Count.ToString(), str);
  169. //}
  170. //Global.writeLog("End");
  171. //foreach (Process process in procs)
  172. //{
  173. // if (process.ProcessName.Contains(WindowName))
  174. // {
  175. // MessageBox.Show($"Process_Name = ({process.ProcessName}), Id = {process.Id}");
  176. // }
  177. //}
  178. //return GetCurrentWindowHandle((uint)procs[0].Id);
  179. //Process[] processes = Process.GetProcesses();
  180. //foreach (Process process in processes)
  181. //{
  182. // if (process.ProcessName.Contains(WindowName))
  183. // {
  184. // MessageBox.Show($"ProcessName = ({process.ProcessName}), Id = {process.Id}");
  185. // }
  186. //}
  187. return hwnd;
  188. }
  189. private static Hashtable processWnd = null;
  190. public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam);
  191. //static User32API()
  192. //{
  193. // if (processWnd == null)
  194. // {
  195. // processWnd = new Hashtable();
  196. // }
  197. //}
  198. [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
  199. public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam);
  200. [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
  201. public static extern IntPtr GetParent(IntPtr hWnd);
  202. [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
  203. public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId);
  204. [DllImport("user32.dll", EntryPoint = "IsWindow")]
  205. public static extern bool IsWindow(IntPtr hWnd);
  206. [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
  207. public static extern void SetLastError(uint dwErrCode);
  208. public static IntPtr GetCurrentWindowHandle()
  209. {
  210. IntPtr ptrWnd = IntPtr.Zero;
  211. uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
  212. object objWnd = processWnd[uiPid];
  213. if (objWnd != null)
  214. {
  215. ptrWnd = (IntPtr)objWnd;
  216. if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
  217. {
  218. return ptrWnd;
  219. }
  220. else
  221. {
  222. ptrWnd = IntPtr.Zero;
  223. }
  224. }
  225. bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
  226. // 枚举窗口返回 false 并且没有错误号时表明获取成功
  227. if (!bResult && Marshal.GetLastWin32Error() == 0)
  228. {
  229. objWnd = processWnd[uiPid];
  230. if (objWnd != null)
  231. {
  232. ptrWnd = (IntPtr)objWnd;
  233. }
  234. }
  235. return ptrWnd;
  236. }
  237. private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
  238. {
  239. uint uiPid = 0;
  240. if (GetParent(hwnd) == IntPtr.Zero)
  241. {
  242. GetWindowThreadProcessId(hwnd, ref uiPid);
  243. if (uiPid == lParam) // 找到进程对应的主窗口句柄
  244. {
  245. processWnd[uiPid] = hwnd; // 把句柄缓存起来
  246. SetLastError(0); // 设置无错误
  247. return false; // 返回 false 以终止枚举窗口
  248. }
  249. }
  250. return true;
  251. }
  252. public static IntPtr GetCurrentWindowHandle(uint proid)
  253. {
  254. IntPtr ptrWnd = IntPtr.Zero;
  255. uint uiPid = proid;
  256. object objWnd = processWnd[uiPid];
  257. if (objWnd != null)
  258. {
  259. ptrWnd = (IntPtr)objWnd;
  260. if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
  261. {
  262. return ptrWnd;
  263. }
  264. else
  265. {
  266. ptrWnd = IntPtr.Zero;
  267. }
  268. }
  269. bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
  270. // 枚举窗口返回 false 并且没有错误号时表明获取成功
  271. if (!bResult && Marshal.GetLastWin32Error() == 0)
  272. {
  273. objWnd = processWnd[uiPid];
  274. if (objWnd != null)
  275. {
  276. ptrWnd = (IntPtr)objWnd;
  277. }
  278. }
  279. return ptrWnd;
  280. }
  281. }
  282. }