- using System;
- using System.Runtime.InteropServices;
-
- /// <summary>
- /// Gère les processus
- /// </summary>
- public class ProcessManager
- {
- [DllImport("kernel32.dll", SetLastError=true)]
- static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags,
- uint th32ProcessID);
-
-
- [DllImport("kernel32.dll", SetLastError=true)]
- static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
-
-
- [DllImport("kernel32.dll", SetLastError=true)]
- static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
-
- [DllImport("kernel32.dll", SetLastError=true)]
- static extern bool CloseHandle(IntPtr hObject);
-
- /// <summary>
- /// structure d'information sur un processus
- /// </summary>
- [StructLayout(LayoutKind.Sequential)]
- internal 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=260)] public string szExeFile;
- };
-
- const int TH32CS_SNAPPROCESS = 2;
- const int SIZEOF_PROCESSENTRY32 = 564;
- const int SIZE_OFFSET = 0;
- const int PROCESS_OFFSET = 8;
- const int PARENT_OFFSET = 24;
-
-
-
-
- /// <summary>
- /// Renvoi le PID du processus parent
- /// </summary>
- /// <param name="Pid"></param>
- /// <returns></returns>
- public static uint GetParentProcess(uint Pid)
- {
- //structure d'information sur un processus
- PROCESSENTRY32 _pe = new PROCESSENTRY32();
- _pe.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
-
-
- //Handle pour accèder aux infos des processus
- IntPtr handleProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
-
- uint PidParent = 0;
-
- try
- {
- //Récupère la structure de processus sur le premier Handle
- bool rv = Process32First(handleProcess,ref _pe);
-
- if(!rv)
- throw new Exception("Ne peut pas énumérer les processus");
-
- //tant qu'il y a des processus à éumérer
- while(rv)
- {
- uint pid = _pe.th32ProcessID;
- uint parent = _pe.th32ParentProcessID;
-
- if( pid == Pid ) //si c'est le process en cours
- {
- //on va renvoyer le process parent
- PidParent = parent;
- break; //sorti du while, on arrête d'énumérer les processus
- }
-
- //Récupère la structure de processus sur le handle suivant
- rv = Process32Next(handleProcess, ref _pe);
- }
- }
- finally
- {
- CloseHandle(handleProcess);
- }
-
- return PidParent;
- }
- }
-
- /***********************************************************************************
- ***********************************************************************************
- ***********************************************************************************
- ***********************************************************************************/
-
- //Utilisation
- uint current_pid = (uint)Process.GetCurrentProcess().Id;
- uint parent_pid = ProcessManager.GetParentProcess(current_pid);
- Process parent_process = Process.GetProcessById((int)parent_pid);
- string strParentFilePath = parent_process.MainModule.ModuleName;
using System;
using System.Runtime.InteropServices;
/// <summary>
/// Gère les processus
/// </summary>
public class ProcessManager
{
[DllImport("kernel32.dll", SetLastError=true)]
static extern IntPtr CreateToolhelp32Snapshot(uint dwFlags,
uint th32ProcessID);
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
[DllImport("kernel32.dll", SetLastError=true)]
static extern bool CloseHandle(IntPtr hObject);
/// <summary>
/// structure d'information sur un processus
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal 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=260)] public string szExeFile;
};
const int TH32CS_SNAPPROCESS = 2;
const int SIZEOF_PROCESSENTRY32 = 564;
const int SIZE_OFFSET = 0;
const int PROCESS_OFFSET = 8;
const int PARENT_OFFSET = 24;
/// <summary>
/// Renvoi le PID du processus parent
/// </summary>
/// <param name="Pid"></param>
/// <returns></returns>
public static uint GetParentProcess(uint Pid)
{
//structure d'information sur un processus
PROCESSENTRY32 _pe = new PROCESSENTRY32();
_pe.dwSize = (uint)Marshal.SizeOf(typeof(PROCESSENTRY32));
//Handle pour accèder aux infos des processus
IntPtr handleProcess = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
uint PidParent = 0;
try
{
//Récupère la structure de processus sur le premier Handle
bool rv = Process32First(handleProcess,ref _pe);
if(!rv)
throw new Exception("Ne peut pas énumérer les processus");
//tant qu'il y a des processus à éumérer
while(rv)
{
uint pid = _pe.th32ProcessID;
uint parent = _pe.th32ParentProcessID;
if( pid == Pid ) //si c'est le process en cours
{
//on va renvoyer le process parent
PidParent = parent;
break; //sorti du while, on arrête d'énumérer les processus
}
//Récupère la structure de processus sur le handle suivant
rv = Process32Next(handleProcess, ref _pe);
}
}
finally
{
CloseHandle(handleProcess);
}
return PidParent;
}
}
/***********************************************************************************
***********************************************************************************
***********************************************************************************
***********************************************************************************/
//Utilisation
uint current_pid = (uint)Process.GetCurrentProcess().Id;
uint parent_pid = ProcessManager.GetParentProcess(current_pid);
Process parent_process = Process.GetProcessById((int)parent_pid);
string strParentFilePath = parent_process.MainModule.ModuleName;