I want to launching a sub application from the main application using CreateProcess function with the following steps:
- launched a sub
.exeprogram from the main without window for the sub program - wait for
rand Sleep - then terminate the sub application first then the main.
In the following my example code for the above but the sub program running with window(in this case NotePad) and i can’t terminate the sub program.
#include "stdafx.h"
#include <windows.h>
#include <conio.h>
#include <strsafe.h>
#include <direct.h>
#include <string.h>
int main(int argc, char* argv[])
{
HWND hWnd;
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
ZeroMemory(&sInfo, sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
ZeroMemory(&pInfo, sizeof(pInfo));
if (CreateProcess("C:\\WINDOWS\\System32\\notepad.exe", NULL, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &sInfo, &pInfo))
{
printf("Sleeping 100ms...\n");
Sleep(100);
DWORD dwExitCode;
GetExitCodeProcess(pInfo.hProcess, &dwExitCode);
CloseHandle(pInfo.hThread);
CloseHandle(pInfo.hProcess);
}
system("pause");
return 0;
}
The reason the notepad window shows is because it’s not a console application. MSDN says this about
CREATE_NO_WINDOW:Instead, use the
STARTUPINFOyou pass in:I believe that will affect the last argument to
WinMainin Notepad’s main function, but I’m not sure.As for why notepad doesn’t close,
GetExitCodeProcessdoesn’t actually end the process, it just retrieves the state. You can useTerminateProcessinstead: