var frm_main: Tfrm_main;
extProgHandle: THandle;
...
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.btn_getHandleClick(Sender: TObject);
begin
extProgHandle := GetProcessHandleFromID(GetProcessID('
WINFx4.exe'));
if extProgHandle = 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
SendMessage (extProgHandle,
// Handle des externen Programmes
WM_CHAR,
// Übergabe = Keycode
68,
// KeyCode von "ALT+D" (DateiMenü)
0
// Dritter Parameter
);
end;