libUTL++
win32api.h
1 #pragma once
2 
4 
5 #if UTL_HOST_OS == UTL_OS_MINGW
6 
8 // win32 ///////////////////////////////////////////////////////////////////////////////////////////
10 
11 #define WINAPI __stdcall
12 #define CreateEvent CreateEventA
13 #define CreateMutex CreateMutexA
14 #define CreateProcess CreateProcessA
15 #define CreateSemaphore CreateSemaphoreA
16 #define GetEnvironmentVariable GetEnvironmentVariableA
17 #define SetEnvironmentVariable SetEnvironmentVariableA
18 
20 
21 #if UTL_HOST_WORDSIZE == 64
22 #define InterlockedIncrement _InterlockedIncrement
23 #define InterlockedDecrement _InterlockedDecrement
24 #define InterlockedCompareExchange _InterlockedCompareExchange
25 #else
26 #endif
27 
29 
30 #undef FALSE
31 #undef TRUE
32 #undef INFINITE
33 #undef MAKEWORD
34 #define FALSE 0
35 #define TRUE 1
36 #define INFINITE 0xffffffffU
37 #define MAKEWORD(a, b) ((WORD)(((BYTE)(a)) | (((WORD)((BYTE)(b))) << 8)))
38 
40 
41 typedef void* HANDLE;
42 typedef int BOOL;
43 typedef unsigned char BYTE;
44 typedef utl::ushort_t WORD;
45 typedef ulong_t DWORD;
46 typedef DWORD(WINAPI* LPTHREAD_START_ROUTINE)(void*);
47 
49 
50 #if UTL_HOST_WORDSIZE == 64
51 typedef unsigned __int64 UINT_PTR;
52 typedef unsigned __int64 ULONG_PTR;
53 #else
54 typedef unsigned int UINT_PTR;
55 typedef unsigned long ULONG_PTR;
56 #endif
57 
59 
60 typedef struct _FILETIME
61 {
62  DWORD dwLowDateTime;
63  DWORD dwHighDateTime;
64 } FILETIME;
65 
67 
68 typedef struct _CRITICAL_SECTION
69 {
70  void* DebugInfo;
71  long LockCount;
72  long RecursionCount;
73  HANDLE OwningThread;
74  HANDLE LockSemaphore;
75  DWORD SpinCount;
76 } CRITICAL_SECTION;
77 
79 
80 typedef struct _STARTUPINFO
81 {
82  DWORD cb;
83  void* lpReserved;
84  void* lpDesktop;
85  void* lpTitle;
86  DWORD dwX;
87  DWORD dwY;
88  DWORD dwXSize;
89  DWORD dwYSize;
90  DWORD dwXCountChars;
91  DWORD dwYCountChars;
92  DWORD dwFillAttribute;
93  DWORD dwFlags;
94  WORD wShowWindow;
95  WORD cbReserved2;
96  void* lpReserved2;
97  HANDLE hStdInput;
98  HANDLE hStdOutput;
99  HANDLE hStdError;
100 } STARTUPINFO;
101 
103 
104 typedef struct _PROCESS_INFORMATION
105 {
106  HANDLE hProcess;
107  HANDLE hThread;
108  DWORD dwProcessId;
109  DWORD dwThreadId;
110 } PROCESS_INFORMATION;
111 
113 
114 extern "C" {
115 int WINAPI CloseHandle(HANDLE);
116 HANDLE WINAPI CreateEventA(void*, BOOL, BOOL, void*);
117 HANDLE WINAPI CreateMutexA(void*, BOOL, void*);
118 BOOL WINAPI CreateProcessA(void*, // application name
119  void*, // command line
120  void*, // process attributes
121  void*, // thread attributes
122  BOOL, // inherit handles?
123  DWORD, // creation flags
124  void*, // environment
125  void*, // current directory
126  void*, // startup information
127  void*); // process information
128 HANDLE WINAPI CreateSemaphoreA(void*, long, long, void*);
129 HANDLE WINAPI CreateThread(void*, // security attributes
130  DWORD, // stack size
131  LPTHREAD_START_ROUTINE, // start address
132  void*, // parameter
133  DWORD, // creation flags
134  DWORD*); // thread id
135 void WINAPI DeleteCriticalSection(CRITICAL_SECTION*);
136 void WINAPI EnterCriticalSection(CRITICAL_SECTION*);
137 HANDLE WINAPI GetCurrentThread();
138 DWORD WINAPI GetCurrentThreadId();
139 DWORD WINAPI GetEnvironmentVariableA(void*, void*, DWORD);
140 BOOL WINAPI GetExitCodeProcess(HANDLE, DWORD*);
141 BOOL WINAPI GetExitCodeThread(HANDLE, DWORD*);
142 DWORD WINAPI GetLastError();
143 BOOL WINAPI GetProcessTimes(HANDLE, FILETIME*, FILETIME*, FILETIME*, FILETIME*);
144 void WINAPI InitializeCriticalSection(CRITICAL_SECTION*);
145 long WINAPI InterlockedIncrement(long volatile* val);
146 long WINAPI InterlockedDecrement(long volatile* val);
147 long WINAPI InterlockedCompareExchange(long volatile* dest, long exchange, long comparand);
148 void WINAPI LeaveCriticalSection(CRITICAL_SECTION*);
149 BOOL WINAPI ReleaseMutex(HANDLE);
150 HANDLE WINAPI ReleaseSemaphore(HANDLE, long, void*);
151 BOOL WINAPI ResetEvent(HANDLE);
152 BOOL WINAPI SetEnvironmentVariableA(void*, void*);
153 BOOL WINAPI SetEvent(HANDLE);
154 void WINAPI Sleep(DWORD);
155 DWORD WINAPI SignalObjectAndWait(HANDLE, HANDLE, DWORD, BOOL);
156 DWORD WINAPI TlsAlloc();
157 BOOL WINAPI TlsFree(DWORD);
158 void* WINAPI TlsGetValue(DWORD);
159 BOOL WINAPI TlsSetValue(DWORD, void*);
160 DWORD WINAPI WaitForSingleObject(HANDLE, DWORD);
161 }
162 
164 // winsock2 ////////////////////////////////////////////////////////////////////////////////////////
166 
167 typedef int socklen_t;
168 typedef UINT_PTR SOCKET;
169 
171 
172 #define h_errno WSAGetLastError()
173 #define s_addr S_un.S_addr
174 #define AF_UNSPEC 0
175 #define AF_UNIX 1
176 #define AF_INET 2
177 #define FD_SETSIZE 64U
178 #define INVALID_SOCKET (SOCKET)(~0)
179 #define SOCK_STREAM 1
180 #define SOCKET_ERROR (-1)
181 #define SO_REUSEADDR 4
182 #define SO_KEEPALIVE 8
183 #define SO_LINGER 128
184 #define SO_ERROR 0x1007
185 #define SOL_SOCKET 0xffff
186 #define TCP_NODELAY 0x0001
187 #define WSABASEERR 10000
188 #define WSADESCRIPTION_LEN 256
189 #define WSAEADDRINUSE (WSABASEERR + 48)
190 #define WSAEADDRNOTAVAIL (WSABASEERR + 49)
191 #define WSAENETUNREACH (WSABASEERR + 51)
192 #define WSAECONNRESET (WSABASEERR + 54)
193 #define WSAECONNREFUSED (WSABASEERR + 61)
194 #define WSAEHOSTUNREACH (WSABASEERR + 65)
195 #define WSAHOST_NOT_FOUND (WSABASEERR + 1001)
196 #define WSATRY_AGAIN (WSABASEERR + 1002)
197 #define WSANO_RECOVERY (WSABASEERR + 1003)
198 #define WSANO_DATA (WSABASEERR + 1004)
199 #define WSASYS_STATUS_LEN 128
200 
202 
203 struct fd_set
204 {
205  uint_t fd_count;
206  SOCKET fd_array[FD_SETSIZE];
207 };
208 
210 
211 struct hostent
212 {
213  char* h_name;
214  char** h_aliases;
215  short h_addrtype;
216  short h_length;
217  char** h_addr_list;
218 #define h_addr h_addr_list[0]
219 };
220 
222 
223 struct in_addr
224 {
225  union
226  {
227  struct
228  {
229  utl::byte_t s_b1, s_b2, s_b3, s_b4;
230  } S_un_b;
231  struct
232  {
233  utl::ushort_t s_w1, s_w2;
234  } S_un_w;
235  ulong_t S_addr;
236  } S_un;
237 };
238 
240 
241 struct sockaddr
242 {
243  utl::ushort_t sa_family;
244  char sa_data[14];
245 };
246 
248 
249 struct sockaddr_in
250 {
251  short sin_family;
252  utl::ushort_t sin_port;
253  struct in_addr sin_addr;
254  char sin_zero[8];
255 };
256 
258 
259 struct addrinfo
260 {
261  int ai_flags;
262  int ai_family;
263  int ai_socktype;
264  int ai_protocol;
265  size_t ai_addrlen;
266  char* ai_canonname;
267  struct sockaddr* ai_addr;
268  struct addrinfo* ai_next;
269 };
270 
272 
273 struct linger
274 {
275  WORD l_onoff;
276  WORD l_linger;
277 };
278 
280 
281 struct sockaddr_storage
282 {
283  short ss_family;
284  char __ss_pad1[6]; // pad to 8
285  int64_t __ss_align; // force alignment
286  char __ss_pad2[112]; // pad to 128
287 };
288 
290 
291 struct WSADATA
292 {
293  WORD wVersion;
294  WORD wHighVersion;
295  char szDescription[WSADESCRIPTION_LEN + 1];
296  char szSystemStatus[WSASYS_STATUS_LEN + 1];
297  utl::ushort_t iMaxSockets;
298  utl::ushort_t iMaxUdpDg;
299  char* lpVendorInfo;
300 };
301 
303 
304 #undef FD_CLR
305 #define FD_CLR(fd, set) \
306  do \
307  { \
308  uint_t __i; \
309  for (__i = 0; __i < ((fd_set*)(set))->fd_count; __i++) \
310  { \
311  if (((fd_set*)(set))->fd_array[__i] == ((SOCKET)fd)) \
312  { \
313  while (__i < ((fd_set*)(set))->fd_count - 1) \
314  { \
315  ((fd_set*)(set))->fd_array[__i] = ((fd_set*)(set))->fd_array[__i + 1]; \
316  __i++; \
317  } \
318  ((fd_set*)(set))->fd_count--; \
319  break; \
320  } \
321  } \
322  } while (0)
323 
325 
326 #undef FD_SET
327 #define FD_SET(fd, set) \
328  do \
329  { \
330  uint_t __i; \
331  for (__i = 0; __i < ((fd_set*)(set))->fd_count; __i++) \
332  { \
333  if (((fd_set*)(set))->fd_array[__i] == ((SOCKET)fd)) \
334  { \
335  break; \
336  } \
337  } \
338  if (__i == ((fd_set*)(set))->fd_count) \
339  { \
340  if (((fd_set*)(set))->fd_count < FD_SETSIZE) \
341  { \
342  ((fd_set*)(set))->fd_array[__i] = ((SOCKET)fd); \
343  ((fd_set*)(set))->fd_count++; \
344  } \
345  } \
346  } while (0)
347 
349 
350 #undef FD_ZERO
351 #define FD_ZERO(set) (((fd_set*)(set))->fd_count = 0)
352 
354 
355 #undef FD_ISSET
356 #define FD_ISSET(fd, set) __WSAFDIsSet((SOCKET)(fd), (fd_set*)(set))
357 
359 
360 extern "C" {
361 SOCKET WINAPI accept(SOCKET, struct sockaddr*, int*);
362 int WINAPI bind(SOCKET, const struct sockaddr*, int);
363 int WINAPI closesocket(SOCKET);
364 int WINAPI connect(SOCKET, const struct sockaddr*, int);
365 int WINAPI getaddrinfo(const char*, const char*, const struct addrinfo*, struct addrinfo**);
366 hostent* WINAPI gethostbyaddr(const char*, int, int);
367 hostent* WINAPI gethostbyname(const char*);
368 int WINAPI gethostname(char*, int);
369 int WINAPI getsockopt(SOCKET, int, int, char*, int*);
370 int WINAPI setsockopt(SOCKET, int, int, const char*, int);
371 WORD WINAPI htons(WORD);
372 WORD WINAPI ntohs(WORD);
373 int WINAPI listen(SOCKET, int);
374 int WINAPI recv(SOCKET, char*, int, int);
375 SOCKET WINAPI socket(int, int, int);
376 int WINAPI select(int, fd_set*, fd_set*, fd_set*, const struct timeval*);
377 int WINAPI send(SOCKET, const char*, int, int);
378 int WINAPI WSACleanup();
379 int WINAPI WSAGetLastError();
380 int WINAPI WSAStartup(WORD, WSADATA*);
381 int WINAPI __WSAFDIsSet(SOCKET, fd_set*);
382 }
383 
385 
386 #endif
unsigned char byte_t
Unsigned character.
Definition: types.h:31
long int64_t
Signed 64-bit integer.
Definition: types.h:164
unsigned int uint_t
Unsigned integer.
Definition: types.h:59
unsigned short ushort_t
Unsigned short integer.
Definition: types.h:45
unsigned long ulong_t
Unsigned long integer.
Definition: types.h:73