Skip to content

Commit 60d4ad8

Browse files
authored
Refactor: Isolate WinAPI declarations. (#122)
This commit replaces the direct inclusion of `<windows.h>` with a minimal set of manual forward declarations. This is a deliberate architectural change to: - **Improve Compilation Speed:** Avoids parsing the notoriously large Windows header. - **Eliminate Naming Pollution:** Prevents name clashes with common names like `min`/`max` which conflict with the C++ standard library. - **Enhance Encapsulation:** Makes the library more self-contained by not exposing the entire Windows API.
1 parent 777cfa7 commit 60d4ad8

File tree

1 file changed

+123
-2
lines changed

1 file changed

+123
-2
lines changed

cpp-subprocess/subprocess.hpp

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,129 @@ Documentation for C++ subprocessing library.
6060
#endif
6161

6262
extern "C" {
63-
#ifdef __USING_WINDOWS__
64-
#include <windows.h>
63+
64+
/*
65+
* == Windows API Declarations ==
66+
*
67+
* Manually declare only the Windows API functions we need instead of
68+
* including the full <windows.h> to:
69+
*
70+
* 1. Keep compile times fast.
71+
* 2. Prevent name collisions with common names like min/max.
72+
* 3. Make the library self-contained and clean.
73+
*/
74+
75+
#ifdef __USING_WINDOWS__
76+
77+
#define CONST const
78+
#define WINAPI __stdcall
79+
80+
#define TRUE 1
81+
#define FALSE 0
82+
83+
#define INFINITE 0xFFFFFFFF
84+
85+
#define STATUS_WAIT_0 ((unsigned long)0x00000000L)
86+
#define WAIT_OBJECT_0 ((STATUS_WAIT_0) + 0)
87+
88+
#define HANDLE_FLAG_INHERIT 0x00000001
89+
90+
#define STARTF_USESTDHANDLES 0x00000100
91+
92+
#define CREATE_NO_WINDOW 0x08000000
93+
#define CREATE_UNICODE_ENVIRONMENT 0x00000400
94+
95+
#define FORMAT_MESSAGE_ALLOCATE_BUFFER 0x00000100
96+
#define FORMAT_MESSAGE_FROM_SYSTEM 0x00001000
97+
#define FORMAT_MESSAGE_IGNORE_INSERTS 0x00000200
98+
#define FORMAT_MESSAGE_MAX_WIDTH_MASK 0x000000FF
99+
100+
#define LANG_NEUTRAL 0x00
101+
#define SUBLANG_DEFAULT 0x01
102+
103+
#ifdef _M_CEE_PURE
104+
typedef System::ArgIterator va_list;
105+
#else
106+
typedef char* va_list;
107+
#endif
108+
109+
typedef void VOID;
110+
typedef char CHAR;
111+
typedef wchar_t WCHAR;
112+
113+
typedef int BOOL;
114+
typedef unsigned int UINT;
115+
typedef unsigned short USHORT;
116+
typedef unsigned short WORD;
117+
typedef unsigned long DWORD;
118+
119+
typedef size_t SIZE_T;
120+
121+
typedef void* PVOID;
122+
typedef void* LPVOID;
123+
typedef const void* LPCVOID;
124+
125+
typedef void* HANDLE;
126+
typedef HANDLE HLOCAL;
127+
typedef HANDLE* PHANDLE;
128+
129+
typedef DWORD* LPDWORD;
130+
131+
typedef CHAR* LPSTR;
132+
typedef WCHAR* LPWCH;
133+
typedef WCHAR* LPWSTR;
134+
typedef CONST WCHAR* LPCWSTR;
135+
136+
typedef struct _SECURITY_ATTRIBUTES {
137+
DWORD nLength;
138+
LPVOID lpSecurityDescriptor;
139+
BOOL bInheritHandle;
140+
} SECURITY_ATTRIBUTES, *LPSECURITY_ATTRIBUTES;
141+
142+
typedef struct _PROCESS_INFORMATION {
143+
HANDLE hProcess;
144+
HANDLE hThread;
145+
DWORD dwProcessId;
146+
DWORD dwThreadId;
147+
} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
148+
149+
typedef struct _STARTUPINFOW {
150+
DWORD cb;
151+
LPWSTR lpReserved;
152+
LPWSTR lpDesktop;
153+
LPWSTR lpTitle;
154+
DWORD dwX;
155+
DWORD dwY;
156+
DWORD dwXSize;
157+
DWORD dwYSize;
158+
DWORD dwXCountChars;
159+
DWORD dwYCountChars;
160+
DWORD dwFillAttribute;
161+
DWORD dwFlags;
162+
WORD wShowWindow;
163+
WORD cbReserved2;
164+
unsigned char* lpReserved2;
165+
HANDLE hStdInput;
166+
HANDLE hStdOutput;
167+
HANDLE hStdError;
168+
} STARTUPINFOW, * LPSTARTUPINFOW;
169+
170+
BOOL WINAPI CloseHandle(HANDLE);
171+
BOOL WINAPI CreatePipe(PHANDLE, PHANDLE, LPSECURITY_ATTRIBUTES, DWORD);
172+
BOOL WINAPI CreateProcessW(LPCWSTR, LPWSTR, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOL, DWORD, LPVOID, LPCWSTR, LPSTARTUPINFOW, LPPROCESS_INFORMATION);
173+
BOOL WINAPI FreeEnvironmentStringsW(LPWCH);
174+
BOOL WINAPI GetExitCodeProcess(HANDLE, LPDWORD);
175+
BOOL WINAPI SetHandleInformation(HANDLE, DWORD, DWORD);
176+
BOOL WINAPI TerminateProcess(HANDLE, UINT);
177+
DWORD WINAPI FormatMessageA(DWORD, LPCVOID, DWORD, DWORD, LPSTR, DWORD, va_list*);
178+
DWORD WINAPI GetLastError(VOID);
179+
DWORD WINAPI WaitForSingleObject(HANDLE, DWORD);
180+
HLOCAL WINAPI LocalFree(HLOCAL);
181+
LPWSTR WINAPI GetEnvironmentStringsW(VOID);
182+
183+
#define ZeroMemory(Destination,Length) memset((Destination),0,(Length))
184+
#define MAKELANGID(p, s) ((((WORD)(s)) << 10) | (WORD)(p))
185+
65186
#include <io.h>
66187
#include <cwchar>
67188
#else

0 commit comments

Comments
 (0)