Следующая статья: Защита программ перекрытием кода.
Под Windows (Win32) это возможно с использованием вспомогательных информационных функций:
- Вызывается функция:
hSnapshot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
- Process32First() – получение информации о первом процессе в списке;
- Далее в цикле Process32Next() – получение информации о следующем процессе в списке.
unit KernlUtl;
interface
uses
TlHelp32, Windows, Classes, SysUtils;
procedure GetProcessList(List: TStrings);
procedure GetModuleList(List: TStrings);
function GetProcessHandle(ProcessID: DWORD): THandle;
procedure GetParentProcessInfo(var ID: DWORD; var Path: String);
const
PROCESS_TERMINATE=$0001;
PROCESS_CREATE_THREAD=$0002;
PROCESS_VM_OPERATION=$0008;
PROCESS_VM_READ=$0010;
PROCESS_VM_WRITE=$0020;
PROCESS_DUP_HANDLE=$0040;
PROCESS_CREATE_PROCESS=$0080;
PROCESS_SET_QUOTA=$0100;
PROCESS_SET_INFORMATION=$0200;
PROCESS_QUERY_INFORMATION=$0400;
PROCESS_ALL_ACCESS=STANDARD_RIGHTS_REQUIRED or SYNCHRONIZE or $0FFF;
implementation
procedure GetProcessList(List: TStrings);
var
I: Integer;
hSnapshoot: THandle;
pe32: TProcessEntry32;
begin
List.Clear;
hSnapshoot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshoot=-1) then Exit;
pe32.dwSize:=SizeOf(TProcessEntry32);
if (Process32First(hSnapshoot, pe32)) then repeat
I:=List.Add(Format('%x, %x: %s', [pe32.th32ProcessID,
pe32.th32ParentProcessID, pe32.szExeFile]));
List.Objects[I]:=Pointer(pe32.th32ProcessID);
until not Process32Next(hSnapshoot, pe32);
CloseHandle (hSnapshoot);
end;
procedure GetModuleList(List: TStrings);
var
I: Integer;
hSnapshoot: THandle;
me32: TModuleEntry32;
begin
List.Clear;
hSnapshoot:=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, 0);
if (hSnapshoot=-1) then Exit;
me32.dwSize:=SizeOf(TModuleEntry32);
if (Module32First(hSnapshoot, me32)) then repeat
I:=List.Add(me32.szModule);
List.Objects[I]:=Pointer(me32.th32ModuleID);
until not Module32Next(hSnapshoot, me32);
CloseHandle (hSnapshoot);
end;
procedure GetParentProcessInfo(var ID: DWORD; var Path: String);
var
ProcessID: DWORD;
hSnapshoot: THandle;
pe32: TProcessEntry32;
begin
ProcessID:=GetCurrentProcessId;
ID:=0;
Path :='';
hSnapshoot:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (hSnapshoot=-1) then Exit;
pe32.dwSize:=SizeOf(TProcessEntry32);
if (Process32First(hSnapshoot, pe32)) then
repeat
if pe32.th32ProcessID=ProcessID then begin
ID:=pe32.th32ParentProcessID;
Break;
end;
until not Process32Next(hSnapshoot, pe32);
if ID<>-1 then
if (Process32First(hSnapshoot, pe32)) then repeat
if pe32.th32ProcessID=ID then begin
Path:=pe32.szExeFile;
Break;
end;
until not Process32Next(hSnapshoot, pe32);
CloseHandle (hSnapshoot);
end;
function GetProcessHandle(ProcessID: DWORD): THandle;
begin
Result:=OpenProcess(PROCESS_ALL_ACCESS, True, ProcessID);
end;
end.