Advertisement
neongm

CreateProcess example/lab

Feb 25th, 2021
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.63 KB | None | 0 0
  1. #include <iostream>
  2. #include <windows.h>
  3.  
  4. int main()
  5. {
  6.     STARTUPINFO startupInfo = { sizeof(startupInfo) };  
  7.     PROCESS_INFORMATION processInfo;    // to save process information
  8.     TCHAR CmdLine[] = TEXT("notepad");  // command line
  9.  
  10.     bool creationResult = CreateProcess(
  11.         NULL,               // use command line (no module name)
  12.         CmdLine,            // command line
  13.         NULL,               // process handle not inheritable
  14.         NULL,               // thread hanfle not inheritable
  15.         FALSE,              // handle inheritance = 0
  16.         0,                  // without creation flags
  17.         NULL,               // usint parent's environment block
  18.         NULL,               // using parent's start directory
  19.         &startupInfo,       // pointer to structure STARTUPINFO
  20.         &processInfo        // pointer to structure POINTER_INFORMATION
  21.     );
  22.  
  23.     if (creationResult)     // outputing some info about the process
  24.     {  
  25.         std::cout << "process created\n PID\t" << processInfo.dwProcessId << "\n TID\t" << processInfo.dwThreadId << '\n';
  26.         system("pause");    // than just wait for input, than attempt to terminate the process
  27.         if (TerminateProcess(processInfo.hProcess, NO_ERROR)) std::cout << "process terminated\n";     // if process successfully terminated
  28.         else std::cout << "error: process termination failed\n";    // it can fail in some cases, for example - when process already terminated by user
  29.     }
  30.     else std::cout << "error: process creation failed\n";           // idk if it can happen but for this case outputing error:
  31. }
  32.  
  33.  
  34. // Vologin V.A. KSU 2021
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement