建projecta和projectb分别加入unita和unitb编译再看调用效果
unit unita;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ProcessInfo: TProcessInformation;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo: TStartUpInfo;
begin
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
CreateProcess(nil, PChar('b.exe'), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
H: THandle;
P: DWORD;
begin
H := FindWindow(nil, 'b');
if H <> 0 then
begin
GetWindowThreadProcessId(H, @P);
if P <> 0 then
if TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, P), $FFFFFFFF) then
showmessage('terminated B')
else
showmessage('can not terminate B');
end;
end;
end.
unit unitb;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
ProcessInfo: TProcessInformation;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var
StartupInfo: TStartUpInfo;
begin
FillChar(StartupInfo, Sizeof(StartupInfo), #0);
StartupInfo.cb := Sizeof(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_HIDE;
CreateProcess(nil, PChar('a.exe'), nil, nil, False, 0, nil, nil, StartupInfo, ProcessInfo);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
H: THandle;
P: DWORD;
begin
H := FindWindow(nil, 'a');
if H <> 0 then
begin
GetWindowThreadProcessId(H, @P);
if P <> 0 then
if TerminateProcess(OpenProcess(PROCESS_TERMINATE, False, P), $FFFFFFFF) then
showmessage('terminated A')
else
showmessage('can not terminate A');
end;
end;
end.