...
const sWinFx4Exe :
string = '
WINFx4.exe';
var frm_main: Tfrm_main;
extProcessHandle: THandle;
extWindowsHandle: THandle;
...
function GetWindowThreadProcessId(hWnd: HWND;
var dwProcessId: DWORD): DWORD;
stdcall;
external '
user32.dll'
name '
GetWindowThreadProcessId';
function MyEnumWindowProc(AHandle: THandle; LParam: LongWord): boolean;
stdcall;
var ProcessID: THandle;
begin
ProcessID := 0;
GetWindowThreadProcessID(AHandle, ProcessID);
Result :=
not (ProcessID = LParam);
if not Result
then extWindowsHandle := AHandle;
end;
function GetWindowHandleByExeName(
const AExeName:
string): THandle;
var SnapShot: THandle;
p: TProcessEntry32;
ProcessHandle: THandle;
begin
// Result := 0;
extWindowsHandle := 0;
ProcessHandle := 0;
p.dwSize := SizeOf(p);
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
try
if Process32First(SnapShot, p)
then
repeat
if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile)
then
ProcessHandle := p.th32ProcessID;
until (ProcessHandle <> 0)
or not Process32Next(SnapShot, p);
EnumWindows(@MyEnumWindowProc, ProcessHandle);
Result := extWindowsHandle;
finally
CloseHandle(SnapShot);
end;
end;
function GetProcessID(Exename:
string): DWORD;
var
hProcSnap: THandle;
pe32: TProcessEntry32;
begin
result := 0;
hProcSnap := CreateToolHelp32SnapShot(TH32CS_SNAPPROCESS, 0);
if hProcSnap <> INVALID_HANDLE_VALUE
then
begin
pe32.dwSize := SizeOf(ProcessEntry32);
if Process32First(hProcSnap, pe32) = true
then
begin
while Process32Next(hProcSnap, pe32) = true
do
begin
if pos(Exename, pe32.szExeFile) <> 0
then
result := pe32.th32ProcessID;
end;
end;
CloseHandle(hProcSnap);
end;
end;
function GetProcessHandleFromID(ID: DWORD): THandle;
begin
result := OpenProcess(SYNCHRONIZE, False, ID);
CloseHandle(result);
end;
procedure Tfrm_main.Beenden1Click(Sender: TObject);
begin
close;
end;
procedure Tfrm_main.btn_getHandleClick(Sender: TObject);
begin
extProcessHandle := GetProcessHandleFromID(GetProcessID(sWinFx4Exe));
extWindowsHandle := GetWindowHandleByExeName(sWinFx4Exe);
if ((extProcessHandle = 0)
or (extWindowsHandle = 0))
then
begin
Label1.Caption := '
Fx4 läuft nicht';
btn_sendKey.enabled := false;
end
else
begin
Label1.Caption := '
Fx4 läuft';
btn_sendKey.enabled := true;
end;
Application.HandleMessage;
end;
procedure Tfrm_main.btn_sendKeyClick(Sender: TObject);
begin
iSendKeyCode := strtoint(ed_keyCode.text);
PostMessage (extWindowsHandle,
// Handle des externen Programmes
WM_CHAR,
// Übergabe = Keycode
iSendKeyCode,
// KeyCode
0
// Dritter Parameter
);
end;
procedure Tfrm_main.btn_setTextClick(Sender: TObject);
var sEditValue :
string;
Buffer :
array of Char;
begin
sEditValue := ed_text.text;
SendMessage(extWindowsHandle, WM_SETTEXT, 0, Integer(Pchar(sEditValue)));
// Text des Objektes schreiben
end;