OILS / win32 / create-process.c View on Github | oils.pub

52 lines, 33 significant
1#include <windows.h>
2#include <stdio.h>
3
4int main() {
5 STARTUPINFO si;
6 PROCESS_INFORMATION pi;
7 BOOL success;
8
9 // Initialize the STARTUPINFO structure
10 ZeroMemory(&si, sizeof(si));
11 si.cb = sizeof(si);
12 ZeroMemory(&pi, sizeof(pi));
13
14 // Command to execute with arguments
15 // Note: We use double quotes around the Python command since the inner command uses single quotes
16 char command[] = "python.exe -c \"print('hi from python 3'); import sys; sys.exit(42)\"";
17
18 // Create the process
19 success = CreateProcess(
20 NULL, // No module name (use command line)
21 command, // Command line with arguments
22 NULL, // Process handle not inheritable
23 NULL, // Thread handle not inheritable
24 FALSE, // Set handle inheritance to FALSE
25 0, // No creation flags
26 NULL, // Use parent's environment block
27 NULL, // Use parent's starting directory
28 &si, // Pointer to STARTUPINFO structure
29 &pi // Pointer to PROCESS_INFORMATION structure
30 );
31
32 // Check if process creation was successful
33 if (success) {
34 printf("Process started successfully!\n");
35
36 // Wait for the process to finish
37 WaitForSingleObject(pi.hProcess, INFINITE);
38
39 // Get the exit code
40 DWORD exitCode;
41 GetExitCodeProcess(pi.hProcess, &exitCode);
42 printf("Process exited with code: %lu\n", exitCode);
43
44 // Close process and thread handles
45 CloseHandle(pi.hProcess);
46 CloseHandle(pi.hThread);
47 } else {
48 printf("Failed to start process. Error code: %lu\n", GetLastError());
49 }
50
51 return 0;
52}