VC WinExec method to open a specified program or file

  • 2020-04-01 21:27:12
  • OfStack

• • function prototype:
UINT Win Exec(LPCSTR lpCmdLine, UINT uCmdShow);
2 arguments:
LpCmdLine: points to a null-terminated string containing the command line for the application to be executed (file name plus optional parameters).
UCmdShow: defines how Windows application Windows are displayed and provides the value of the wShowWindow member of the STARTUPINFO parameter to the CreateProcess function.
Return value:
If the function is called successfully, the return value is greater than 31. If the function call fails, the return value is one of the following:
0: system memory or resources have been exhausted.
ERROR_BAD_FORMAT: EXE file is invalid (not win32.exe 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 considers WinExec obsolete, many times a simple WinExec function is the best way to run a new program. Simply pass the command line as the first argument, and you need to decide how to display the second argument of the program (which the program may ignore). Usually, it is set for SW_SHOW, also can try SW_MINIMIZED or SW_MAXIMIZED. WinExec does not allow all the options obtained with the CreateProcess, and it is simple.
Use the ShellExecute command
• • function prototype:
Quote:

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

2 arguments:
HWND: window handle to the parent window. This window receives any message boxes generated by the application.
LpOperation: a null-terminated string address that specifies the operation to be performed. The following operation string is valid:
This function opens the file specified by the parameter lpFile, which can be an executable 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.
"Explore" searches for the folder specified by the parameter lpFile, which should be a document file,
This parameter can be null. In this case, the function is used to open the file specified by the parameter lpFile.
LpFile: an empty terminated string address that specifies the file to open or print or the folder to open or search.
LpParameters: if the parameter lpFile specifies an executable file, lpParameters is a null-terminated string address that specifies the parameters to pass to the application. If lpFile specifies a document file, lpParameters should be empty.
LpDirectory: a null-terminated string address that specifies the default directory.
NShowCmd: if lpFile specifies an executable, the nShowCmd indicates how the application will display when it opens. If lpFile specifies a document file, nShowCmd should be empty.
Return value:
If the function is called successfully, the return value is greater than 32, otherwise it is an error value less than or equal to 32.
Description: you can use this function to open or search a shell folder. Open the folder in any of the following forms:
Code:

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

or
Quote:

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

Search folder, available in the following form

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

ShellExecute commands are obsolete but readily available. This command makes a request to the command interpreter to open, browse, or print a document or folder, and while you can run the program with ShellExecute, it usually only sends the document name, while the command interpreter decides to run that program. Also, the ShellExecute command is very useful when opening a directory folder.
Sample article
Here is an example of the use of the names WinExec and ShellExecute. The following program has an example console program that opens a text file in two different ways. The following program USES WinExec and explicitly specifies the use of notepad. Then, using ShellExecute, open the text file.
Program listing
Code:  

#include <windows.h> 
#include <iostream.h> 
void main(int argc,char *argv[]) 
{ 
cout < < " Opening with WinExecn " ; 
if (WinExec( " notepad readme.txt " ,SH_SHOW) <32) 
MessagBox(NULL, " Can't WinExec " ,NULL,MB_OK); 
cout < < " Press Entern " ; 
MessagBox(NULL, " Press OK to continue " , " Progrm Launched " ,MB_OK); 
cout < < " Opening with ShellExecuten " ; 
if (ShellExecute (NULL, " open " , " readme.txt " ,NULL,NULL,SW_SHOW) <(HANDLE) 32) 
MessagBox(NULL, " Can't ShellExecuten " ,NULL,MB_OK); 
}

Use the CreateProcess command
• • function prototype:
Code:

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 
); 

2 arguments:
LpApplicationName: points to a string that ends in an empty string that specifies the module to execute
LpCommandLine: points to a null-terminated string that defines the command line to execute.
LpProcessAttributes: points to a SECURITY_ATTRIBUTES structure that determines whether the returned handle is inheritable by the quilt process.
LpThreadAttributes: points to a SECURITY_ATTRIBUTES structure that determines whether the returned handle is inheritable by the quilt process.
BInheritHandles, : indicates whether the new process inherits the handle from the calling process.
DwCreationFlags: defines additional flags that control the creation of priority classes and processes.
LpEnvironment: refers to the environment block of a new process.
LpCurrentDirectory: points to a null-terminated string that defines 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 the presentation information about the new process.
Return value:
If the function call succeeds, 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 fully control a new process, you must call CreateProcess.
In the above parameters, the parameter lpStartupInfo is the STARTUPINFO structure. 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 often ignore most of these data items if they choose to do so. You can specify flags in the structure that indicate the data segment to be set. Sometimes, you don't want to set any information, and 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 parameter lpProcessInformation returns the process and thread handles, including the process and thread ids. These handles have access specified in the parameters lpProcessAttributes and lpThreadAttributes.
Note that some parameters for the CreateProcess are specific to the console application, while others are useful for various applications. In most cases, you do not have to fill in the STARTUPINFO structure, but you must provide it anyway. The return value is Boolean, and the return value of real interest occurs in the structure passed 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. You can send an ID to another process or use a handle to control a new process.

Related articles: