绑架其它程序
-------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo = record
ProcessID : DWORD;
HWND : THandle;
end;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Button2: TButton;
Label5: TLabel;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
hwndOldParent,hwndNotePad : integer;
implementation
{$R *.dfm}
{
====================
用于取得启动程序句柄
====================
}
function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;
var
PID : DWORD;
begin
GetWindowThreadProcessID(Wnd, @PID);
Result := (PID <> EI.ProcessID) or
(not IsWindowVisible(WND)) or
(not IsWindowEnabled(WND));
if not result then EI.HWND := WND;
end;
{
=====================
用于取得启动程序句柄
=====================
}
function FindMainWindow(PID: DWORD): DWORD;
var
EI : TEnumInfo;
begin
EI.ProcessID := PID;
EI.HWND := 0;
EnumWindows(@EnumWindowsProc, Integer(@EI));
Result := EI.HWND;
end;
//用于 NOTEPAD
procedure TForm1.Button1Click(Sender: TObject);
begin
hwndNotePad:=FindWindow(PChar(Edit1.Text),0);
if hwndNotePad = 0 then
Showmessage('没找到')
else begin
hwndOldParent:=GetParent(hwndNotePad);
Windows.SetParent(hwndNotePad,handle);
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
Windows.SetParent(hwndNotePad,hwndOldParent);
end;
用于我的程序自选启动的程序
procedure TForm1.Button2Click(Sender: TObject);
var
SI : TStartupInfo;
PI : TProcessInformation;
H : THandle;
S : String;
EXEFileName : String;
begin
if not OpenDialog1.Execute then exit;
EXEFileName := OpenDialog1.FileName;
ZeroMemory(@SI, SizeOf(SI));
ZeroMemory(@PI, SizeOf(PI));
SI.cb := SizeOf(SI);
if CreateProcess(nil,PChar(EXEFileName), nil, nil, FALSE, 0 ,nil,nil, SI, PI) then
begin
//注意!
WaitForInputIdle(PI.hProcess, INFINITE);
H := FindMainWindow(PI.dwProcessID);
if H > 0 then
begin
SetLength(S, 255);
GetWindowText(H, PChar(S), 255);
SetLength(S, StrLen(PChar(S)));
//绑架
hwndOldParent:=GetParent(H);
Windows.SetParent(H,handle);
ShowMessage(S);
end;
CloseHandle(PI.hProcess);
CloseHandle(PI.hThread);
end;
end;
end.