Three Windows SDK functions: winexec, ShellExecute, and CreateProcess. You can call other programs.
Winexec
This function is the simplest and has only two parameters. The prototype is as follows:
Uint winexec (
Maid, // command path
Uint ucmdshow // Display Mode
;
The usage is as follows:
Winexec ("notepad.exe", sw_show); // open notepad
Winexec ("D:" "Program Files" "test" "test.exe", sw_showmaximized); // open test.exe in simplified format.
Note: Ms does not recommend this API. Below words is from msdn:
NoteThis function is provided only for compatibility with 16-bit windows. Applications shocould useCreateProcessFunction. If you must useWinexecFor legacy reasons, make sure the application name is enclosed in quotation marks as shown in the example below.
WinExec("""C:""Program Files""MyApp.exe"" -L -S", ...)
ShellExecute
The prototype is as follows:
Hinstance ShellExecute (
Hwnd, // parent window handle
Lpctstr lpoperation, // operation, opening method "edit", "lead E", "open", "find", "print", "null"
Lptstr lpfile, // file name, which can be added to the path above
Lptstr lpparameters, // Parameter
Lptstr lpdirectory, // Default Folder
Int nshowcmd // Display Mode
);
The usage is as follows:
ShellExecute (null, "open", "C:" test.txt ", null, null, sw_shownormal); // open the C:" test.txt File
ShellExecute (null, "open", ": URL: http://www.google.com",/null, null, sw_shownormal); // open the webpage www.google.com
ShellExecute (null, "Explore", "d:" "C ++", null, null, sw_shownormal); // open the Directory D: "C ++
ShellExecute (null, "print", "C:" test.txt ", null, null, sw_hide); // print the file C:" test.txt
ShellExecute does not support targeted output.
CreateProcess
Use below function directly:
//////////////////////////////////////// ///////////////////////////////////////
//
// Execapp ()
//
// Purpose: runs the specified application (replacement for winexec)
//
// Parameters: lpszcommandline-[in] command line (including EXE filepath)
// That is passed to CreateProcess ()
// Wshowcmd-[in] specifies how APP window is to be shown.
// See showwindow () in msdn for possible values.
//
// Returns: bool-true = CreateProcess () succeeded
//
Bool execapp (lpctstr lpszcommandline, word wshowcmd/* = sw_shownormal */)
{
Bool rc = false;
If (lpszcommandline & (lpszcommandline [0]! = _ T ('"0 ')))
{
Startupinfo Si = {0 };
Si. cb = sizeof (SI );
Si. dwflags = startf_useshowwindow;
Si. wshowwindow = wshowcmd;
Process_information Pi = {0 };
Rc =: CreateProcess (null, (lptstr) lpszcommandline, null, null, false,
Normal_priority_class, null, null, & Si, & PI );
Trace (_ T ("CreateProcess returned % d For <% S>" N "), RC, lpszcommandline );
// Close process and thread handles now (app will continue to run)
If (PI. hprocess)
: Closehandle (PI. hprocess );
If (PI. hthread)
: Closehandle (PI. hthread );
}
Return RC;
}
Note the following when using these three functions:
1. Define the header file
The following two header files must be defined in the header file stdafx. h:
# Include <shlobj. h> //Can be replacedWindows. h
# Include <shellapi. h>
If the header file is defined# Include <windows. h># Include <shlobj. h> now, "windows. H "not only contains" shellapi. H ", it also defines many data types, if not, shellapi. h itself will fail.
2, Define the path
The path represented in C ++ must be "rather than", so the preceding three functions indicate that the paths are:
Disk: "" directory ""... "File Name