program killmetin2;
{$APPTYPE CONSOLE}
uses
SysUtils,
TlHelp32,
Windows;
function AdjustTokenPrivileges(TokenHandle: THandle; DisableAllPrivileges: BOOL;
const NewState: TTokenPrivileges; BufferLength: DWORD;
PreviousState: PTokenPrivileges; ReturnLength: PDWORD): BOOL;
stdcall;
external '
advapi32.dll'
name '
AdjustTokenPrivileges';
function SetProcessPrivilege(
const Enable: Boolean;
const Privilege:
string): Boolean;
const
PrivAttrs:
array [Boolean]
of DWORD = (0, SE_PRIVILEGE_ENABLED);
var
Token: THandle;
TokenPriv: TTokenPrivileges;
begin
Result := False;
if (
not OpenThreadToken(GetCurrentThread(), TOKEN_ADJUST_PRIVILEGES, True,
Token))
then
WriteLn('
Could not open thread token');
if (
not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES,
Token))
then
begin
WriteLn('
Could not open process token');
SetLastError(ERROR_INVALID_HANDLE);
exit;
end;
WriteLn('
Got token');
TokenPriv.PrivilegeCount := 1;
LookupPrivilegeValue(
nil, PChar(Privilege), TokenPriv.Privileges[0].Luid);
TokenPriv.Privileges[0].Attributes := PrivAttrs[Enable];
Result := AdjustTokenPrivileges(Token, False, TokenPriv, SizeOf(TokenPriv),
nil,
nil);
if(
not Result)
then
begin
WriteLn('
Failed to set ', Privilege, '
privilege');
end;
CloseHandle(Token);
end;
function GetProcessID(sProcName:
String): DWORD;
var
hProcSnap: THandle;
pe32: TProcessEntry32;
begin
Result := 0;
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
if hProcSnap <> INVALID_HANDLE_VALUE
then
begin
try
pe32.dwSize := SizeOf(ProcessEntry32);
{ wenn es geklappt hat }
if Process32First(hProcSnap, pe32) = True
then
{ und los geht's: Prozess suchen }
while Process32Next(hProcSnap, pe32) = True
do
begin
if pos(sProcName, pe32.szExeFile) <> 0
then
begin
Result := pe32.th32ProcessID;
WriteLn('
Found process as ID: ', Result);
break;
end;
end;
finally
CloseHandle(hProcSnap);
end;
end
else
begin
WriteLn('
Failed to create snapshot');
end;
end;
procedure KillProcess(dwProcID: DWORD);
var
hProcess: THandle;
begin
if(SetProcessPrivilege(True, '
SeDebugPrivilege'))
then
try
hProcess := OpenProcess(PROCESS_TERMINATE, False, dwProcID);
if(0 <> hProcess)
then
begin
if(TerminateProcess(hProcess, 0))
then
WriteLn('
Terminated process')
else
WriteLn('
Failed to terminate process, error ', GetLastError());
end
else
WriteLn('
Failed to open process handle, error ', GetLastError());
CloseHandle(hProcess);
finally
SetProcessPrivilege(False, '
SeDebugPrivilege');
end;
end;
begin
try
KillProcess(GetProcessID('
metin2client.bin'));
WriteLn('
Press ENTER to quit');
ReadLn;
except
//on E: Exception do
//Writeln(E.ClassName, ': ', E.Message);
end;
end.