//use Tlhelp32
function GetProcessIDEx(List:TStrings; ProcessName: string = ''): TProcessEntry32;
var
ret: Boolean;
processID: Cardinal;
_processName: string;
FSnapshotHandle: HWND;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ret := Process32First(FSnapshotHandle,FProcessEntry32);
while ret do
begin
_processName := ExtractFileName(FProcessEntry32.szExeFile);
if (ProcessName = '') then
begin
processID := FProcessEntry32.th32ProcessID;
List.Add(Format('%-20s%d',[_processName,processID]));
end
else if (AnsiCompareText(_processName,ProcessName)=0) then
begin
processID := FProcessEntry32.th32ProcessID;
List.Add(Format('%-20s%d',[_processName,processID]));
Result := FProcessEntry32;
Break;
end;
ret := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
function GetProcessID(const ProcessName: string): Integer;
var
ret: Boolean;
processID: Cardinal;
_processName: string;
FSnapshotHandle: HWND;
FProcessEntry32: TProcessEntry32;
begin
Result := -1;
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ret := Process32First(FSnapshotHandle,FProcessEntry32);
while ret do
begin
_processName := ExtractFileName(FProcessEntry32.szExeFile);
if (AnsiCompareText(_processName,ProcessName)=0) then
begin
processID := FProcessEntry32.th32ProcessID;
Result := processID;
Break;
end;
ret := Process32Next(FSnapshotHandle,FProcessEntry32);
end;
end;
function IsDestProcess: Integer;
type
TExeNameEnum = (exeNotepad=1,exeTest=2,exeBds=3);
const
K_ExeName: array [TExeNameEnum] of string = (
('Notepad1.exe'),('Test.exe'),('bds.exe')
);
var
ExeName: TExeNameEnum;
//ExeName: exeNotepad..exeBds;
begin
Result := -1;
//for in 高版本才可以用如D2007
for ExeName in [Low(TExeNameEnum)..High(TExeNameEnum)] do
//for ExeName in [exeNotepad..exeBds] do
begin
ShowMessage(K_ExeName[ExeName]);
Result := GetProcessID(K_ExeName[ExeName]);
if Result > 0 then Break;
end;
end;