“This is the 13th day of my participation in the First Challenge 2022. For details: First Challenge 2022”

Start a process:

If we have a requirement to start another program in one program, we need to understand the concept of a process. To execute a program is to produce a process. The App process that executes is actually activated by the shell call CreateProcess. So to start another program in one program, you need CreateProcess ().

Function prototype:

CreateProcess(LPCSTR lpApplicationName, 
              LPSTR lpCommandLine, 
              LPSECURITY_ATTRIBUTES lpProcessAttributes,
              LPSECURITY_ATTRIBUTES lpThreadAttributes, 
              BOOL bInheritHandles, 
              DWORD dwCreationFlags,
              LPVOID lpEnvironment,
              LPCSTR lpCurrentDirectory,
              LPSTARTUPINFO lpStartupInfo,
              LPPROCESS_INFORMATION lpProcessInformation);
Copy the code

Example:

CString strPath = ""; // here I'll use full path + filename + extension CString strEnvironment = ""; // Path name STARTUPINFO si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si) ); si.cb = sizeof(si); ZeroMemory( &pi, sizeof(pi) ); CreateProcess(NULL, CT2W(strPath), NULL, NULL, FALSE, 0, NULL, strEnvironment, &si, &pi);Copy the code

The first argument, lpApplicationName, specifies an executable file name;

The second argument, lpCommandLine, specifies the command line argument to pass to the new process; If the first parameter is specified as NULL, the system uses the first paragraph (token) of the second parameter as the executable file name. If no extension is specified, the default extension is.exe. If no path is specified, the system follows five search paths to find the executable.

The five search paths are: 1. The directory where the caller’s executable resides; 2. The current working directory of the caller; 3. Windows directory; 4.Windows System directory; 5. Directories specified by path in the environment variable.

The third and fourth parameters specify the security properties of the two core objects, process object and thread object, respectively.

The fifth argument (TRUE or FALSE) sets whether these security attributes are to be inherited;

The sixth parameter can be a combination of many constants that affect the creation of a process. The most common of these constants is CREATE_SUSPENDED, which causes the main thread to be suspended immediately after the child process is created.

The seventh parameter specifies the area of environment variables used by the process. The environment variable that the child normally inherits from the parent needs to be specified as NULL;

The eighth parameter sets the child process’s working directory and working drive. If NULL is specified, the child process will use the parent process’s working directory and working drive.

The ninth argument is a pointer to the STARTUPINFO structure. Can be used to set the window title, position and size;

The tenth argument is a pointer to the PROCESS_INFORMATION structure;

typedef struct _PROCESS_INFORMATION
{
    HANDLE hProcess;
    HANDLE hThread;
    DWORD dwProcessId;
    DWORD dwThreadId;
}PROCESS_INFORMATION;
Copy the code

To end a process:

  • A process that wants to terminate itself can call VOID ExitProcesss(UINT fuExitCode);
  • BOOL TerminateProcess(HANDLE hProcess, UINT fuExitCode);

TerminateProcess is not recommended, not because it is too powerful, but because normally when a process TerminateProcess ends, the system notifies all DLLs opened by that process, but if you terminate a process with TerminateProcess, the system does not notify other opened DLLs.

Obviously, you can terminate a process as long as you have a handle on it.

When a process needs to be shut down, it sends a custom message to the other process and executes the shutdown command when the other process receives the message.