-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessExtensions.cs
105 lines (96 loc) · 3.08 KB
/
ProcessExtensions.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
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.Runtime.InteropServices;
namespace System.Runtime.CompilerServices
{
[AttributeUsage(AttributeTargets.Method)]
public sealed class ExtensionAttribute : Attribute
{
public ExtensionAttribute() { }
}
}
namespace InstantBackgroundUploader
{
public static class ProcessExtensions
{
[DllImport("KERNEL32.dll")] //[DllImport("toolhelp.dll")]
public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("KERNEL32.DLL")] //[DllImport("toolhelp.dll")]
public static extern int CloseHandle(int handle);
[DllImport("KERNEL32.DLL")] //[DllImport("toolhelp.dll")
public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
[StructLayout(LayoutKind.Sequential)]
public struct ProcessEntry32
{
public uint dwSize;
public uint cntUsage;
public uint th32ProcessID;
public IntPtr th32DefaultHeapID;
public uint th32ModuleID;
public uint cntThreads;
public uint th32ParentProcessID;
public int pcPriClassBase;
public uint dwFlags;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string szExeFile;
};
public static Process Parent(this Process process)
{
int SnapShot = CreateToolhelp32Snapshot(0x00000002, 0); //2 = SNAPSHOT of all procs
try
{
ProcessEntry32 pe32 = new ProcessEntry32();
pe32.dwSize = 296;
int procid = process.Id;
while (Process32Next(SnapShot, ref pe32) != 0)
{
string xname = pe32.szExeFile.ToString();
if (procid == pe32.th32ProcessID)
{
return System.Diagnostics.Process.GetProcessById(Convert.ToInt32(pe32.th32ParentProcessID));
}
}
}
catch (Exception ex)
{
throw new Exception(System.Reflection.MethodBase.GetCurrentMethod() + " failed! [Type:" + ex.GetType().ToString() + ", Msg:" + ex.Message + "]");
}
finally
{
CloseHandle(SnapShot);
}
return null;
}
}
/*public static class ProcessExtensions
{
private static string FindIndexedProcessName(int pid)
{
var processName = Process.GetProcessById(pid).ProcessName;
var processesByName = Process.GetProcessesByName(processName);
string processIndexdName = null;
for (var index = 0; index < processesByName.Length; index++)
{
processIndexdName = index == 0 ? processName : processName + "#" + index;
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
if (((int)processId.NextValue()).Equals(pid))
{
return processIndexdName;
}
}
return processIndexdName;
}
private static Process FindPidFromIndexedProcessName(string indexedProcessName)
{
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
return Process.GetProcessById((int)parentId.NextValue());
}
public static Process Parent(this Process process)
{
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
}
}*/
}