SoFunction
Updated on 2025-03-02

VC WinExec opens a specified program or file

⑴ Function prototype:
UINT Win Exec(LPCSTR lpCmdLine, UINT uCmdShow);
⑵ Parameters:
lpCmdLine: Points to an empty ending string containing the command line of the application to be executed (file name plus optional parameters).
uCmdShow: Defines how the window of a Windows application is displayed and provides the CreateProcess function with the value of the wShowWindow member of the STARTUPINFO parameter.
⑶ Return value:
If the function call is successful, the return value is greater than 31. If the function call fails, the return value is one of the following:
① 0: The system memory or resources have been exhausted.
② ERROR_BAD_FORMAT: The EXE file is invalid (non-or.EXE image error).
③ ERROR_FILE_NOT_FOUND: The specified file was not found.
④ ERROR_PATH_NOT_FOUND: The specified path was not found.
Although Microsoft believes that WinExec is outdated, in many cases, simple WinExec functions are still the best way to run new programs. Simply passing the command line as the first parameter also requires deciding how to display the second parameter of the program (which may ignore it). Usually, set it to SW_SHOW, you can also try SW_MINIMIZED or SW_MAXIMIZED. WinExec does not allow all the options you get with CreateProcess, and it is really simple.
Use the ShellExecute command
⑴ Function prototype:
Quote:
Copy the codeThe code is as follows:

HINSTANCE ShellExecute(HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile, LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd);

⑵ Parameters:
hwnd: The window handle pointing to the parent window. This window receives any information box generated by the application.
lpOperation: An empty ending string address, which specifies the operation to be performed. The following operation string is valid:
"open" This function opens the file specified by the parameter lpFile. This file can be an executable file or a document file or a folder.
"print" This function prints the file specified by the parameter lpFile. This file should be a document file. If the file is an executable file, open the file.
"explore" This function searches for the folder specified by the parameter lpFile, this file should be a document file.
This parameter can be empty. In this case, the function is used to open the file specified by the parameter lpFile.
lpFile: An empty ending string address, which specifies the file to be opened or printed or the folder to be opened or searched.
lpParameters: If the parameter lpFile specifies an executable file, lpParameters is an empty ending string address, which specifies the parameters to be passed to the application. If lpFile specifies a document file, lpParameters should be empty.
lpDirectory: An empty ending string address, this string specifies the default directory.
nShowCmd: If lpFile specifies an executable file, nShowCmd shows how the application is displayed when it is opened. If lpFile specifies a document file, nShowCmd should be empty.
⑶ Return value:
If the function call is successful, the return value is greater than 32, otherwise it is an error value less than or equal to 32.
Note: You can use this function to open or search for a shell folder. Opening a folder can be any of the following forms:
Code:
Copy the codeThe code is as follows:

ShellExecute(handle, NULL, path_to_folder, NULL, NULL, SW_SHOWNORMAL);

or
Quote:
Copy the codeThe code is as follows:

ShellExecute(handle, "open", path_to_folder, NULL, NULL, SW_SHOWNORMAL);

Search for folders, which can be used as follows
Copy the codeThe code is as follows:

ShellExecute(handle, "explore", path_to_folder, NULL, NULL, SW_SHOWNORMAL);

The ShellExecute command is outdated but easy to obtain. This command makes a request to open, browse, or print a document or folder to the command interpreter. Although it can be run with ShellExecute, it usually only sends the document name, and the command interpreter decides which program to run. In addition, the ShellExecute command is very useful when opening the directory folder.
⑷Program example
Here is an example to use WinExec and ShellExecute. The following program has a console program example, which uses two different methods to open a text file. The following program uses WinExec and explicitly specifies the use of Notepad program. Then, use ShellExecute to open the text file.
Program list
Code: 
Copy the codeThe code is as follows:

#include <>
#include <>
void main(int argc,char *argv[])
{
cout < <”Opening with WinExec\n”;
if (WinExec(“notepad ”,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”,””,NULL,NULL,SW_SHOW) <(HANDLE) 32)
MessagBox(NULL,”Can't ShellExecute\n”,NULL,MB_OK);
}

Use the CreateProcess command
⑴ Function prototype:
Code:
Copy the codeThe code is as follows:

BOOL CreateProcess(
LPCTSTR lpApplicationName,
LPTSTR lpCommandLine,
LPSECURITY_ATTRIBUTES lpProcessAttributes,
LPSECURITY_ATTRIBUTES lpThreadAttributes,
BOOL bInheritHandles,
DWORD dwCreationFlags,
LPVOID lpEnvironment,
LPCTSTR lpCurrentDirectory,
LPSTARTUPINFO lpStartupInfo,
LPPROCESS_INFORMATION lpProcessInformation
);

⑵ Parameters:
lpApplicationName: Points to a string ending with an empty specified module to be executed
lpCommandLine: Points to a string ending with an empty definition of the command line to be executed.
lpProcessAttributes: Points to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by the child process.
lpThreadAttributes: Points to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by the child process.
bInheritHandles,: Indicates whether the new process inherits a handle from the calling process.
dwCreationFlags: Defines additional flags for controlling priority classes and process creation.
lpEnvironment: An environment block pointing to a new process.
lpCurrentDirectory: Points to a string ending with an empty definition of the current drive and current directory of the child process.
lpStartupInfo: Points to a STARTUPINFO structure that defines how the main window of the new process will be displayed.
lpProcessInformation: Points to the PROCESS_INFORMATION structure, which accepts representation information about the new process.
⑶ 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. If you want to have full control of a new process, you must call CreateProcess.
Among the above parameters, the parameter lpStartupInfo is a STARTUPINFO structure. It can be used to set the console title, the initial size and position of the new window, and redirect standard input and output. New programs can usually ignore most of these data items if you choose to do that. The flags in the structure can be specified, indicating 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 empty structure (determine the setting size to cb, and set the dwFlags member to 0). The parameter lpProcessInformation returns the process and thread handles, and also includes the process and thread ID. These handles have access specified in the parameters lpProcessAttributes and lpThreadAttributes.
Note that some parameters for CreateProcess are specific to console applications, while others are useful for various applications. In most cases, it is not necessary to fill in the STARTUPINFO structure, but it must be provided anyway. The return value is Boolean, and the return value of the real interest occurs in the structure transmitted as a parameter (PROCESS_INFORMATION). CreateProcess returns the process ID and its handle in this structure, as well as the initial thread ID and its handle. The ID can be sent to other processes, or a handle can be used to control a new process.