获取进程列表的单元

参考一些网上资料, 然后自己改了改......主要是为自己写的一个监视活动进程工具用的, 有需要可以参考参考

unit ProcessInfos;

interface

uses
  Windows, TLHelp32, PsAPI, SysUtils;

type
  TProcessInfo = record
    PID: Cardinal;
    PathName: string;
    CMDName: string;
  end;
  PProcessInfo = ^TProcessInfo;
  TProcessInfos = array of TProcessInfo;

procedure GetProcessInfos(var APIList: TProcessInfos);
function FindProcess(APathName: string; var API: TProcessInfo): Boolean;
function GetProcessInfo(APID: Cardinal; API: PProcessInfo): Boolean;

implementation

type
  UNICODE_STRING = packed record
    Length: Word;
    MaximumLength: Word;
    Buffer: PWideChar;
  end;
  PUNICODE_STRING = UNICODE_STRING;

  PROCESS_PARAMETERS = packed record
    AllocationSize: ULONG;
    ActualSize: ULONG;
    Flags: ULONG;
    Unknown1: ULONG;
    Unknown2: UNICODE_STRING;
    InputHandle: THandle;
    OutputHandle: THandle;
    ErrorHandle: THandle;
    CurrentDirectory: UNICODE_STRING;
    CurrentDirectoryHandle: THandle;
    SearchPaths: UNICODE_STRING;
    ApplicationName: UNICODE_STRING;
    CommandLine: UNICODE_STRING;
    EnvironmentBlock: Pointer;
    Unknown: array[0..9 - 1] of ULONG;
    Unknown3: UNICODE_STRING;
    Unknown4: UNICODE_STRING;
    Unknown5: UNICODE_STRING;
    Unknown6: UNICODE_STRING;
  end;
  PPROCESS_PARAMETERS = ^PROCESS_PARAMETERS;

  PEB = packed record
    AllocationSize: ULONG;
    Unknown1: ULONG;
    ProcessHinstance: Longword;
    ListDlls: Pointer;
    ProcessParameters: PPROCESS_PARAMETERS;
    Unknown2: ULONG;
    Heap: THandle;
  end;
  PPEB = ^PEB;

  _PROCESS_BASIC_INFORMATION = packed record
    Reserved1: Pointer;
    PebBaseAddress: PPEB;
    Reserved2: array[0..1] of Pointer;
    UniqueProcessId: PULONG;
    Reserved3: Pointer;
  end;
  PROCESS_BASIC_INFORMATION = _PROCESS_BASIC_INFORMATION;
  PPROCESS_BASIC_INFORMATION = ^PROCESS_BASIC_INFORMATION;
  PROCESSINFOCLASS = (
    ProcessBasicInformation = 0,
    ProcessWow64Information = 26
  );
  NTSTATUS = DWORD;

function NtQueryInformationProcess(
  ProcessHandle: THandle;
  ProcessInformationClass: PROCESSINFOCLASS;
  ProcessInformation: Pointer;
  ProcessInformationLength: ULONG;
  ReturnLength: PULONG
): NTSTATUS; stdcall; external ‘ntdll.dll‘ name ‘NtQueryInformationProcess‘;

var
  _SystemRoot: string;

procedure GetProcessInfos(var APIList: TProcessInfos);
var
  nContinueLoop: BOOL;
  nSnapShotHandle: THandle;
  nProcessEntry32: TProcessEntry32;
  nCount: Word;
begin
  nSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  try
    nProcessEntry32.dwSize := SizeOf(nProcessEntry32);
    nContinueLoop := Process32First(nSnapShotHandle, nProcessEntry32);

    SetLength(APIList, 65536);
    nCount := 0;

    while nContinueLoop do
    begin
      if GetProcessInfo(nProcessEntry32.th32ProcessID, @APIList[nCount]) then
        Inc(nCount);
      nContinueLoop := Process32Next(nSnapShotHandle, nProcessEntry32);
    end;

    SetLength(APIList, nCount);
  finally
    CloseHandle(nSnapShotHandle);
  end;
end;

function FindProcess(APathName: string; var API: TProcessInfo): Boolean;
var
  nContinueLoop: BOOL;
  nSnapShotHandle: THandle;
  nProcessEntry32: TProcessEntry32;
  nR: TProcessInfo;
begin
  Result := False;
  nSnapShotHandle := CreateToolhelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  try
    nProcessEntry32.dwSize := SizeOf(nProcessEntry32);
    nContinueLoop := Process32First(nSnapShotHandle, nProcessEntry32);

    while nContinueLoop do
    begin
      if GetProcessInfo(nProcessEntry32.th32ProcessID, @nR) then
      begin
        if SameText(nR.PathName, APathName) then
        begin
          API := nR;
          Result := True;
          Break;
        end;
      end;
      nContinueLoop := Process32Next(nSnapShotHandle, nProcessEntry32);
    end;
  finally
    CloseHandle(nSnapShotHandle);
  end;
end;

function GetProcessInfo(APID: Cardinal; API: PProcessInfo): Boolean;

  procedure _CurePath(var APath: string);
  begin
    APath := Trim(APath);
    if APath = ‘‘ then
      Exit;
    if Copy(APath, 1, 4) = ‘\??\‘ then
      {类似\??\c:\windows\system32\winlogon.exe的路径}
      APath := Copy(APath, 5, Length(APath))
    else if Copy(APath, 1, 12) = ‘\SystemRoot\‘ then
      {类似\SystemRoot\system32\smss.exe的路径}
      APath := IncludeTrailingBackslash(_SystemRoot) + ExtractFileName(APath);
  end;

var
  nPHandle: THandle;
  nStr: string;
  nStrC: WideString;
  nPBI: PROCESS_BASIC_INFORMATION;
  nR: Cardinal;
  nPEB: PEB;
  nPP: PROCESS_PARAMETERS;
  nCount: Word;
  nBuff: array [0..MAX_PATH] of Char;
begin
  Result := False;
  if API = nil then
    Exit;

  nPHandle := OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, False, APID);

  if nPHandle = 0 then
    Exit;

  try
    if GetModuleFileNameEx(nPHandle, 0, @nBuff[0], SizeOf(nBuff)) > 0 then
      API^.PathName := nBuff
    else
      API^.PathName := ‘‘;

    try
      if NtQueryInformationProcess(nPHandle, ProcessBasicInformation, @nPBI, SizeOf(nPBI), @nR) <> 0 then
        Abort;
      if not ReadProcessMemory(nPHandle, nPBI.PebBaseAddress, @nPEB, SizeOf(nPEB), nR) then
        Abort;
      if not ReadProcessMemory(nPHandle, nPEB.ProcessParameters, @nPP, SizeOf(nPP), nR) then
        Abort;
      if nPP.CommandLine.Length = 0 then
        Abort;
      SetLength(nStrC, nPP.CommandLine.Length div 2);
      if not ReadProcessMemory(nPHandle, nPP.CommandLine.Buffer, @nStrC[1],
        nPP.CommandLine.Length, nR) then
        Abort;
      API^.CMDName := nStrC;
    except
      API^.CMDName := ‘‘;
    end;
  finally
    CloseHandle(nPHandle);
  end;

  _CurePath(API^.PathName);
  _CurePath(API^.CMDName);
  API^.PID := APID;
  Result := True;
end;

var
  nBuff: array [0..MAX_PATH] of Char;
initialization
  GetSystemDirectory(nBuff, SizeOf(nBuff));
  _SystemRoot := nBuff;

end.

http://www.cnblogs.com/hs-kill/p/4702571.html

时间: 2024-11-03 21:38:31

获取进程列表的单元的相关文章

获取进程列表

获取进程列表: public static string GetProcessList() { string str=""; Process[] processes=Process.GetProcesses(); Process proces; for (int i=0;i<processes.Length;i++) { proces=processes[i]; str += Convert.ToString(proces.Id) + ":" + proces

获取进程列表及相关信息

闲着没事,看着任务管理器好玩,查资料先简单实现一下,代码中没有加入获取CPU占用率的代码,这个代码网上很多,只是不喜欢那种写法,这里就不写了.以后继续完善,对于System Process和System的信息还没法获得,那位兄弟知道可以提个醒. 代码如下 [delphi] view plaincopy unit Main; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Form

Android 获取进程列表

有时候我们需要获取进行的PID好,好执行kill命令 ActivityManager mActivityManager = (ActivityManager)this.getSystemService(ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> mRunningProcess = mActivityManager.getRunningAppProcesses(); for (ActivityManager.Run

C# TreeGridView 实现进程列表

效果如图 0x01 获取进程列表,使用Win32Api规避"拒绝访问"异常 public List<AppProcess> GetAppProcesses() { IntPtr handle = NativeMethods.CreateToolhelp32Snapshot(0x2, 0); List<ProcessEntry32> list = new List<ProcessEntry32>(); List<AppProcess> ap

枚举PEB获取进程模块列表

枚举进程模块的方法有很多种,常见的有枚举PEB和内存搜索法,今天,先来看看实现起来最简单的枚举PEB实现获取进程模块列表. 首先,惯例是各种繁琐的结构体定义.需要包含 ntifs.h 和 WinDef.h, 此处不再列出,各位看官根据情况自行添加. [cpp] view plain copy print? typedef PPEB (__stdcall *PFNPsGetProcessPeb)(PEPROCESS pEProcess); typedef ULONG   PPS_POST_PROC

Qt 扫描进程列表以及获取进程信息

使用方法: QMap<QString,qint64> app_pid; getAllAppPidList( app_pid ); #include <tlhelp32.h>// for CreateToolhelp32Snapshot #include <Psapi.h> // for GetModuleFileNameEx #define FORMAT_PATH(path) path.replace('\\','/').toLower() QString GetPat

Atitit,通过pid获取进程文件路径&#160;java&#160;php&#160;&#160;c#.net版本大总结

Atitit,通过pid获取进程文件路径 java php  c#.net版本大总结 1. 通过PID获取进程路径的几种方法2 1.1. GetModuleFileNameEx 想获得进程可执行文件的路径最常用的方法是通过GetModuleFileNameEx函数获得可执行文件的模块路径这个函数从Windows NT 4.0开始到现在的Vista系统都能使用,向后兼容性比较好.2 1.2. 第二种方法是GetProcessImageFileName函数,这个函数在Windows XP及其以后的系

Android之——获取进程总数、内存、任务列表

转载请注明出处:http://blog.csdn.net/l1028386804/article/details/47263863 如今,基于Android平台的应用越来越多,不乏这样一些应用程序,这些应用可以多手机中的进程,内存和任务列表进行管理.Android系统中没有自带的应用程序对这些数据进行管理.大家不禁会说,Android的应用设置里面有这些功能啊!是的,虽然应用设置里面有,但是我们如果想看一下系统的进程,还要跑到系统设置里面去看吗?这样是不是很麻烦?是不是很挫呢?那获取Androi

android 查看手机运行的进程列表

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation