Einzelnen Beitrag anzeigen

speedy

Registriert seit: 19. Sep 2003
55 Beiträge
 
Delphi XE5 Professional
 
#41

AW: Process_Terminate funktioniert nicht

  Alt 15. Nov 2010, 23:48
Delphi-Quellcode:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, tlhelp32, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function GetProcessID(sProcName: String): Integer; //ProzessID an Hand der exe-Datei ermitteln
var
  hProcSnap: THandle;
  pe32: TProcessEntry32;
begin
  result := -1;
  hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
  if hProcSnap = INVALID_HANDLE_VALUE then exit;
  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
        result := pe32.th32ProcessID;
    end;
end;


procedure KillProcess(dwProcID: DWORD); // Prozess abschießen
var
  hProcess : Cardinal;
  dw : DWORD;
begin
  { open the process and store the process-handle }
  hProcess := OpenProcess(SYNCHRONIZE or PROCESS_TERMINATE, False, dwProcID);
  { kill it }
  TerminateProcess(hProcess, 0);
  { TerminateProcess returns immediately, so wie have to verify the result via
    WaitForSingleObject }

  dw := WaitForSingleObject(hProcess, 5000);
  case dw of
    WAIT_TIMEOUT: exit;
    WAIT_FAILED: exit;
  end;
end;


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;


procedure TForm1.FormCreate(Sender: TObject);
var pid:integer;

begin
  //ggf. killprocess2 durch killprocess ersetzen um andere Methode zu probieren

  pid := getprocessid('metin2.bin');
  if pid <> -1 then killprocess2(pid,5000);

  pid := getprocessid('metin2client.bin');
  if pid <> -1 then killprocess2(pid,5000);

  pid := getprocessid('metin2.exe');
  if pid <> -1 then killprocess2(pid,5000);

end;

end.
  Mit Zitat antworten Zitat