procedure KillProcess2(
const dwProcID, dwTimeOut: DWORD);
var
hProcess, Size,
WrittenBytes, TID,
hThread, ExitCode: DWord;
Memory: Pointer;
procedure _injectedCode_ExitProcess(P: Pointer);
stdcall;
type
TExitProcess =
procedure(uExitCode: UINT);
stdcall;
begin
TExitProcess(P)(0);
end;
procedure _injectedCode_End();
asm
ret
end;
begin
hProcess := OpenProcess( GENERIC_WRITE, False, dwProcID );
if hProcess <> ERROR
then
try
Size := DWord( @_injectedCode_End ) - DWord( @_injectedCode_ExitProcess );
Memory := VirtualAllocEx( hProcess,
NIL, Size, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
if Assigned( Memory )
then
try
WriteProcessMemory( hProcess, Memory, @_injectedCode_ExitProcess, Size, WrittenBytes );
if WrittenBytes = Size
then
begin
hThread := CreateRemoteThread( hProcess,
NIL, 0, Memory, GetProcAddress( GetModuleHandle( '
kernel32.dll' ), '
ExitProcess' ),
0, TID );
if hThread <> ERROR
then
WaitForSingleObject( hThread, dwTimeOut );
end;
finally
VirtualFreeEx( hProcess, Memory, Size, MEM_DECOMMIT );
end;
finally
CloseHandle( hProcess );
end;
end;