WinExec Kill

Delphi'de kod yazma ile ilgili sorularınızı bu foruma yazabilirsiniz.
Cevapla
Kullanıcı avatarı
haydarxxx
Üye
Mesajlar: 668
Kayıt: 09 May 2005 11:31
Konum: izmir

WinExec Kill

Mesaj gönderen haydarxxx »

Kod: Tümünü seç

 WinExec('C:\UsbAnahtar\Data\Lock.exe',sw_ShowNormal);
olarak bir exe çalıştırıyorum.
çağırdığımız exenin çalışmasını kill olarak nasıl durdururuz kapatırız.wi7 ve üstü versiyonlar için
ertank
Kıdemli Üye
Mesajlar: 1651
Kayıt: 12 Eyl 2015 12:45

Re: WinExec Kill

Mesaj gönderen ertank »

Merhaba,

Aşağıdaki şekilde deneyebilirsiniz. Eğer uygulama kapanmıyor ise bilgim dahilinde "kapatmaya zorlama" yapamazsınız. Bunu sadece windows işletim sistemi kendisi yapıyor.

Ünite

Kod: Tümünü seç

unit Unit1;

interface

uses
  Winapi.Windows,
  Winapi.Messages,
  System.SysUtils,
  System.Variants,
  System.Classes,
  Vcl.Graphics,
  Vcl.Controls,
  Vcl.Forms,
  Vcl.Dialogs,
  Vcl.StdCtrls;

type
  TFindWindowRec = record
  ModuleToFind: string;
  FoundHWnd: HWND;
end;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function EnumWindowsCallBack(Handle: hWnd; var FindWindowRec: TFindWindowRec): BOOL; stdcall;

var
  Form1: TForm1;
  FindWindowRec: TFindWindowRec;

implementation

{$R *.dfm}

uses
  PsAPI;

function EnumWindowsCallBack(Handle: hWnd; var FindWindowRec: TFindWindowRec): BOOL; stdcall;
const
  C_FileNameLength = 256;
var
  WinFileName: string;
  PID, hProcess: DWORD;
  Len: Byte;
begin
  Result := True;
  SetLength(WinFileName, C_FileNameLength);
  GetWindowThreadProcessId(Handle, PID);
  hProcess := OpenProcess(PROCESS_ALL_ACCESS, False, PID);
  Len := GetModuleFileNameEx(hProcess, 0, PChar(WinFileName), C_FileNameLength);
  if Len > 0 then
  begin
    SetLength(WinFileName, Len);
    if SameText(WinFileName, FindWindowRec.ModuleToFind) then
    begin
      Result := False;
      FindWindowRec.FoundHWnd := Handle;
    end;
  end;
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  WinExec('C:\Windows\SysWOW64\Notepad.exe', SW_SHOWNORMAL);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  FindWindowRec.ModuleToFind := 'C:\Windows\SysWOW64\Notepad.exe';
  FindWindowRec.FoundHWnd := 0;
  EnumWindows(@EnumWindowsCallback, integer(@FindWindowRec));
  if FindWindowRec.FoundHWnd <> 0 then
  begin
    SendMessage(FindWindowRec.FoundHWnd, WM_CLOSE, 0, 0);
  end;
end;

end.
Form:

Kod: Tümünü seç

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 299
  ClientWidth = 635
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  PixelsPerInch = 96
  TextHeight = 13
  object Button1: TButton
    Left = 32
    Top = 32
    Width = 89
    Height = 25
    Caption = 'Notepad a'#231
    TabOrder = 0
    OnClick = Button1Click
  end
  object Button2: TButton
    Left = 32
    Top = 72
    Width = 89
    Height = 25
    Caption = 'Notepad kapat'
    TabOrder = 1
    OnClick = Button2Click
  end
end
Cevapla