Running Another Application
The last major piece of functionality to be implemented in your application is for the set of controls for running another program. If you remember, you added the names of three Windows applications into the combo box, and when you run your application, you can see these application names in the drop-down list. You can select any one of them, and the value area on the combo box is updated with that application name. With that part working as it should, you only need to add code to the Run Program button to actually get the value for the combo box and run the appropriate program. Once you create the function for the Run Program button using the Class Wizard, add the code in Listing 2.7 to the function.
LISTING 2.7. DAY2DLG.CPP--THE OnRunpgm FUNCTION STARTS OTHER WINDOWS APPLICATIONS.
1: void CDay2Dlg::OnRunpgm() 2: { 3: // TODO: Add your control notification handler code here 4: 5: /// 6: // MY CODE STARTS HERE 7: /// 8: 9: // Get the current values from the screen 10: UpdateData(TRUE); 11: 12: // Declare a local variable for holding the program name 13: CString strPgmName; 14: 15: // Copy the program name to the local variable 16: strPgmName = m_strProgToRun; 17: 18: // Make the program name all uppercase 19: strPgmName.MakeUpper(); 20: 21: // Did the user select to run the Paint program? 22: if (strPgmName == "PAINT") 23: // Yes, run the Paint program 24: WinExec("pbrush.exe", SW_SHOW); 25: 26: // Did the user select to run the Notepad program? 27: if (strPgmName == "NOTEPAD") 28: // Yes, run the Notepad program 29: WinExec("notepad.exe", SW_SHOW); 30: 31: // Did the user select to run the Solitaire program? 32: if (strPgmName == "SOLITAIRE") 33: // Yes, run the Solitaire program 34: WinExec("sol.exe", SW_SHOW); 35: 36: /// 37: // MY CODE ENDS HERE 38: ///
39: }
//The WinExec function is an obsolete Windows function. You really should use the CreateProcess function instead. However, the CreateProcess function has a number of arguments that are difficult to understand this early in programming using Visual C++. The WinExec function is still available and is implemented as a macro that calls the CreateProcess function. This allows you to use the much simpler WinExec function to run another application while still using the function that Windows wants you to use.
Another API function that can be used to run another application is the ShellExecute function. This function was originally intended for opening or printing files, but can also be used to run other programs.