-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotepadlaunch.cpp
More file actions
49 lines (40 loc) · 1.44 KB
/
notepadlaunch.cpp
File metadata and controls
49 lines (40 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <windows.h>
#include <iostream>
int main()
{
// Structure to receive process startup info
STARTUPINFOA si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
// Structure to receive process information
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
// Command line to execute
// Note: CreateProcessA requires a mutable buffer for the command line
char cmd[] = "notepad.exe";
// Create the process
BOOL success = CreateProcessA(
nullptr, // lpApplicationName: use command line only
cmd, // lpCommandLine: our mutable command string
nullptr, // lpProcessAttributes: default security
nullptr, // lpThreadAttributes: default security
FALSE, // bInheritHandles: no handle inheritance
0, // dwCreationFlags: default options
nullptr, // lpEnvironment: use parent environment
nullptr, // lpCurrentDirectory: use parent working dir
&si, // pointer to STARTUPINFO
&pi // pointer to PROCESS_INFORMATION
);
if (!success)
{
std::cerr << "CreateProcess failed (" << GetLastError() << ")\n";
return 1;
}
std::cout << "Launched Notepad (PID = " << pi.dwProcessId << ")\n";
// Optionally, wait for the process to exit
WaitForSingleObject(pi.hProcess, INFINITE);
// Clean up handles
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
return 0;
}