/// <summary>
/// Wrapper around the ToolHelp ProcessEntry information
/// </summary>
/// <remarks>
/// This class requires the toolhelp32.dll
/// </remarks>
public class ProcessEntry
{
private PROCESSENTRY32 m_pe;
/// <summary>
/// Get the short name of the current process
/// </summary>
/// <returns>The current process name</returns>
public override string ToString()
{
return System.IO.Path.GetFileName(m_pe.ExeFile);
}
/// <summary>
/// Base address for the process
/// </summary>
[CLSCompliant(false)]
public uint BaseAddress
{
get { return m_pe.BaseAddress; }
}
/// <summary>
/// Number of execution threads started by the process.
/// </summary>
public int ThreadCount
{
get { return (int)m_pe.cntThreads; }
}
/// <summary>
/// Identifier of the process. The contents of this member can be used by Win32 API elements.
/// </summary>
[CLSCompliant(false)]
public uint ProcessID
{
get { return (uint)m_pe.ProcessID; }
}
/// <summary>
/// Null-terminated string that contains the path and file name of the executable file for the process.
/// </summary>
public string ExeFile
{
get { return m_pe.ExeFile; }
}
/// <summary>
/// Kill the Process
/// </summary>
public void Kill()
{
IntPtr hProcess;
/// <summary>
/// Rerieves an array of all running processes
/// </summary>
/// <returns></returns>
public static ProcessEntry[] GetProcesses()
{
ArrayList procList = new ArrayList();
if ((int)handle > 0)
{
try
{
PROCESSENTRY32 peCurrent;
PROCESSENTRY32 pe32 = new PROCESSENTRY32();
//Get byte array to pass to the API calls
byte[] peBytes = pe32.ToByteArray();
//Get the first process
int retval = Util.Process32First(handle, peBytes);
while(retval == 1)
{
//Convert bytes to the class
peCurrent = new PROCESSENTRY32(peBytes);
//New instance
ProcessEntry proc = new ProcessEntry(peCurrent);
[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;
};
[DllImport("KERNEL32.DLL")]
public static extern int CreateToolhelp32Snapshot(uint flags, uint processid);
[DllImport("KERNEL32.DLL")]
public static extern int Process32First(int handle, ref ProcessEntry32 pe);
[DllImport("KERNEL32.DLL")]
public static extern int Process32Next(int handle, ref ProcessEntry32 pe);
/// <summary>
/// Wrapper around the ToolHelp ProcessEntry information
/// </summary>
/// <remarks>
/// This class requires the toolhelp32.dll
/// </remarks>
7楼的兄弟这个结构体没错
[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;
};