#region Helper conversion functions // utility: get a uint from the byte array privatestaticuint GetUInt(byte[] aData, int Offset) { return BitConverter.ToUInt32(aData, Offset); }
// utility: set a uint int the byte array privatestaticvoid SetUInt(byte[] aData, int Offset, int Value) { byte[] buint = BitConverter.GetBytes(Value); Buffer.BlockCopy(buint, 0, aData, Offset, buint.Length); }
// utility: get a ushort from the byte array privatestaticushort GetUShort(byte[] aData, int Offset) { return BitConverter.ToUInt16(aData, Offset); }
// utility: set a ushort int the byte array privatestaticvoid SetUShort(byte[] aData, int Offset, int Value) { byte[] bushort = BitConverter.GetBytes((short)Value); Buffer.BlockCopy(bushort, 0, aData, Offset, bushort.Length); }
// utility: get a unicode string from the byte array privatestaticstring GetString(byte[] aData, int Offset, int Length) { String sReturn = Encoding.Unicode.GetString(aData, Offset, Length); return sReturn; }
// utility: set a unicode string in the byte array privatestaticvoid SetString(byte[] aData, int Offset, string Value) { byte[] arr = Encoding.ASCII.GetBytes(Value); Buffer.BlockCopy(arr, 0, aData, Offset, arr.Length); } #endregion
// create an initialized data array publicbyte[] ToByteArray() { byte[] aData; aData =newbyte[Size]; //set the Size member SetUInt(aData, SizeOffset, Size); return aData; }
publicstring Name { get { return szExeFile.Substring(0, szExeFile.IndexOf('\0')); } }
publiculong PID { get { return th32ProcessID; } }
publiculong BaseAddress { get { return th32MemoryBase; } }
Process class #region Process class ///<summary> /// Summary description for Process. ///</summary> publicclass Process { privatestring processName; private IntPtr handle; privateint threadCount; privateint baseAddress;
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 = Process32First(handle, peBytes);
while(retval ==1) { //Convert bytes to the class peCurrent =new PROCESSENTRY32(peBytes); //New instance Process proc =new Process(new IntPtr((int)peCurrent.PID), peCurrent.Name, (int)peCurrent.ThreadCount, (int)peCurrent.BaseAddress);
} else { thrownew Exception("Unable to create snapshot"); }
}
#endregion
Client
staticvoid Main(string[] args) { if (args.Length ==0) { Console.WriteLine("Please enter the process name."); return; } string processName = args[0].ToUpper() +".EXE";
Process[] processes = Process.GetProcesses(); foreach (Process proc in processes) { if (proc.ProcessName.ToUpper() == processName) { proc.Kill(); Console.WriteLine("{0} was killed.", processName); break; } } }
【推荐】FlashTable:表单开发界的极速跑车,让你的开发效率一路狂飙
【推荐】Flutter适配HarmonyOS 5知识地图,实战解析+高频避坑指南
【推荐】博客园的心动:当一群程序员决定开源共建一个真诚相亲平台
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步