Skip to content

Instantly share code, notes, and snippets.

@pedrominicz
Last active March 15, 2020 01:15
Show Gist options
  • Save pedrominicz/1bc84c90d9beb630b2ed17db4e8828f0 to your computer and use it in GitHub Desktop.
Save pedrominicz/1bc84c90d9beb630b2ed17db4e8828f0 to your computer and use it in GitHub Desktop.
Create a process in Windows.
#include <windows.h>
int main(void) {
// `STARTUPINFO` structure specifies the main window properties of a new
// process, if one is created. I guess the name "Windows" is not for nothing,
// GUIs are very important for it.
STARTUPINFO si;
// `PROCESS_INFORMATION` structure specifies information about a new process
// and its primary thread. More in line with what one would except from an OS
// API.
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
// Specifies the size, in bytes, of the structure.
si.cb = sizeof(si);
// `CreateProcess` returns a boolean indicating success.
if(CreateProcess(
// Module to execute. Can be set to `NULL` in which case the system
// will get the module from the second argument.
NULL,
// Command line to execute.
"C:\\WINDOWS\\System32\\mspaint.exe",
// Security attributes. `NULL` means that the process handle is not
// inheritable by child processes.
//
// A handle, from what I understand, is similar to `pid_t` in the Unix
// world and "not inheritable" means that other processes created from
// this one will not be able to use the handle to this Microsoft Paint
// process.
NULL,
// Thread attributes. Similar to security attributes: `NULL` means the
// handle is not inheritable.
NULL,
// Inherit handles. Indicates whether the _new_ process, i.e.
// Microsoft Paint, can inherit the handles from the parent.
FALSE,
// Creation flags.
0,
// Environment block. If `NULL` inherit from the parent.
NULL,
// Working directory. If `NULL` inherit from the parent.
NULL,
&si,
&pi)) {
WaitForSingleObject(pi.hProcess, INFINITE);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment