-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
93 lines (81 loc) · 3.01 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using NLog;
using System;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace DeploymentToolkit.Debugger
{
public static class Program
{
private static string _namespace;
internal static string Namespace
{
get
{
if (string.IsNullOrEmpty(_namespace))
_namespace = typeof(Program).Namespace;
return _namespace;
}
}
private static Version _version;
internal static Version Version
{
get
{
if (_version == null)
_version = Assembly.GetExecutingAssembly().GetName().Version;
return _version;
}
}
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
private static Logger _logger = LogManager.GetCurrentClassLogger();
public static void Main(string[] args)
{
try
{
Logging.LogManager.Initialize("Debugger");
}
catch(Exception ex)
{
Console.WriteLine("Failed to initialized logging");
Console.WriteLine(ex);
return;
}
_logger.Info($"{Namespace} v{Version} intitialized");
if (args.Length == 0)
{
_logger.Warn("No comamnd line parameters specified. Aborting...");
return;
}
// This shouldn't even be neccessary but why not
var processName = args[0];
if (processName.ToLower().EndsWith(".exe"))
processName = processName.Substring(0, processName.Length - 4);
_logger.Info($"Target process name: {processName}");
var processes = Process.GetProcessesByName(processName);
_logger.Info($"Found {processes.Length} processes with name {processName}");
foreach(var process in processes)
{
try
{
_logger.Info($"Trying to close [{process.Id}]{process.ProcessName} gracefully");
// Send a WM_CLOSE and wait for a gracefull exit
PostMessage(process.Handle, 0x0010, IntPtr.Zero, IntPtr.Zero);
if (!process.WaitForExit(1000))
{
_logger.Info($"Process did not close within 1 second. Killing {process.Id}");
process.Kill();
}
}
catch(Exception ex)
{
_logger.Error(ex, $"Error while trying to process [{process.Id}]{process.ProcessName}");
}
}
// Show information to the user that the execution has been blocked
_logger.Info("Program ended successfully");
}
}
}