This repository has been archived by the owner on Feb 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.cs
116 lines (101 loc) · 3.09 KB
/
Helper.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
using System;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System.ServiceProcess;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace net.vieapps.Services.Utility.Memcached
{
internal static class Helper
{
static EventLog EventLog = null;
internal static void InitializeLog()
{
if (Helper.EventLog == null)
{
string name = "Application";
string source = "VIEAppsNGXMemcached";
if (!EventLog.SourceExists(source))
EventLog.CreateEventSource(source, name);
Helper.EventLog = new EventLog(source)
{
Source = source,
Log = name
};
}
}
internal static void DisposeLog()
{
Helper.EventLog.Close();
Helper.EventLog.Dispose();
}
internal static void WriteLog(string log, Exception ex = null)
{
string msg = log + (ex != null ? "\r\n\r\n" + "Message: " + ex.Message + " [" + ex.GetType().ToString() + "]\r\n\r\n" + "Details: " + ex.StackTrace : "");
if (Program.AsService)
Helper.EventLog.WriteEntry(msg, ex != null ? EventLogEntryType.Error : EventLogEntryType.Information);
else
Program.Form.UpdateLogs(msg);
}
[DllImport("kernel32.dll")]
private static extern IntPtr OpenProcess(ProcessAccessFlags dwDesiredAccess, bool bInheritHandle, int dwProcessId);
[DllImport("kernel32.dll")]
private static extern bool QueryFullProcessImageName(IntPtr hprocess, int dwFlags, StringBuilder lpExeName, out int size);
[DllImport("kernel32.dll", SetLastError = true)]
private static extern bool CloseHandle(IntPtr hHandle);
[Flags]
private enum ProcessAccessFlags : uint
{
All = 0x1f0fff,
CreateThread = 2,
DupHandle = 0x40,
QueryInformation = 0x400,
ReadControl = 0x20000,
SetInformation = 0x200,
Synchronize = 0x100000,
Terminate = 1,
VMOperation = 8,
VMRead = 0x10,
VMWrite = 0x20
}
internal static List<Tuple<int, string>> GetProcesses(string processExeFilename)
{
if (string.IsNullOrWhiteSpace(processExeFilename))
return null;
var processes = new List<Tuple<int, string>>();
foreach (var process in Process.GetProcesses())
{
var id = process.Id;
var handler = Helper.OpenProcess(ProcessAccessFlags.QueryInformation, false, id);
if (handler != IntPtr.Zero)
try
{
var pathBuilder = new StringBuilder(0x400);
var capacity = pathBuilder.Capacity;
if (Helper.QueryFullProcessImageName(handler, 0, pathBuilder, out capacity))
{
string processName = pathBuilder.ToString();
if (processName.ToLower().EndsWith(processExeFilename.ToLower()))
processes.Add(new Tuple<int, string>(id, processName));
}
}
catch { }
finally
{
Helper.CloseHandle(handler);
}
}
return processes;
}
internal static int GetProcessID(string processExeFilename, int excludedPID = 0)
{
if (string.IsNullOrWhiteSpace(processExeFilename))
return -1;
var process = Helper.GetProcesses(processExeFilename).FirstOrDefault(info => excludedPID > 0 ? !info.Item1.Equals(excludedPID) : true);
return process == null
? - 1
: process.Item1;
}
}
}