Differences between WinExec () and system ()

Source: Internet
Author: User

WinExec () is a WIN32 API. Its first parameter must contain an executable file name. System () is a C-library function and it accepts a DOS command. The difference between ShellExecute () and WinExec () is that there are too many zookeeper I think in terms of functionality: createprocess> shellexecute> winexec is also complex! Winexec () is only used for executable files. It is an old function. It is easy to use but not recommended! (Poor compatibility) shellexcute () is to open any file through the windows Shell, non-executable files are automatically opened through the associated program for executable files, the difference is not big, however, shellexcute can specify the runtime working path winexec () to be returned only after GetMessage or timeout! Both CreateProcess and ShellExecute are directly returned! LaunchAppAndWait () LaunchAppAndWait syntax in InstallShield: LaunchAppAndWait (szProgram, szCmdLine, lWait); Description: The LaunchAppAndWait function runs the application with the command line parameters specified by szProgram. The third parameter, lWait indicates whether to wait until the running application is terminated before the installation continues. An installer can only monitor applications specified by szProgram. If the application is to run other applications or processes, the installer cannot monitor them. Therefore, the installer will continue after the first application ends, even if other applications running by the first application are still running. Note: If the running application fails to terminate, the installer will wait for the running application to complete. Parameter: szProgram specifies the name of the application to be run. We recommend that you specify the full path and file name of the application. If you do not include a path, InstallShield uses the same search order used by the Windows API function CreateProcess to locate the file. If the file cannot be found in these locations, the function will fail. If the application's fully qualified name includes the long folder name and/or a long file name, it will be passed to LongPathToQuote before the szCommand is passed to LaunchAppAndWait. SzCmdLine specifies the command line parameters that are passed to the running application. To run an application without command line parameters, pass an empty string. LWait specifies whether to wait for the running application to terminate before the installer continues. At this parameter location, pass one of the following predefined constants: NOWAIT: specifies that the installer continues immediately after running the application, and the application will run at the same time as the installation script. Note that using this parameter is equivalent to calling the LaunchApp function. WAIT: specifies that the installer must WAIT until the application running by the function is terminated. Return Value: 1: indicates that the application runs successfully. <0: indicates that the application is not running. Note: The InstallShield installer uses the CreateProcess function. After InstallShield runs the application, it looks for the window handle of the mounted application. If it finds the window handle, it waits until the application window disappears. · The installer cannot monitor an application that does not create a window. If no window is created for the specified application, the installer continues immediately after running the application. Note that the application window does not need to be visible, but it must exist so that the installer can wait. · Some applications try to mount DLLs and cannot run correctly when those DLLs cannot be located. To ensure that an application can find the desired DLLs, it is necessary to change the directory containing the executable application before calling LaunchAppAndWait. To change the current directory, call the ChangeDirectory function. · If the running application fails to be terminated, the installer will wait for the running application to complete. · LaunchAppAndWait runs the DOS program in a full screen DOS window. To run a DOS program in a different type of window, you must directly call Windows APIs. Example ---- You can do this: WinExec ("command/C dir C: \> dir.txt", SW_HIDE); system ("dir> dir.txt") 1: UINT WinExec (LPCSTR lpCmdLine, // address of command line UINT uCmdShow // window style for new application); Header File: winbase. h. Explain that the first parameter in the Win32 SDK Reference is the program name and parameter number, and the second parameter is used to specify the object How to explain after the program is started. EX1: WinExec ("Notepad.exe c :\\ autoexec. bat ", SW_SHOW); when the notepad.exe line is passed, the program window EX2: WinExec (" Notepad.exe ", SW_SHOWMINIMIZED) is displayed normally; when the notepad.exe line is opened, the program window EX3 is minimized: winExec ("Command.com/c dir c :\\", SW_SHOW); export row dir C: \. After completion, close MSDOS export window EX4: winExec ("Command.com/k dir c :\\", SW_SHOW); the complete row dir C: \. After completion, do not close the MSDOS success window. Note: the first parameter number can be used for long term names, but if there is space in the long term name, the program will generate a warning when the program runs, the reason why the program was born was that the program itself had neglected the relationship between long term names when determining the number of concurrent users. If this happens, remember to change the program name Short answer name. 2: ShellExecute (HWND hwnd, // handle to parent window LPCTSTR lpOperation, // pointer to string that specifies operation to perform LPCTSTR lpFile, // pointer to filename or folder name string LPCTSTR lpParameters, // pointer to string that specifies executable-file parameters LPCTSTR lpDirectory, // pointer to string that specifies default directory INT nShowCmd // whether file is shown when open Ed); Head File: ShellApi. h. Example Description: Please refer to the Win32 SDK Reference ===================== use the WinExec command (1) function prototype: UINT Win Exec (maid, UINT uCmdShow); (2) parameter: lpCmdLine: point to an empty ending string, the string contains the command line of the application to be executed (the file name is added with an optional parameter ). UCmdShow: defines how Windows application Windows are displayed, and provides the value of the wShowWindow member of STARTUPINFO for the CreateProcess function. (3) Return Value: If the function call is successful, the return value is greater than 31. If the function call fails, return one of the following values: ① 0: The system memory or resources are exhausted. ② ERROR_BAD_FORMAT: the EXE file is invalid (non-Win32.EXE or. EXE image error ). ③ ERROR_FILE_NOT_FOUND: the specified file is not found. ④ ERROR_PATH_NOT_FOUND: the specified path is not found. Microsoft considers WinExec obsolete, but in many cases, simple WinExec functions are still the best way to run new programs. Simply passing the command line that acts as the first parameter also requires deciding how to display the second parameter of the Program (which may ignore it. Generally, set it to SW_SHOW. You can also try SW_MINIMIZED or SW_MAXIMIZED. WinExec does not allow all options obtained using CreateProcess, but it is indeed simple. Use the ShellExecute command (1) function prototype: HINSTANCE ShellExecute (HWND hwnd, LPCTSTR lpOperation, lpFile, lptstr lpParameters, lptstr lpDirectory, INT nShowCmd); (2) parameter: hwnd: the window handle pointing to the parent window. This window receives any information boxes generated by the application. LpOperation: An empty string address, which specifies the operation to be performed. The following Operation string is valid: "open" This function opens the file specified by the lpFile parameter. This file can be an executable file or document file or a folder. "Print" This function prints the file specified by the lpFile parameter. This file should be a document file. If this file is an executable file, open it. "E" This function searches for folders specified by the lpFile parameter. This file should be a document file, which can be empty. In this case, the function is used to open the file specified by the lpFile parameter. LpFile: An empty ending string address. This string specifies the file to be opened or printed or the folder to be opened or searched. LpParameters: If the lpFile parameter specifies an executable file, lpParameters is an empty string address, which specifies the parameter to be passed to the application. If lpFile specifies a document file, lpParameters should be empty. LpDirectory: An empty ending string address, which specifies the default directory. NShowCmd: If lpFile specifies an executable file, nShowCmd indicates how the application is displayed when it is opened. If lpFile specifies a document file, nShowCmd should be blank. (3) Return Value: if the function is successfully called, the return value is greater than 32; otherwise, it is an error value smaller than or equal to 32. Note: You can use this function to open or search for a shell folder. Open a folder in any of the following formats: ShellExecute (handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL); or ShellExecute (handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL); search for folders. The following forms can be used: ShellExecute (handle, "cmde", path_to_folder, NULL, NULL, SW_SHOWNORMAL); the ShellExecute command is outdated but easy to get. This command sends a request to the command interpreter to open, browse, or print a document or folder. Although you can run the program using ShellExecute, only the document name is usually sent, the command interpreter decides the program to run. In addition, the ShellExecute command is very useful when you open a directory folder. (4) The following is an example of how to use WinExec and ShellExecute. The following program provides a console program example, which uses two different methods to open a text file. The following program uses WinExec and explicitly specifies the Notepad program. Then, use ShellExecute to open a text file. Program list # include <windows. h> # include <iostream. h> void main (int argc, char * argv []) {cout <"Opening with WinExec \ n"; if (WinExec ("notepad readme.txt", SH_SHOW) <32) messagBox (NULL, "Can't WinExec", NULL, MB_ OK); cout <"Press Enter \ n"; MessagBox (NULL, "Press OK to continue ", "Progrm Launched", MB_ OK); cout <"Opening with ShellExecute \ n"; if (ShellExecute (null,#open}#readme.txt ", NULL, NULL, SW_SHOW) <( HANDLE) 32) MessagBox (NULL, "Can't ShellExecute \ n", NULL, MB_ OK);} use the CreateProcess command (1) function prototype: BOOL CreateProcess (LPCTSTR lpApplicationName, LPTSTR lpCommandLine, LPSECURITY_ATTRIBUTES lpProcessAttributes, LPSECURITY_ATTRIBUTES attributes, BOOL bInheritHandles, DWORD dwCreationFlags, LPVOID lpEnvironment, LPCTSTR lpCurrentDirectory, LPSTARTUPINFO lpStartupInfo, LPPROCESS_INFORMATION lpProcessIn Formation); (2) parameter: lpApplicationName: points to a string ending with an empty value, which specifies the module to be executed lpCommandLine: points to a string ending with an empty value, this string defines the command line to be executed. LpProcessAttributes: points to a SECURITY_ATTRIBUTES structure, which determines whether the returned handle can be inherited by the quilt process. LpThreadAttributes: points to a SECURITY_ATTRIBUTES structure, which determines whether the returned handle can be inherited by the quilt process. BInheritHandles: indicates whether the new process inherits the handle from the called process. DwCreationFlags: defines the control priority class and additional identifier for Process Creation. LpEnvironment: point to the Environment block of a new process. LpCurrentDirectory: point to an empty string that defines the current drive and directory of the child process. LpStartupInfo: points to a STARTUPINFO structure, which defines how the new process's main window will be displayed. LpProcessInformation: point to the PROCESS_INFORMATION structure, which accepts the representation information about the new process. (3) Return Value: If the function call is successful, the return value is not 0. If the function call fails, the return value is 0. The ShellExecute and WinExec commands are used for simple jobs. To completely control a new process, you must call CreateProcess. In the preceding parameters, the lpStartupInfo parameter is the STARTUPINFO structure. You can set the title of the console, the initial size and position of the new window, and redirect standard input and output. Most of these data items can be ignored by new programs. You can specify a flag in the struct that indicates the data segment to be set. Sometimes, if you do not want to set any information, you must also pass a valid pointer to the null structure (make sure to set the size to cb, and set the dwFlags member to 0 ). The lpProcessInformation parameter returns the process and thread handle, and also contains the process and thread ID. These handles have the access specified in the lpProcessAttributes and lpThreadAttributes parameters. Note that some parameters for CreateProcess are specific to the console application, while other parameters are useful to various applications. In most cases, you do not have to enter the STARTUPINFO structure, but you must provide it anyway. The return value is Boolean, and the returned value that is really interest occurs in the structure transmitted as a parameter (PROCESS_INFORMATION ). CreateProcess returns the process ID and its handle in the structure, as well as the initial thread ID and its handle. You can send an ID to another process or use a handle to control the new process. Function Name: system www.2cto.com function prototype: int system (const char * command) function: Call the doscommand function and return: parameter description: file: <stdlib. h> # include <stdlib. h> # include <stdio. h> int main () {system ("dir"); return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.