unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, SHDocVw;
type
TForm1 =
class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private-Deklarationen }
PID:DWord;
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
var
s:
String;
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
const
NameDesProgramms = '
C:\windows\explorer.exe';
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
begin
ZeroMemory(@StartupInfo, SizeOf(StartupInfo));
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOW;
if CreateProcess( PChar(NameDesProgramms),'
/select,c:\windows\system32\calc.exe',
nil,
nil, false,
CREATE_NEW_CONSOLE
or NORMAL_PRIORITY_CLASS,
nil,
nil, StartupInfo,
ProcessInfo)
then
try
PID := ProcessInfo.dwProcessId;
//PID ist eine private DWORD-Variable
finally
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end;
end;
type
PEnumInfo = ^TEnumInfo;
TEnumInfo =
record ProcessID: DWORD; HWND: THandle;
end;
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;
procedure TForm1.Button2Click(Sender: TObject);
var
wnd:Hwnd;
begin
if PID <> 0
then
begin
wnd:=FindMainWindow(PID);
if WND<>0
then
begin
s := '
Test';
SendMessage(wnd,wm_Settext,0,Integer(@s[1]));
end;
end;
end;
end.