Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delphi, how to switch to another application if it is running 1

Status
Not open for further replies.

mhorvat

Programmer
Dec 11, 2011
2
0
0
SI
Hello,

I have problem with switching to another application from my own. For example I would like to switch to notepad if it is open. Not to open with shell execute a new instance of application.

I already tried all kind of api calls or messages but with no success.

Any help would be welcome...

thanks





 
Code:
var
  npadhandle: HWnd;
begin
  npadhandle := FindWindow('Notepad', nil);
  if npadhandle <> 0 then
    begin
      SetForegroundWindow(npadhandle);
      SendMessage(npadhandle, WM_SYSCOMMAND, SC_RESTORE, 0)
    end
  else
    Showmessage('Notepad not found.');
end;

It is not possible for anyone to acknowledge truth when their salary depends on them not doing it.
 
Is it possible to do this if have only process id or process name?
 
You need to use Process Explorer to know the window name, as well as get the process ID
 
to see if a process exists:

function TFrmKDSSwitch.processExists(exeFileName: string): Boolean;
{Uses TlHelp32}
var
ContinueLoop: BOOL;
FSnapshotHandle: THandle;
FProcessEntry32: TProcessEntry32;
begin
FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
FProcessEntry32.dwSize := SizeOf(FProcessEntry32);
ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);
Result := False;
while Integer(ContinueLoop) <> 0 do
begin
if ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =
UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =
UpperCase(ExeFileName))) then
begin
Result := True;
end;
ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);
end;

You call it depending on what the process name is

if not processExists('WBVod.exe') then
begin
SetCurrentDir('c:\Program Files\Wizbang\Waiter\WBVod');
shellexecute(Handle, 'open', 'c:\Program Files\Wizbang\Waiter\WBVod\WBVod.exe', nil, nil, SW_SHOWNORMAL) ;
end;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top