uses TlHelp32 // 引用单元
// aPathExe : 带全路径Exe
function KillAppExe(const aPathExe: string): Boolean;
const
PROCESS_TERMINATE = $0001;
var
_vHandle : THandle;
_vProEntry : TProcessEntry32;
_vIsFound : Boolean;
_vTempStr : string;
begin
Result := False;
_vHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
try
_vProEntry.dwSize := SizeOf(_vProEntry);
_vIsFound := Process32First(_vHandle, _vProEntry);
while _vIsFound do
begin
_vTempStr := _vProEntry.szExeFile;
if (UpperCase(_vTempStr) = UpperCase(ExtractFileName(aPathExe))) or
(UpperCase(_vTempStr) = UpperCase(aPathExe)) then
begin
Result := TerminateProcess(OpenProcess(PROCESS_TERMINATE,
Boolean(0), _vProEntry.th32ProcessID), 0);
DyjPlatLogs.WriteLog('KillAppExe', aPathExe);
end;
_vIsFound := Process32Next(_vHandle, _vProEntry);
end;
finally
CloseHandle(_vHandle);
end;
end;
但是对于服务程序,它会提示"拒绝访问".其实只要程序拥有Debug权限即可:
function EnableDebugPrivilege: Boolean;
function EnablePrivilege(hToken: Cardinal; PrivName: string; bEnable: Boolean): Boolean;
var
TP: TOKEN_PRIVILEGES;
Dummy: Cardinal;
begin
TP.PrivilegeCount := 1;
LookupPrivilegeValue(nil, pchar(PrivName), TP.Privileges[0].Luid);
if bEnable then
TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
else
TP.Privileges[0].Attributes := 0;
AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil, Dummy);
Result := GetLastError = ERROR_SUCCESS;
end;
var
hToken: Cardinal;
begin
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES, hToken);
result := EnablePrivilege(hToken, 'SeDebugPrivilege', True);
CloseHandle(hToken);
end;
阅读(4399) | 评论(0) | 转发(0) |