Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- //наши директивы
- using System.Diagnostics;
- using System.Runtime.InteropServices;
- using System.IO;
- using System.Reflection;
- using System.Management;
- using System.Runtime.Serialization;
- using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
- namespace ProcessManipulation
- {
- public partial class MainWindow : Form
- {
- public MainWindow()
- {
- InitializeComponent();
- LoadAvailableAssemblies();
- }
- const uint WM_SETTEXT = 0x0C;
- //импортируем библиотеку функции SendMessage
- [DllImport("user32.dll")]
- public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
- List<Process> Processes = new List<Process>();
- int Counter = 0;
- void LoadAvailableAssemblies()
- {
- string except = new FileInfo(Application.ExecutablePath).Name;
- except = except.Substring(0, except.IndexOf("."));
- string[] files = Directory.GetFiles(Application.StartupPath, "*.exe");
- foreach (var file in files)
- {
- //получаем имя файла
- string fileName = new FileInfo(file).Name;
- if (fileName.IndexOf(except) == -1)
- lb_AvailableAssemblies.Items.Add(fileName);
- }
- }
- void RunProcess(string AssamblyName)
- {
- Process proc = Process.Start(AssamblyName);
- Processes.Add(proc);
- if (Process.GetCurrentProcess().Id == GetParentProcessId(proc.Id))
- {
- MessageBox.Show(proc.ProcessName + " - child process");
- proc.EnableRaisingEvents = true;
- proc.Exited += proc_Exited;
- SetChildWindowText(proc.MainWindowHandle, "child process #" + (++Counter));
- if (!lb_StartAssemblies.Items.Contains(proc.ProcessName))
- lb_StartAssemblies.Items.Add(proc.ProcessName);
- lb_AvailableAssemblies.Items.Remove(lb_AvailableAssemblies.SelectedItem);
- }
- }
- void SetChildWindowText(IntPtr Handle, string text)
- {
- SendMessage(Handle, WM_SETTEXT, (IntPtr)0, text);
- }
- int GetParentProcessId(int Id)
- {
- int parentId = 0;
- using (ManagementObject obj = new ManagementObject("win32_process.handle=" + Id.ToString()))
- {
- obj.Get();
- parentId = Convert.ToInt32(obj["ParentProcessId"]);
- }
- return parentId;
- }
- void proc_Exited(object sender, EventArgs e)
- {
- Process Proc = sender as Process;
- if (InvokeRequired)
- this.Invoke(new MethodInvoker(delegate { lb_AvailableAssemblies.Items.Add(Proc); }));
- lb_StartAssemblies.Items.Remove(Proc);
- Counter--;
- int index = 0;
- foreach (var p in Processes)
- SetChildWindowText(p.MainWindowHandle, "child process #" + ++index);
- }
- delegate void ProcessDelegate(Process proc);
- void ExecuteOnProcessesByName(string ProcessName, ProcessDelegate func)
- {
- Process[] processes = Process.GetProcessesByName(ProcessName);
- foreach (var process in processes)
- if (Process.GetCurrentProcess().Id == GetParentProcessId(process.Id))
- func(process);
- }
- void Kill(Process proc)
- {
- proc.Kill();
- }
- private void bt_Start_Click(object sender, EventArgs e)
- {
- RunProcess(lb_AvailableAssemblies.SelectedItem.ToString());
- }
- private void bt_Stop_Click(object sender, EventArgs e)
- {
- ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), Kill);
- lb_StartAssemblies.Items.Remove(lb_StartAssemblies.SelectedItem);
- }
- void CloseMainWindow(Process proc)
- {
- proc.CloseMainWindow();
- }
- private void bt_CloseWindows_Click(object sender, EventArgs e)
- {
- ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), CloseMainWindow);
- lb_StartAssemblies.Items.Remove
- (lb_StartAssemblies.SelectedItem);
- }
- void Refresh(Process proc)
- {
- proc.Refresh();
- }
- private void bt_refresh_Click(object sender, EventArgs e)
- {
- ExecuteOnProcessesByName(lb_StartAssemblies.SelectedItem.ToString(), Refresh);
- }
- private void bt_runcalc_Click(object sender, EventArgs e)
- {
- RunProcess("calc.exe");
- }
- private void bt_runword_Click(object sender, EventArgs e)
- {
- RunProcess("WINWORD.exe");
- }
- private void lb_AvailableAssemblies_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (lb_AvailableAssemblies.SelectedItems.Count == 0)
- bt_Start.Enabled = false;
- else
- bt_Start.Enabled = true;
- }
- private void lb_StartAssemblies_SelectedIndexChanged(object sender, EventArgs e)
- {
- if (lb_StartAssemblies.SelectedItems.Count == 0)
- {
- bt_Stop.Enabled = false;
- bt_CloseWindows.Enabled = false;
- }
- else
- {
- bt_Stop.Enabled = true;
- bt_CloseWindows.Enabled = true;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement