0

I've been looking for a way to open a program using a C++ application. I'm trying to make a program which launches two programs together, for example I want to launch a game with AutoHotkey and when the game closes AutoHotkey does too.

I can't seem to find a simple answer online, I've read posts of people saying to use system() but others say not to use that because it's resource heavy which isn't something I want.

What other ways could I use to launch a program? I'm fairly new to C++ I've been using it for about a month now so I'm not very experienced at all. I know there is a way in C# to launch a program something like: Process.Start("notepad.exe").

As many different way to this would be awesome to know, thanks in advance guys.

Paul R
  • 208,748
  • 37
  • 389
  • 560
ShadyOrb09
  • 75
  • 1
  • 10
  • http://stackoverflow.com/a/17703834/517073 – Stepan Yakovenko Apr 13 '17 at 15:25
  • Yeah I read that post earlier, but people were saying not to use the system() function. http://www.cplusplus.com/forum/lounge/17684/ – ShadyOrb09 Apr 13 '17 at 15:27
  • 3
    use CreateProcess() from `windows.h` (http://stackoverflow.com/a/15440094/4353712) – Sándor Mátyás Márton Apr 13 '17 at 15:28
  • You could use boost: http://stackoverflow.com/questions/7934982/how-to-launch-a-script-sh-or-bat-vai-boost-process – cfromme Apr 13 '17 at 15:37
  • Thanks for the answers, CreateProcess() seems so complex for such a simple task, makes it hard to understand for beginners. – ShadyOrb09 Apr 13 '17 at 15:38
  • if win32 native way is too complex why not write in c# (given that you seem to know it and you are running on windows) – pm100 Apr 13 '17 at 15:42
  • If you need to manage a group of processes as a unit, use [Job Objects](https://msdn.microsoft.com/en-us/library/windows/desktop/ms684161.aspx). If `CreateProcess` is too complex for you, there's nothing you can do to implement a robust, reliable solution. – IInspectable Apr 13 '17 at 16:48

1 Answers1

1

As more of an AHK than C++ programmer, combined with the fact that AHK is arguably more simple to program, I would ask if you have considered a different approach? You could put more of the functionality in the AHK program instead of in the C++.

I.e. Instead of having C++ start two programs, you could have it just start the Autohotkey program which then starts the game while it keeps running. Making the Autohotkey program check if the game is still running, and shut itself off if not, is quite easy too.

Thorondale
  • 63
  • 2