Reputation: 896
I am trying to learn windows programming. I would like to launch an executable program.exe
(say) from c++ code. I am able to achieve this by using CreateProcess()
method in windows. However, my problem is if the process is already created and running in the background then the windows for program.exe
should come to foreground otherwise a new process should be created and brought to the foreground. Any help will be appreciated.
Upvotes: 1
Views: 2317
Reputation: 11311
Is that program.exe
written by you? This functionality is better handled there: on start you check if there is already an instance running and if there is - activate it.
Otherwise - what do you do if there are multiple instances of program.exe
already running?
Upvotes: 0
Reputation: 596001
Look at Win32 API functions such as the following:
to discover if a given process is running, you can use:
CreateToolHelp32Snapshot(TH32CS_SNAPPROCESS)
with Process32First()
and Process32Next()
. See Taking a Snapshot and Viewing Processes.or
to find an existing window, you can use:
FindWindow()
or FindWindowEx()
, if you know the window's class name or title text ahead of time.or
EnumWindows()
with GetClassName()
and/or GetWindowText()
, if the window's class name or title text are dynamic but follow a pattern you can look for.to restore a window if it is minimized, you can use IsIconic()
with SetWindowPos(SW_RESTORE)
.
to bring a window into the foreground, you can use BringWindowToTop()
and/or SetForegroundWindow()
.
Upvotes: 2