Advertisement
selebry

adsda

Mar 6th, 2023
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.89 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. //наши директивы
  11. using System.Diagnostics;
  12. using System.Runtime.InteropServices;
  13. using System.IO;
  14. using System.Reflection;
  15. using System.Management;
  16. using System.Runtime.Serialization;
  17. using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
  18.  
  19. namespace ProcessManipulation
  20. {
  21. public partial class MainWindow : Form
  22. {
  23. public MainWindow()
  24. {
  25. InitializeComponent();
  26. LoadAvailableAssemblies();
  27. }
  28.  
  29. const uint WM_SETTEXT = 0x0C;
  30.  
  31. //импортируем библиотеку функции SendMessage
  32. [DllImport("user32.dll")]
  33. public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
  34.  
  35. List<Process> Processes = new List<Process>();
  36. int Counter = 0;
  37.  
  38. void LoadAvailableAssemblies()
  39. {
  40. string except = new FileInfo(Application.ExecutablePath).Name;
  41. except = except.Substring(0, except.IndexOf("."));
  42. string[] files = Directory.GetFiles(Application.StartupPath, "*.exe");
  43. foreach (var file in files)
  44. {
  45. //получаем имя файла
  46. string fileName = new FileInfo(file).Name;
  47. if (fileName.IndexOf(except) == -1)
  48. lb_AvailableAssemblies.Items.Add(fileName);
  49. }
  50. }
  51.  
  52. void RunProcess(string AssamblyName)
  53. {
  54. Process proc = Process.Start(AssamblyName);
  55. Processes.Add(proc);
  56.  
  57. if (Process.GetCurrentProcess().Id == GetParentProcessId(proc.Id))
  58. {
  59. MessageBox.Show(proc.ProcessName + " - child process");
  60. proc.EnableRaisingEvents = true;
  61. proc.Exited += proc_Exited;
  62.  
  63. SetChildWindowText(proc.MainWindowHandle, "child process #" + (++Counter));
  64.  
  65. if (!lb_StartAssemblies.Items.Contains(proc.ProcessName))
  66. lb_StartAssemblies.Items.Add(proc.ProcessName);
  67. lb_AvailableAssemblies.Items.Remove(lb_AvailableAssemblies.SelectedItem);
  68. }
  69. }
  70.  
  71. void SetChildWindowText(IntPtr Handle, string text)
  72. {
  73. SendMessage(Handle, WM_SETTEXT, (IntPtr)0, text);
  74. }
  75.  
  76. int GetParentProcessId(int Id)
  77. {
  78. int parentId = 0;
  79. using (ManagementObject obj = new ManagementObject("win32_process.handle=" + Id.ToString()))
  80. {
  81. obj.Get();
  82. parentId = Convert.ToInt32(obj["ParentProcessId"]);
  83. }
  84. return parentId;
  85. }
  86.  
  87. void proc_Exited(object sender, EventArgs e)
  88. {
  89. Process Proc = sender as Process;
  90. if (InvokeRequired)
  91. this.Invoke(new MethodInvoker(delegate { lb_AvailableAssemblies.Items.Add(Proc); }));
  92. lb_StartAssemblies.Items.Remove(Proc);
  93. Counter--;
  94. int index = 0;
  95. foreach (var p in Processes)
  96. SetChildWindowText(p.MainWindowHandle, "child process #" + ++index);
  97.  
  98. }
  99.  
  100. delegate void ProcessDelegate(Process proc);
  101.  
  102. void ExecuteOnProcessesByName(string ProcessName, ProcessDelegate func)
  103. {
  104. Process[] processes = Process.GetProcessesByName(ProcessName);
  105. foreach (var process in processes)
  106. if (Process.GetCurrentProcess().Id == GetParentProcessId(process.Id))
  107. func(process);
  108. }
  109.  
  110. void Kill(Process proc)
  111. {
  112. proc.Kill();
  113. }
  114.  
  115. private void bt_Start_Click(object sender, EventArgs e)
  116. {
  117. RunProcess(lb_AvailableAssemblies.SelectedItem.ToString());
  118. }
  119.  
  120. private void bt_Stop_Click(object sender, EventArgs e)
  121. {
  122. ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), Kill);
  123. lb_StartAssemblies.Items.Remove(lb_StartAssemblies.SelectedItem);
  124. }
  125.  
  126. void CloseMainWindow(Process proc)
  127. {
  128. proc.CloseMainWindow();
  129. }
  130.  
  131. private void bt_CloseWindows_Click(object sender, EventArgs e)
  132. {
  133. ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), CloseMainWindow);
  134. lb_StartAssemblies.Items.Remove
  135. (lb_StartAssemblies.SelectedItem);
  136. }
  137.  
  138. void Refresh(Process proc)
  139. {
  140. proc.Refresh();
  141. }
  142.  
  143. private void bt_refresh_Click(object sender, EventArgs e)
  144. {
  145. ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), Refresh);
  146. }
  147.  
  148. private void bt_runcalc_Click(object sender, EventArgs e)
  149. {
  150. RunProcess("calc.exe");
  151. }
  152.  
  153. private void bt_runword_Click(object sender, EventArgs e)
  154. {
  155. RunProcess("WINWORD.exe");
  156. }
  157.  
  158. private void lb_AvailableAssemblies_SelectedIndexChanged(object sender, EventArgs e)
  159. {
  160. if (lb_AvailableAssemblies.SelectedItems.Count == 0)
  161. bt_Start.Enabled = false;
  162. else
  163. bt_Start.Enabled = true;
  164. }
  165.  
  166. private void lb_StartAssemblies_SelectedIndexChanged(object sender, EventArgs e)
  167. {
  168. if (lb_StartAssemblies.SelectedItems.Count == 0)
  169. {
  170. bt_Stop.Enabled = false;
  171. bt_CloseWindows.Enabled = false;
  172. }
  173. else
  174. {
  175. bt_Stop.Enabled = true;
  176. bt_CloseWindows.Enabled = true;
  177. }
  178. }
  179. }
  180. }
  181.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement