Jump to content
Tuts 4 You

[Delphi]Some Snippets


0xFF

Recommended Posts

returns a process base (CSTR) priority by a process ID, takes care of the low-level part

function GetProcessPri(PID: DWORD): String;
var
hSnap: THandle;
lppe: TProcessEntry32A;
szPri: String;
begin
lppe.dwSize := sizeof(lppe);
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if Process32FirstA(hSnap, lppe) <> False then
With lppe Do begin
if PID = lppe.th32ProcessID then
case lppe.pcPriClassBase of
13:szPri := 'High';
8,9, 11:szPri := 'Normal';
6:szPri := 'Below Normal';
4:szPri := 'Low';
end;
while Process32NextA(hSnap, lppe) do
if PID = lppe.th32ProcessID then
case lppe.pcPriClassBase of
13:szPri := 'High';
8,9, 11:szPri := 'Normal';
6:szPri := 'Below Normal';
4:szPri := 'Low';
end;
end;
CloseHandle(hSnap);
Result := szPri;
end;

Returns process name by process ID

function GetPNameByPid(PID: DWORD): String;
var
hSnap: THandle;
lppe: TProcessEntry32A;
szPName: String;
begin
lppe.dwSize := sizeof(lppe);
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if Process32FirstA(hSnap, lppe) <> False then
With lppe Do begin
if th32ProcessID = PID then szPName := szExeFile;
while Process32NextA(hSnap, lppe) do
if th32ProcessID = PID then szPName := szExeFile;
end;
CloseHandle(hSnap);
Result := szPName;
end;

Gets a process ID by the ID of the process (useful to return parent process)

function TForm1.GetPidByPid(PID: DWORD): DWORD;
var
hSnap: THandle;
lppe: TProcessEntry32A;
dwPID: DWORD;
begin
dwPID := 0; //initializing the variable
lppe.dwSize := sizeof(lppe);
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if Process32FirstA(hSnap, lppe) <> False then
With lppe Do begin
if th32ProcessID = PID then dwPID := th32ProcessID;
while Process32NextA(hSnap, lppe) do
if th32ProcessID = PID then dwPID := th32ProcessID;
end;
Result := dwPID;
end;

Returns process ID by process name (CSTR)

function TForm1.GetPidByPName(Selected_Process: String): DWORD;
var
hSnap: THandle;
lppe: TProcessEntry32A;
dwPID: DWORD;
begin
dwPID := 0; //initializing the variable
lppe.dwSize := sizeof(lppe);
hSnap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);if Process32FirstA(hSnap, lppe) <> False then
begin
if lppe.szExeFile = Selected_Process then dwPID := lppe.th32ProcessID;
while Process32NextA(hSnap, lppe) do
if lppe.szExeFile = Selected_Process then dwPID := lppe.th32ProcessID;
end;
Result := dwPID;
end;
Link to comment

thanks rot1, always nice to see someone contribute delphi snippets...

Im still waiting to see your search and replacee snippets in delphi :)

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...