6.4: How do I run another program from my program? (top)
There are several functions that run other programs. The simplest is WinExec():
WinExec ( "C:\\path\\to\\program.exe", SW_SHOWNORMAL );
There is also ShellExecute(), which can run executables as well as files that are associated with a program. For example, you can "run" a text file, as shown here:
ShellExecute ( hwndYourWindow, "open",
"C:\\path\\to\\readme.txt",
NULL, NULL, SW_SHOWNORMAL );
In this example, ShellExecute() looks up the program associated with .TXT files and runs that program. ShellExecute() also lets you set the program's starting directory and additional command line parameters. See the MSDN docs on ShellExecute() for more info.
If you want complete control over every aspect of the program launching process, use CreateProcess(). CreateProcess() has a ton of options, so see MSDN for all the details. Here is a simple example:
STARTUPINFO si = { sizeof(STARTUPINFO) };
PROCESS_INFORMATION pi = {0};
BOOL bSuccess;
bSuccess = CreateProcess ( NULL, "\"C:\\Program Files\\dir\\program.exe\"",
NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS,
NULL, NULL, &si, &pi );
Note that the program name should be enclosed in quotes, as shown above, if the path contains spaces.
If CreateProcess() succeeds, be sure to close the handles in the PROCESS_INFORMATION structure once you don't need them anymore.
CloseHandle ( pi.hThread );
CloseHandle ( pi.hProcess );
Of course, if all you need to do is just run a program, CreateProcess() is probably overkill, and ShellExecute() would be sufficient.
// see FAQ 6.4 for all the parameters
bSuccess = CreateProcess ( ..., &si, &pi );
If the CreateProcess() call succeeds, the console window will be hidden because we specified SW_HIDE in the wShowWindow member.