MessageUtil.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. namespace GeekDesk.Util
  9. {
  10. public class MessageUtil
  11. {
  12. public const int WM_COPYDATA = 0x004A;
  13. public static bool SendMsgByPName(string processName, string msg)
  14. {
  15. try
  16. {
  17. Process[] processArr = Process.GetProcessesByName(processName);
  18. if (processArr != null && processArr.Length > 0)
  19. {
  20. foreach (Process process in processArr)
  21. {
  22. IntPtr windowHandle = process.MainWindowHandle;
  23. // 发送消息
  24. CopyDataStruct cds = new CopyDataStruct(IntPtr.Zero, msg);
  25. SendMessage(
  26. windowHandle,
  27. WM_COPYDATA,
  28. 0, ref cds);
  29. }
  30. return true;
  31. }
  32. else
  33. {
  34. return false;
  35. }
  36. } catch (Exception e)
  37. {
  38. LogUtil.WriteErrorLog(e, processName + "P发送消息失败!");
  39. return false;
  40. }
  41. }
  42. public static bool SendMsgByWName(string windowName, string msg)
  43. {
  44. try
  45. {
  46. IntPtr hwnd = FindWindow(null, windowName);
  47. if (hwnd != IntPtr.Zero)
  48. {
  49. // 发送消息
  50. CopyDataStruct cds = new CopyDataStruct(IntPtr.Zero, msg);
  51. SendMessage(
  52. hwnd,
  53. WM_COPYDATA,
  54. 0, ref cds);
  55. } else
  56. {
  57. return false;
  58. }
  59. return true;
  60. }
  61. catch (Exception e)
  62. {
  63. LogUtil.WriteErrorLog(e, windowName + "W发送消息失败!");
  64. return false;
  65. }
  66. }
  67. public static bool CheckProcessIsRuning(string processName)
  68. {
  69. try
  70. {
  71. Process[] processArr = Process.GetProcessesByName(processName);
  72. return (processArr != null && processArr.Length > 0);
  73. }
  74. catch (Exception e)
  75. {
  76. LogUtil.WriteErrorLog(e, processName + "检查进程名失败!");
  77. return false;
  78. }
  79. }
  80. public static bool CheckWindowIsRuning(string windowName)
  81. {
  82. try
  83. {
  84. IntPtr hwnd = FindWindow(null, windowName);
  85. return (hwnd != IntPtr.Zero);
  86. } catch(Exception)
  87. {
  88. return false;
  89. }
  90. }
  91. [DllImport("User32.dll", EntryPoint = "FindWindow")]
  92. public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  93. [DllImport("User32.dll", EntryPoint = "SendMessage")]
  94. private static extern int SendMessage(
  95. IntPtr hWnd, //目标窗体句柄
  96. int Msg, //WM_COPYDATA
  97. int wParam, //自定义数值
  98. ref CopyDataStruct lParam //传递消息的结构体,
  99. );
  100. [StructLayout(LayoutKind.Sequential)]
  101. public struct CopyDataStruct
  102. {
  103. public CopyDataStruct(IntPtr custom, string msg)
  104. {
  105. this.custom = custom;
  106. this.msg = msg;
  107. this.msgLength = msg.Length + 1;
  108. }
  109. public IntPtr custom;//用户定义数据
  110. public int msgLength;//字符串长度
  111. [MarshalAs(UnmanagedType.LPStr)]
  112. public string msg;//字符串
  113. }
  114. [DllImport("user32")]
  115. public static extern bool ChangeWindowMessageFilter(uint msg, int flags);
  116. }
  117. }