MyClassLibrary.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. namespace CassiniDev
  7. {
  8. public class MyClassLibrary
  9. {
  10. public MyClassLibrary()
  11. {
  12. }
  13. //返回值:
  14. //若函数调用成功,则返回值大于31。若函数调用失败,则返回值为下列之一:
  15. //① 0:系统内存或资源已耗尽。
  16. //② ERROR_BAD_FORMAT:EXE文件无效(非Win32.EXE或.EXE影像错误)。
  17. //③ ERROR_FILE_NOT_FOUND:指定的文件未找到。
  18. //④ ERROR_PATH_NOT_FOUND:指定的路径未找到。
  19. //大于 31 {调用成功}
  20. //等于 0 {内存不足}
  21. //ERROR_FILE_NOT_FOUND = 2; {文件名错误}
  22. //ERROR_PATH_NOT_FOUND = 3; {路径名错误}
  23. //ERROR_BAD_FORMAT = 11; {EXE 文件无效}
  24. ///uCmdShow 参数可选值:
  25. //SW_HIDE = 0; {隐藏, 并且任务栏也没有最小化图标}
  26. //SW_SHOWNORMAL = 1; {用最近的大小和位置显示, 激活}
  27. //SW_NORMAL = 1; {同 SW_SHOWNORMAL}
  28. //SW_SHOWMINIMIZED = 2; {最小化, 激活}
  29. //SW_SHOWMAXIMIZED = 3; {最大化, 激活}
  30. //SW_MAXIMIZE = 3; {同 SW_SHOWMAXIMIZED}
  31. //SW_SHOWNOACTIVATE = 4; {用最近的大小和位置显示, 不激活}
  32. //SW_SHOW = 5; {同 SW_SHOWNORMAL}
  33. //SW_MINIMIZE = 6; {最小化, 不激活}
  34. //SW_SHOWMINNOACTIVE = 7; {同 SW_MINIMIZE}
  35. //SW_SHOWNA = 8; {同 SW_SHOWNOACTIVATE}
  36. //SW_RESTORE = 9; {同 SW_SHOWNORMAL}
  37. //SW_SHOWDEFAULT = 10; {同 SW_SHOWNORMAL}
  38. //SW_MAX = 10; {同 SW_SHOWNORMAL}
  39. [DllImport("kernel32.dll")]
  40. public static extern int WinExec(string exeName, int uCmdShow);
  41. [DllImport("user32.dll", EntryPoint = "SendMessage")]
  42. public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  43. //调用Win32 API
  44. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
  45. public static extern bool MoveWindow(System.IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  46. //调用Win32 API
  47. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "MoveWindow")]
  48. public static extern bool MoveWindow(int hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
  49. [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "SetWindowPos")]
  50. public static extern bool SetWindowPos(System.IntPtr hWnd, System.IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
  51. //查找窗体
  52. [DllImport("User32.dll", EntryPoint = "FindWindow")]
  53. public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  54. [DllImport("User32.dll", EntryPoint = "FindWindowEx")]
  55. public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
  56. public delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
  57. [DllImport("user32.dll")]
  58. public static extern int EnumWindows(EnumWindowsProc hWnd, IntPtr lParam);
  59. [DllImport("user32.dll", CharSet = CharSet.Auto)]
  60. public static extern int GetWindowText(IntPtr hWnd, StringBuilder lpText, int nCount);
  61. [DllImport("user32.dll")]
  62. public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  63. public static EnumWindowsInfo GetEnumWindows(string title)
  64. {
  65. EnumWindowsInfo EwInfo = new EnumWindowsInfo();
  66. EwInfo.WindowCount = 0;
  67. EwInfo.FindWindowTitle = title;
  68. EwInfo.WindowsHWnds = "";
  69. int size = Marshal.SizeOf(EwInfo);
  70. IntPtr buffer = Marshal.AllocHGlobal(size);
  71. try
  72. {
  73. Marshal.StructureToPtr(EwInfo, buffer, true);
  74. EnumWindows(PrintWindow, buffer);
  75. EwInfo = (EnumWindowsInfo)Marshal.PtrToStructure(buffer, typeof(EnumWindowsInfo));
  76. EwInfo.WindowsHWnds = EwInfo.WindowsHWnds.Trim(',');
  77. }
  78. finally
  79. {
  80. Marshal.FreeHGlobal(buffer);
  81. }
  82. return EwInfo;
  83. }
  84. /// <summary>
  85. /// 获取指定窗体句柄的标题
  86. /// </summary>
  87. /// <param name="hWnd"></param>
  88. /// <returns></returns>
  89. public static string GetWindowTitle(IntPtr hWnd)
  90. {
  91. //分配空间
  92. var sb = new StringBuilder(50);
  93. GetWindowText(hWnd, sb, 50);
  94. string title = sb.ToString();
  95. sb.Clear();
  96. sb = null;
  97. return title;
  98. }
  99. //回调函数
  100. static bool PrintWindow(IntPtr hWnd, IntPtr lParam)
  101. {
  102. EnumWindowsInfo EwInfo = (EnumWindowsInfo)Marshal.PtrToStructure(lParam, typeof(EnumWindowsInfo));
  103. //分配空间
  104. var sb = new StringBuilder(50);
  105. GetWindowText(hWnd, sb, 50);
  106. string title = sb.ToString();
  107. //注意某些窗口没有标题
  108. sb.Clear();
  109. sb = null;
  110. if (!string.IsNullOrEmpty(title))
  111. {
  112. if (title.ToLower().Contains(EwInfo.FindWindowTitle.ToLower()))
  113. {
  114. EwInfo.WindowCount++;
  115. EwInfo.WindowsHWnds += ((int)hWnd) + ",";
  116. Marshal.StructureToPtr(EwInfo, lParam, true);
  117. }
  118. // Console.WriteLine(sb.ToString());
  119. }
  120. //回调函数有返回值
  121. return true;
  122. }
  123. /// <summary>
  124. /// 执行CMD命令
  125. /// </summary>
  126. /// <param name="command">CMD命令字符串</param>
  127. /// <returns></returns>
  128. public static string ExecuteCmdReturn(string[] commands, string cmdPath)
  129. {
  130. System.Diagnostics.Process p = new System.Diagnostics.Process();
  131. p.StartInfo.FileName = "cmd.exe";
  132. p.StartInfo.UseShellExecute = false; //是否使用操作系统shell启动
  133. p.StartInfo.RedirectStandardInput = true;//接受来自调用程序的输入信息
  134. p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
  135. p.StartInfo.RedirectStandardError = true;//重定向标准错误输出
  136. p.StartInfo.CreateNoWindow = true;//不显示程序窗口
  137. // p.StartInfo.StandardOutputEncoding = Encoding.UTF8;
  138. p.Start();//启动程序
  139. //向cmd窗口发送输入信息
  140. p.StandardInput.WriteLine(@"cd /d " + cmdPath);
  141. bool isEnd = false;
  142. for (int i = 0; i < commands.Length; i++)
  143. {
  144. if (i == commands.Length - 1)
  145. {
  146. p.StandardInput.WriteLine(commands[i] + " &exit");
  147. isEnd = true;
  148. }
  149. else
  150. {
  151. p.StandardInput.WriteLine(commands[i]);
  152. }
  153. }
  154. if (!isEnd)
  155. {
  156. System.Threading.Thread.Sleep(100);
  157. p.StandardInput.WriteLine("cls &exit");
  158. }
  159. p.StandardInput.AutoFlush = true;
  160. //p.StandardInput.WriteLine("exit");
  161. //向标准输入写入要执行的命令。这里使用&是批处理命令的符号,表示前面一个命令不管是否执行成功都执行后面(exit)命令,如果不执行exit命令,后面调用ReadToEnd()方法会假死
  162. //同类的符号还有&&和||前者表示必须前一个命令执行成功才会执行后面的命令,后者表示必须前一个命令执行失败才会执行后面的命令
  163. //获取cmd窗口的输出信息
  164. string output = p.StandardOutput.ReadToEnd();
  165. p.WaitForExit(5000);//等待程序执行完退出进程
  166. p.Close();
  167. return output;
  168. }
  169. public static void ShowHideFRPC(bool bl)
  170. {
  171. try
  172. {
  173. EnumWindowsInfo EwInfo = MyClassLibrary.GetEnumWindows("frpc");
  174. string[] windHwnds = EwInfo.WindowsHWnds.Split(',');
  175. foreach (string windHwnd in windHwnds)
  176. {
  177. if (!string.IsNullOrEmpty(windHwnd))
  178. {
  179. if (bl)
  180. {
  181. MyClassLibrary.ShowWindow((IntPtr)Convert.ToInt32(windHwnd), 10);
  182. }
  183. else
  184. {
  185. MyClassLibrary.ShowWindow((IntPtr)Convert.ToInt32(windHwnd), 0);
  186. }
  187. }
  188. }
  189. }
  190. catch { }
  191. }
  192. }
  193. public struct EnumWindowsInfo
  194. {
  195. /// <summary>
  196. /// 窗体数量
  197. /// </summary>
  198. public Int32 WindowCount;
  199. /// <summary>
  200. /// 要查找的窗体标题
  201. /// </summary>
  202. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
  203. public string FindWindowTitle;
  204. /// <summary>
  205. /// 已查找到的窗体句柄 用逗号“,”分隔
  206. /// </summary>
  207. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 11000)]
  208. public string WindowsHWnds;
  209. }
  210. }