blob: d472c9ee53efefe295fa6eb9dfc55c274bc52f9d [file] [log] [blame]
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001/*
2 * Support routines from the Windows API
3 *
4 * This module was originally created by merging PC/_subprocess.c with
5 * Modules/_multiprocessing/win32_functions.c.
6 *
7 * Copyright (c) 2004 by Fredrik Lundh <fredrik@pythonware.com>
8 * Copyright (c) 2004 by Secret Labs AB, http://www.pythonware.com
9 * Copyright (c) 2004 by Peter Astrand <astrand@lysator.liu.se>
10 *
11 * By obtaining, using, and/or copying this software and/or its
12 * associated documentation, you agree that you have read, understood,
13 * and will comply with the following terms and conditions:
14 *
15 * Permission to use, copy, modify, and distribute this software and
16 * its associated documentation for any purpose and without fee is
17 * hereby granted, provided that the above copyright notice appears in
18 * all copies, and that both that copyright notice and this permission
19 * notice appear in supporting documentation, and that the name of the
20 * authors not be used in advertising or publicity pertaining to
21 * distribution of the software without specific, written prior
22 * permission.
23 *
24 * THE AUTHORS DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
25 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
26 * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
27 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
28 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
29 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
30 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
31 *
32 */
33
34/* Licensed to PSF under a Contributor Agreement. */
35/* See http://www.python.org/2.4/license for licensing details. */
36
37#include "Python.h"
38#include "structmember.h"
39
40#define WINDOWS_LEAN_AND_MEAN
41#include "windows.h"
42#include <crtdbg.h>
43
44#if defined(MS_WIN32) && !defined(MS_WIN64)
45#define HANDLE_TO_PYNUM(handle) \
46 PyLong_FromUnsignedLong((unsigned long) handle)
47#define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLong(obj))
48#define F_POINTER "k"
49#define T_POINTER T_ULONG
50#else
51#define HANDLE_TO_PYNUM(handle) \
52 PyLong_FromUnsignedLongLong((unsigned long long) handle)
53#define PYNUM_TO_HANDLE(obj) ((HANDLE)PyLong_AsUnsignedLongLong(obj))
54#define F_POINTER "K"
55#define T_POINTER T_ULONGLONG
56#endif
57
58#define F_HANDLE F_POINTER
59#define F_DWORD "k"
60#define F_BOOL "i"
61#define F_UINT "I"
62
63#define T_HANDLE T_POINTER
64
Victor Stinner71765772013-06-24 23:13:24 +020065#define DWORD_MAX 4294967295U
66
Antoine Pitrou23bba4c2012-04-18 20:51:15 +020067/* Grab CancelIoEx dynamically from kernel32 */
68static int has_CancelIoEx = -1;
69static BOOL (CALLBACK *Py_CancelIoEx)(HANDLE, LPOVERLAPPED);
70
71static int
72check_CancelIoEx()
73{
74 if (has_CancelIoEx == -1)
75 {
76 HINSTANCE hKernel32 = GetModuleHandle("KERNEL32");
77 * (FARPROC *) &Py_CancelIoEx = GetProcAddress(hKernel32,
78 "CancelIoEx");
79 has_CancelIoEx = (Py_CancelIoEx != NULL);
80 }
81 return has_CancelIoEx;
82}
83
84
85/*
86 * A Python object wrapping an OVERLAPPED structure and other useful data
87 * for overlapped I/O
88 */
89
90typedef struct {
91 PyObject_HEAD
92 OVERLAPPED overlapped;
93 /* For convenience, we store the file handle too */
94 HANDLE handle;
95 /* Whether there's I/O in flight */
96 int pending;
97 /* Whether I/O completed successfully */
98 int completed;
99 /* Buffer used for reading (optional) */
100 PyObject *read_buffer;
101 /* Buffer used for writing (optional) */
102 Py_buffer write_buffer;
103} OverlappedObject;
104
105static void
106overlapped_dealloc(OverlappedObject *self)
107{
108 DWORD bytes;
109 int err = GetLastError();
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000110
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200111 if (self->pending) {
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200112 if (check_CancelIoEx() &&
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000113 Py_CancelIoEx(self->handle, &self->overlapped) &&
114 GetOverlappedResult(self->handle, &self->overlapped, &bytes, TRUE))
115 {
116 /* The operation is no longer pending -- nothing to do. */
117 }
118 else if (_Py_Finalizing == NULL)
119 {
120 /* The operation is still pending -- give a warning. This
121 will probably only happen on Windows XP. */
122 PyErr_SetString(PyExc_RuntimeError,
123 "I/O operations still in flight while destroying "
124 "Overlapped object, the process may crash");
125 PyErr_WriteUnraisable(NULL);
126 }
127 else
128 {
129 /* The operation is still pending, but the process is
130 probably about to exit, so we need not worry too much
131 about memory leaks. Leaking self prevents a potential
132 crash. This can happen when a daemon thread is cleaned
133 up at exit -- see #19565. We only expect to get here
134 on Windows XP. */
135 CloseHandle(self->overlapped.hEvent);
136 SetLastError(err);
137 return;
138 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200139 }
Richard Oudkerk633db6f2013-11-17 13:15:51 +0000140
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200141 CloseHandle(self->overlapped.hEvent);
142 SetLastError(err);
143 if (self->write_buffer.obj)
144 PyBuffer_Release(&self->write_buffer);
145 Py_CLEAR(self->read_buffer);
146 PyObject_Del(self);
147}
148
149static PyObject *
150overlapped_GetOverlappedResult(OverlappedObject *self, PyObject *waitobj)
151{
152 int wait;
153 BOOL res;
154 DWORD transferred = 0;
155 DWORD err;
156
157 wait = PyObject_IsTrue(waitobj);
158 if (wait < 0)
159 return NULL;
160 Py_BEGIN_ALLOW_THREADS
161 res = GetOverlappedResult(self->handle, &self->overlapped, &transferred,
162 wait != 0);
163 Py_END_ALLOW_THREADS
164
165 err = res ? ERROR_SUCCESS : GetLastError();
166 switch (err) {
167 case ERROR_SUCCESS:
168 case ERROR_MORE_DATA:
169 case ERROR_OPERATION_ABORTED:
170 self->completed = 1;
171 self->pending = 0;
172 break;
173 case ERROR_IO_INCOMPLETE:
174 break;
175 default:
176 self->pending = 0;
177 return PyErr_SetExcFromWindowsErr(PyExc_IOError, err);
178 }
179 if (self->completed && self->read_buffer != NULL) {
180 assert(PyBytes_CheckExact(self->read_buffer));
181 if (transferred != PyBytes_GET_SIZE(self->read_buffer) &&
182 _PyBytes_Resize(&self->read_buffer, transferred))
183 return NULL;
184 }
185 return Py_BuildValue("II", (unsigned) transferred, (unsigned) err);
186}
187
188static PyObject *
189overlapped_getbuffer(OverlappedObject *self)
190{
191 PyObject *res;
192 if (!self->completed) {
193 PyErr_SetString(PyExc_ValueError,
194 "can't get read buffer before GetOverlappedResult() "
195 "signals the operation completed");
196 return NULL;
197 }
198 res = self->read_buffer ? self->read_buffer : Py_None;
199 Py_INCREF(res);
200 return res;
201}
202
203static PyObject *
204overlapped_cancel(OverlappedObject *self)
205{
206 BOOL res = TRUE;
207
208 if (self->pending) {
209 Py_BEGIN_ALLOW_THREADS
210 if (check_CancelIoEx())
211 res = Py_CancelIoEx(self->handle, &self->overlapped);
212 else
213 res = CancelIo(self->handle);
214 Py_END_ALLOW_THREADS
215 }
216
217 /* CancelIoEx returns ERROR_NOT_FOUND if the I/O completed in-between */
218 if (!res && GetLastError() != ERROR_NOT_FOUND)
219 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
220 self->pending = 0;
221 Py_RETURN_NONE;
222}
223
224static PyMethodDef overlapped_methods[] = {
225 {"GetOverlappedResult", (PyCFunction) overlapped_GetOverlappedResult,
226 METH_O, NULL},
227 {"getbuffer", (PyCFunction) overlapped_getbuffer, METH_NOARGS, NULL},
228 {"cancel", (PyCFunction) overlapped_cancel, METH_NOARGS, NULL},
229 {NULL}
230};
231
232static PyMemberDef overlapped_members[] = {
233 {"event", T_HANDLE,
234 offsetof(OverlappedObject, overlapped) + offsetof(OVERLAPPED, hEvent),
235 READONLY, "overlapped event handle"},
236 {NULL}
237};
238
239PyTypeObject OverlappedType = {
240 PyVarObject_HEAD_INIT(NULL, 0)
241 /* tp_name */ "_winapi.Overlapped",
242 /* tp_basicsize */ sizeof(OverlappedObject),
243 /* tp_itemsize */ 0,
244 /* tp_dealloc */ (destructor) overlapped_dealloc,
245 /* tp_print */ 0,
246 /* tp_getattr */ 0,
247 /* tp_setattr */ 0,
248 /* tp_reserved */ 0,
249 /* tp_repr */ 0,
250 /* tp_as_number */ 0,
251 /* tp_as_sequence */ 0,
252 /* tp_as_mapping */ 0,
253 /* tp_hash */ 0,
254 /* tp_call */ 0,
255 /* tp_str */ 0,
256 /* tp_getattro */ 0,
257 /* tp_setattro */ 0,
258 /* tp_as_buffer */ 0,
259 /* tp_flags */ Py_TPFLAGS_DEFAULT,
260 /* tp_doc */ "OVERLAPPED structure wrapper",
261 /* tp_traverse */ 0,
262 /* tp_clear */ 0,
263 /* tp_richcompare */ 0,
264 /* tp_weaklistoffset */ 0,
265 /* tp_iter */ 0,
266 /* tp_iternext */ 0,
267 /* tp_methods */ overlapped_methods,
268 /* tp_members */ overlapped_members,
269 /* tp_getset */ 0,
270 /* tp_base */ 0,
271 /* tp_dict */ 0,
272 /* tp_descr_get */ 0,
273 /* tp_descr_set */ 0,
274 /* tp_dictoffset */ 0,
275 /* tp_init */ 0,
276 /* tp_alloc */ 0,
277 /* tp_new */ 0,
278};
279
280static OverlappedObject *
281new_overlapped(HANDLE handle)
282{
283 OverlappedObject *self;
284
285 self = PyObject_New(OverlappedObject, &OverlappedType);
286 if (!self)
287 return NULL;
288 self->handle = handle;
289 self->read_buffer = NULL;
290 self->pending = 0;
291 self->completed = 0;
292 memset(&self->overlapped, 0, sizeof(OVERLAPPED));
293 memset(&self->write_buffer, 0, sizeof(Py_buffer));
294 /* Manual reset, initially non-signalled */
295 self->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
296 return self;
297}
298
299/* -------------------------------------------------------------------- */
300/* windows API functions */
301
302PyDoc_STRVAR(CloseHandle_doc,
303"CloseHandle(handle) -> None\n\
304\n\
305Close handle.");
306
307static PyObject *
308winapi_CloseHandle(PyObject *self, PyObject *args)
309{
310 HANDLE hObject;
311 BOOL success;
312
313 if (!PyArg_ParseTuple(args, F_HANDLE ":CloseHandle", &hObject))
314 return NULL;
315
316 Py_BEGIN_ALLOW_THREADS
317 success = CloseHandle(hObject);
318 Py_END_ALLOW_THREADS
319
320 if (!success)
321 return PyErr_SetFromWindowsErr(0);
322
323 Py_RETURN_NONE;
324}
325
326static PyObject *
327winapi_ConnectNamedPipe(PyObject *self, PyObject *args, PyObject *kwds)
328{
329 HANDLE hNamedPipe;
330 int use_overlapped = 0;
331 BOOL success;
332 OverlappedObject *overlapped = NULL;
333 static char *kwlist[] = {"handle", "overlapped", NULL};
334
335 if (!PyArg_ParseTupleAndKeywords(args, kwds,
336 F_HANDLE "|" F_BOOL, kwlist,
337 &hNamedPipe, &use_overlapped))
338 return NULL;
339
340 if (use_overlapped) {
341 overlapped = new_overlapped(hNamedPipe);
342 if (!overlapped)
343 return NULL;
344 }
345
346 Py_BEGIN_ALLOW_THREADS
347 success = ConnectNamedPipe(hNamedPipe,
348 overlapped ? &overlapped->overlapped : NULL);
349 Py_END_ALLOW_THREADS
350
351 if (overlapped) {
352 int err = GetLastError();
353 /* Overlapped ConnectNamedPipe never returns a success code */
354 assert(success == 0);
355 if (err == ERROR_IO_PENDING)
356 overlapped->pending = 1;
357 else if (err == ERROR_PIPE_CONNECTED)
358 SetEvent(overlapped->overlapped.hEvent);
359 else {
360 Py_DECREF(overlapped);
361 return PyErr_SetFromWindowsErr(err);
362 }
363 return (PyObject *) overlapped;
364 }
365 if (!success)
366 return PyErr_SetFromWindowsErr(0);
367
368 Py_RETURN_NONE;
369}
370
371static PyObject *
372winapi_CreateFile(PyObject *self, PyObject *args)
373{
374 LPCTSTR lpFileName;
375 DWORD dwDesiredAccess;
376 DWORD dwShareMode;
377 LPSECURITY_ATTRIBUTES lpSecurityAttributes;
378 DWORD dwCreationDisposition;
379 DWORD dwFlagsAndAttributes;
380 HANDLE hTemplateFile;
381 HANDLE handle;
382
383 if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_POINTER
384 F_DWORD F_DWORD F_HANDLE,
385 &lpFileName, &dwDesiredAccess, &dwShareMode,
386 &lpSecurityAttributes, &dwCreationDisposition,
387 &dwFlagsAndAttributes, &hTemplateFile))
388 return NULL;
389
390 Py_BEGIN_ALLOW_THREADS
391 handle = CreateFile(lpFileName, dwDesiredAccess,
392 dwShareMode, lpSecurityAttributes,
393 dwCreationDisposition,
394 dwFlagsAndAttributes, hTemplateFile);
395 Py_END_ALLOW_THREADS
396
397 if (handle == INVALID_HANDLE_VALUE)
398 return PyErr_SetFromWindowsErr(0);
399
400 return Py_BuildValue(F_HANDLE, handle);
401}
402
403static PyObject *
404winapi_CreateNamedPipe(PyObject *self, PyObject *args)
405{
406 LPCTSTR lpName;
407 DWORD dwOpenMode;
408 DWORD dwPipeMode;
409 DWORD nMaxInstances;
410 DWORD nOutBufferSize;
411 DWORD nInBufferSize;
412 DWORD nDefaultTimeOut;
413 LPSECURITY_ATTRIBUTES lpSecurityAttributes;
414 HANDLE handle;
415
416 if (!PyArg_ParseTuple(args, "s" F_DWORD F_DWORD F_DWORD
417 F_DWORD F_DWORD F_DWORD F_POINTER,
418 &lpName, &dwOpenMode, &dwPipeMode,
419 &nMaxInstances, &nOutBufferSize,
420 &nInBufferSize, &nDefaultTimeOut,
421 &lpSecurityAttributes))
422 return NULL;
423
424 Py_BEGIN_ALLOW_THREADS
425 handle = CreateNamedPipe(lpName, dwOpenMode, dwPipeMode,
426 nMaxInstances, nOutBufferSize,
427 nInBufferSize, nDefaultTimeOut,
428 lpSecurityAttributes);
429 Py_END_ALLOW_THREADS
430
431 if (handle == INVALID_HANDLE_VALUE)
432 return PyErr_SetFromWindowsErr(0);
433
434 return Py_BuildValue(F_HANDLE, handle);
435}
436
437PyDoc_STRVAR(CreatePipe_doc,
438"CreatePipe(pipe_attrs, size) -> (read_handle, write_handle)\n\
439\n\
440Create an anonymous pipe, and return handles to the read and\n\
441write ends of the pipe.\n\
442\n\
443pipe_attrs is ignored internally and can be None.");
444
445static PyObject *
446winapi_CreatePipe(PyObject* self, PyObject* args)
447{
448 HANDLE read_pipe;
449 HANDLE write_pipe;
450 BOOL result;
451
452 PyObject* pipe_attributes; /* ignored */
453 DWORD size;
454
455 if (! PyArg_ParseTuple(args, "O" F_DWORD ":CreatePipe",
456 &pipe_attributes, &size))
457 return NULL;
458
459 Py_BEGIN_ALLOW_THREADS
460 result = CreatePipe(&read_pipe, &write_pipe, NULL, size);
461 Py_END_ALLOW_THREADS
462
463 if (! result)
464 return PyErr_SetFromWindowsErr(GetLastError());
465
466 return Py_BuildValue(
467 "NN", HANDLE_TO_PYNUM(read_pipe), HANDLE_TO_PYNUM(write_pipe));
468}
469
470/* helpers for createprocess */
471
472static unsigned long
473getulong(PyObject* obj, char* name)
474{
475 PyObject* value;
476 unsigned long ret;
477
478 value = PyObject_GetAttrString(obj, name);
479 if (! value) {
480 PyErr_Clear(); /* FIXME: propagate error? */
481 return 0;
482 }
483 ret = PyLong_AsUnsignedLong(value);
484 Py_DECREF(value);
485 return ret;
486}
487
488static HANDLE
489gethandle(PyObject* obj, char* name)
490{
491 PyObject* value;
492 HANDLE ret;
493
494 value = PyObject_GetAttrString(obj, name);
495 if (! value) {
496 PyErr_Clear(); /* FIXME: propagate error? */
497 return NULL;
498 }
499 if (value == Py_None)
500 ret = NULL;
501 else
502 ret = PYNUM_TO_HANDLE(value);
503 Py_DECREF(value);
504 return ret;
505}
506
507static PyObject*
508getenvironment(PyObject* environment)
509{
510 Py_ssize_t i, envsize, totalsize;
511 Py_UCS4 *buffer = NULL, *p, *end;
512 PyObject *keys, *values, *res;
513
Ezio Melotti85a86292013-08-17 16:57:41 +0300514 /* convert environment dictionary to windows environment string */
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200515 if (! PyMapping_Check(environment)) {
516 PyErr_SetString(
517 PyExc_TypeError, "environment must be dictionary or None");
518 return NULL;
519 }
520
521 envsize = PyMapping_Length(environment);
522
523 keys = PyMapping_Keys(environment);
524 values = PyMapping_Values(environment);
525 if (!keys || !values)
526 goto error;
527
528 totalsize = 1; /* trailing null character */
529 for (i = 0; i < envsize; i++) {
530 PyObject* key = PyList_GET_ITEM(keys, i);
531 PyObject* value = PyList_GET_ITEM(values, i);
532
533 if (! PyUnicode_Check(key) || ! PyUnicode_Check(value)) {
534 PyErr_SetString(PyExc_TypeError,
535 "environment can only contain strings");
536 goto error;
537 }
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500538 if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(key) - 1) {
539 PyErr_SetString(PyExc_OverflowError, "environment too long");
540 goto error;
541 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200542 totalsize += PyUnicode_GET_LENGTH(key) + 1; /* +1 for '=' */
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500543 if (totalsize > PY_SSIZE_T_MAX - PyUnicode_GET_LENGTH(value) - 1) {
544 PyErr_SetString(PyExc_OverflowError, "environment too long");
545 goto error;
546 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200547 totalsize += PyUnicode_GET_LENGTH(value) + 1; /* +1 for '\0' */
548 }
549
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500550 buffer = PyMem_NEW(Py_UCS4, totalsize);
551 if (! buffer) {
552 PyErr_NoMemory();
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200553 goto error;
Benjamin Peterson8ce68062015-02-09 20:58:12 -0500554 }
Antoine Pitrou23bba4c2012-04-18 20:51:15 +0200555 p = buffer;
556 end = buffer + totalsize;
557
558 for (i = 0; i < envsize; i++) {
559 PyObject* key = PyList_GET_ITEM(keys, i);
560 PyObject* value = PyList_GET_ITEM(values, i);
561 if (!PyUnicode_AsUCS4(key, p, end - p, 0))
562 goto error;
563 p += PyUnicode_GET_LENGTH(key);
564 *p++ = '=';
565 if (!PyUnicode_AsUCS4(value, p, end - p, 0))
566 goto error;
567 p += PyUnicode_GET_LENGTH(value);
568 *p++ = '\0';
569 }
570
571 /* add trailing null byte */
572 *p++ = '\0';
573 assert(p == end);
574
575 Py_XDECREF(keys);
576 Py_XDECREF(values);
577
578 res = PyUnicode_FromKindAndData(PyUnicode_4BYTE_KIND, buffer, p - buffer);
579 PyMem_Free(buffer);
580 return res;
581
582 error:
583 PyMem_Free(buffer);
584 Py_XDECREF(keys);
585 Py_XDECREF(values);
586 return NULL;
587}
588
589PyDoc_STRVAR(CreateProcess_doc,
590"CreateProcess(app_name, cmd_line, proc_attrs, thread_attrs,\n\
591 inherit, flags, env_mapping, curdir,\n\
592 startup_info) -> (proc_handle, thread_handle,\n\
593 pid, tid)\n\
594\n\
595Create a new process and its primary thread. The return\n\
596value is a tuple of the process handle, thread handle,\n\
597process ID, and thread ID.\n\
598\n\
599proc_attrs and thread_attrs are ignored internally and can be None.");
600
601static PyObject *
602winapi_CreateProcess(PyObject* self, PyObject* args)
603{
604 BOOL result;
605 PROCESS_INFORMATION pi;
606 STARTUPINFOW si;
607 PyObject* environment;
608 wchar_t *wenvironment;
609
610 wchar_t* application_name;
611 wchar_t* command_line;
612 PyObject* process_attributes; /* ignored */
613 PyObject* thread_attributes; /* ignored */
614 BOOL inherit_handles;
615 DWORD creation_flags;
616 PyObject* env_mapping;
617 wchar_t* current_directory;
618 PyObject* startup_info;
619
620 if (! PyArg_ParseTuple(args, "ZZOO" F_BOOL F_DWORD "OZO:CreateProcess",
621 &application_name,
622 &command_line,
623 &process_attributes,
624 &thread_attributes,
625 &inherit_handles,
626 &creation_flags,
627 &env_mapping,
628 &current_directory,
629 &startup_info))
630 return NULL;
631
632 ZeroMemory(&si, sizeof(si));
633 si.cb = sizeof(si);
634
635 /* note: we only support a small subset of all SI attributes */
636 si.dwFlags = getulong(startup_info, "dwFlags");
637 si.wShowWindow = (WORD)getulong(startup_info, "wShowWindow");
638 si.hStdInput = gethandle(startup_info, "hStdInput");
639 si.hStdOutput = gethandle(startup_info, "hStdOutput");
640 si.hStdError = gethandle(startup_info, "hStdError");
641 if (PyErr_Occurred())
642 return NULL;
643
644 if (env_mapping != Py_None) {
645 environment = getenvironment(env_mapping);
646 if (! environment)
647 return NULL;
648 wenvironment = PyUnicode_AsUnicode(environment);
649 if (wenvironment == NULL)
650 {
651 Py_XDECREF(environment);
652 return NULL;
653 }
654 }
655 else {
656 environment = NULL;
657 wenvironment = NULL;
658 }
659
660 Py_BEGIN_ALLOW_THREADS
661 result = CreateProcessW(application_name,
662 command_line,
663 NULL,
664 NULL,
665 inherit_handles,
666 creation_flags | CREATE_UNICODE_ENVIRONMENT,
667 wenvironment,
668 current_directory,
669 &si,
670 &pi);
671 Py_END_ALLOW_THREADS
672
673 Py_XDECREF(environment);
674
675 if (! result)
676 return PyErr_SetFromWindowsErr(GetLastError());
677
678 return Py_BuildValue("NNkk",
679 HANDLE_TO_PYNUM(pi.hProcess),
680 HANDLE_TO_PYNUM(pi.hThread),
681 pi.dwProcessId,
682 pi.dwThreadId);
683}
684
685PyDoc_STRVAR(DuplicateHandle_doc,
686"DuplicateHandle(source_proc_handle, source_handle,\n\
687 target_proc_handle, target_handle, access,\n\
688 inherit[, options]) -> handle\n\
689\n\
690Return a duplicate handle object.\n\
691\n\
692The duplicate handle refers to the same object as the original\n\
693handle. Therefore, any changes to the object are reflected\n\
694through both handles.");
695
696static PyObject *
697winapi_DuplicateHandle(PyObject* self, PyObject* args)
698{
699 HANDLE target_handle;
700 BOOL result;
701
702 HANDLE source_process_handle;
703 HANDLE source_handle;
704 HANDLE target_process_handle;
705 DWORD desired_access;
706 BOOL inherit_handle;
707 DWORD options = 0;
708
709 if (! PyArg_ParseTuple(args,
710 F_HANDLE F_HANDLE F_HANDLE F_DWORD F_BOOL F_DWORD
711 ":DuplicateHandle",
712 &source_process_handle,
713 &source_handle,
714 &target_process_handle,
715 &desired_access,
716 &inherit_handle,
717 &options))
718 return NULL;
719
720 Py_BEGIN_ALLOW_THREADS
721 result = DuplicateHandle(
722 source_process_handle,
723 source_handle,
724 target_process_handle,
725 &target_handle,
726 desired_access,
727 inherit_handle,
728 options
729 );
730 Py_END_ALLOW_THREADS
731
732 if (! result)
733 return PyErr_SetFromWindowsErr(GetLastError());
734
735 return HANDLE_TO_PYNUM(target_handle);
736}
737
738static PyObject *
739winapi_ExitProcess(PyObject *self, PyObject *args)
740{
741 UINT uExitCode;
742
743 if (!PyArg_ParseTuple(args, F_UINT, &uExitCode))
744 return NULL;
745
746 #if defined(Py_DEBUG)
747 SetErrorMode(SEM_FAILCRITICALERRORS|SEM_NOALIGNMENTFAULTEXCEPT|
748 SEM_NOGPFAULTERRORBOX|SEM_NOOPENFILEERRORBOX);
749 _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
750 #endif
751
752 ExitProcess(uExitCode);
753
754 return NULL;
755}
756
757PyDoc_STRVAR(GetCurrentProcess_doc,
758"GetCurrentProcess() -> handle\n\
759\n\
760Return a handle object for the current process.");
761
762static PyObject *
763winapi_GetCurrentProcess(PyObject* self, PyObject* args)
764{
765 if (! PyArg_ParseTuple(args, ":GetCurrentProcess"))
766 return NULL;
767
768 return HANDLE_TO_PYNUM(GetCurrentProcess());
769}
770
771PyDoc_STRVAR(GetExitCodeProcess_doc,
772"GetExitCodeProcess(handle) -> Exit code\n\
773\n\
774Return the termination status of the specified process.");
775
776static PyObject *
777winapi_GetExitCodeProcess(PyObject* self, PyObject* args)
778{
779 DWORD exit_code;
780 BOOL result;
781
782 HANDLE process;
783 if (! PyArg_ParseTuple(args, F_HANDLE ":GetExitCodeProcess", &process))
784 return NULL;
785
786 result = GetExitCodeProcess(process, &exit_code);
787
788 if (! result)
789 return PyErr_SetFromWindowsErr(GetLastError());
790
791 return PyLong_FromUnsignedLong(exit_code);
792}
793
794static PyObject *
795winapi_GetLastError(PyObject *self, PyObject *args)
796{
797 return Py_BuildValue(F_DWORD, GetLastError());
798}
799
800PyDoc_STRVAR(GetModuleFileName_doc,
801"GetModuleFileName(module) -> path\n\
802\n\
803Return the fully-qualified path for the file that contains\n\
804the specified module. The module must have been loaded by the\n\
805current process.\n\
806\n\
807The module parameter should be a handle to the loaded module\n\
808whose path is being requested. If this parameter is 0, \n\
809GetModuleFileName retrieves the path of the executable file\n\
810of the current process.");
811
812static PyObject *
813winapi_GetModuleFileName(PyObject* self, PyObject* args)
814{
815 BOOL result;
816 HMODULE module;
817 WCHAR filename[MAX_PATH];
818
819 if (! PyArg_ParseTuple(args, F_HANDLE ":GetModuleFileName",
820 &module))
821 return NULL;
822
823 result = GetModuleFileNameW(module, filename, MAX_PATH);
824 filename[MAX_PATH-1] = '\0';
825
826 if (! result)
827 return PyErr_SetFromWindowsErr(GetLastError());
828
829 return PyUnicode_FromWideChar(filename, wcslen(filename));
830}
831
832PyDoc_STRVAR(GetStdHandle_doc,
833"GetStdHandle(handle) -> integer\n\
834\n\
835Return a handle to the specified standard device\n\
836(STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE).\n\
837The integer associated with the handle object is returned.");
838
839static PyObject *
840winapi_GetStdHandle(PyObject* self, PyObject* args)
841{
842 HANDLE handle;
843 DWORD std_handle;
844
845 if (! PyArg_ParseTuple(args, F_DWORD ":GetStdHandle", &std_handle))
846 return NULL;
847
848 Py_BEGIN_ALLOW_THREADS
849 handle = GetStdHandle(std_handle);
850 Py_END_ALLOW_THREADS
851
852 if (handle == INVALID_HANDLE_VALUE)
853 return PyErr_SetFromWindowsErr(GetLastError());
854
855 if (! handle) {
856 Py_INCREF(Py_None);
857 return Py_None;
858 }
859
860 /* note: returns integer, not handle object */
861 return HANDLE_TO_PYNUM(handle);
862}
863
864PyDoc_STRVAR(GetVersion_doc,
865"GetVersion() -> version\n\
866\n\
867Return the version number of the current operating system.");
868
869static PyObject *
870winapi_GetVersion(PyObject* self, PyObject* args)
871{
872 if (! PyArg_ParseTuple(args, ":GetVersion"))
873 return NULL;
874
875 return PyLong_FromUnsignedLong(GetVersion());
876}
877
878static PyObject *
879winapi_OpenProcess(PyObject *self, PyObject *args)
880{
881 DWORD dwDesiredAccess;
882 BOOL bInheritHandle;
883 DWORD dwProcessId;
884 HANDLE handle;
885
886 if (!PyArg_ParseTuple(args, F_DWORD F_BOOL F_DWORD,
887 &dwDesiredAccess, &bInheritHandle, &dwProcessId))
888 return NULL;
889
890 handle = OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId);
891 if (handle == NULL)
892 return PyErr_SetFromWindowsErr(0);
893
894 return Py_BuildValue(F_HANDLE, handle);
895}
896
897static PyObject *
898winapi_PeekNamedPipe(PyObject *self, PyObject *args)
899{
900 HANDLE handle;
901 int size = 0;
902 PyObject *buf = NULL;
903 DWORD nread, navail, nleft;
904 BOOL ret;
905
906 if (!PyArg_ParseTuple(args, F_HANDLE "|i:PeekNamedPipe" , &handle, &size))
907 return NULL;
908
909 if (size < 0) {
910 PyErr_SetString(PyExc_ValueError, "negative size");
911 return NULL;
912 }
913
914 if (size) {
915 buf = PyBytes_FromStringAndSize(NULL, size);
916 if (!buf)
917 return NULL;
918 Py_BEGIN_ALLOW_THREADS
919 ret = PeekNamedPipe(handle, PyBytes_AS_STRING(buf), size, &nread,
920 &navail, &nleft);
921 Py_END_ALLOW_THREADS
922 if (!ret) {
923 Py_DECREF(buf);
924 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
925 }
926 if (_PyBytes_Resize(&buf, nread))
927 return NULL;
928 return Py_BuildValue("Nii", buf, navail, nleft);
929 }
930 else {
931 Py_BEGIN_ALLOW_THREADS
932 ret = PeekNamedPipe(handle, NULL, 0, NULL, &navail, &nleft);
933 Py_END_ALLOW_THREADS
934 if (!ret) {
935 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
936 }
937 return Py_BuildValue("ii", navail, nleft);
938 }
939}
940
941static PyObject *
942winapi_ReadFile(PyObject *self, PyObject *args, PyObject *kwds)
943{
944 HANDLE handle;
945 int size;
946 DWORD nread;
947 PyObject *buf;
948 BOOL ret;
949 int use_overlapped = 0;
950 DWORD err;
951 OverlappedObject *overlapped = NULL;
952 static char *kwlist[] = {"handle", "size", "overlapped", NULL};
953
954 if (!PyArg_ParseTupleAndKeywords(args, kwds,
955 F_HANDLE "i|i:ReadFile", kwlist,
956 &handle, &size, &use_overlapped))
957 return NULL;
958
959 buf = PyBytes_FromStringAndSize(NULL, size);
960 if (!buf)
961 return NULL;
962 if (use_overlapped) {
963 overlapped = new_overlapped(handle);
964 if (!overlapped) {
965 Py_DECREF(buf);
966 return NULL;
967 }
968 /* Steals reference to buf */
969 overlapped->read_buffer = buf;
970 }
971
972 Py_BEGIN_ALLOW_THREADS
973 ret = ReadFile(handle, PyBytes_AS_STRING(buf), size, &nread,
974 overlapped ? &overlapped->overlapped : NULL);
975 Py_END_ALLOW_THREADS
976
977 err = ret ? 0 : GetLastError();
978
979 if (overlapped) {
980 if (!ret) {
981 if (err == ERROR_IO_PENDING)
982 overlapped->pending = 1;
983 else if (err != ERROR_MORE_DATA) {
984 Py_DECREF(overlapped);
985 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
986 }
987 }
988 return Py_BuildValue("NI", (PyObject *) overlapped, err);
989 }
990
991 if (!ret && err != ERROR_MORE_DATA) {
992 Py_DECREF(buf);
993 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
994 }
995 if (_PyBytes_Resize(&buf, nread))
996 return NULL;
997 return Py_BuildValue("NI", buf, err);
998}
999
1000static PyObject *
1001winapi_SetNamedPipeHandleState(PyObject *self, PyObject *args)
1002{
1003 HANDLE hNamedPipe;
1004 PyObject *oArgs[3];
1005 DWORD dwArgs[3], *pArgs[3] = {NULL, NULL, NULL};
1006 int i;
1007
1008 if (!PyArg_ParseTuple(args, F_HANDLE "OOO",
1009 &hNamedPipe, &oArgs[0], &oArgs[1], &oArgs[2]))
1010 return NULL;
1011
1012 PyErr_Clear();
1013
1014 for (i = 0 ; i < 3 ; i++) {
1015 if (oArgs[i] != Py_None) {
1016 dwArgs[i] = PyLong_AsUnsignedLongMask(oArgs[i]);
1017 if (PyErr_Occurred())
1018 return NULL;
1019 pArgs[i] = &dwArgs[i];
1020 }
1021 }
1022
1023 if (!SetNamedPipeHandleState(hNamedPipe, pArgs[0], pArgs[1], pArgs[2]))
1024 return PyErr_SetFromWindowsErr(0);
1025
1026 Py_RETURN_NONE;
1027}
1028
1029PyDoc_STRVAR(TerminateProcess_doc,
1030"TerminateProcess(handle, exit_code) -> None\n\
1031\n\
1032Terminate the specified process and all of its threads.");
1033
1034static PyObject *
1035winapi_TerminateProcess(PyObject* self, PyObject* args)
1036{
1037 BOOL result;
1038
1039 HANDLE process;
1040 UINT exit_code;
1041 if (! PyArg_ParseTuple(args, F_HANDLE F_UINT ":TerminateProcess",
1042 &process, &exit_code))
1043 return NULL;
1044
1045 result = TerminateProcess(process, exit_code);
1046
1047 if (! result)
1048 return PyErr_SetFromWindowsErr(GetLastError());
1049
1050 Py_INCREF(Py_None);
1051 return Py_None;
1052}
1053
1054static PyObject *
1055winapi_WaitNamedPipe(PyObject *self, PyObject *args)
1056{
1057 LPCTSTR lpNamedPipeName;
1058 DWORD nTimeOut;
1059 BOOL success;
1060
1061 if (!PyArg_ParseTuple(args, "s" F_DWORD, &lpNamedPipeName, &nTimeOut))
1062 return NULL;
1063
1064 Py_BEGIN_ALLOW_THREADS
1065 success = WaitNamedPipe(lpNamedPipeName, nTimeOut);
1066 Py_END_ALLOW_THREADS
1067
1068 if (!success)
1069 return PyErr_SetFromWindowsErr(0);
1070
1071 Py_RETURN_NONE;
1072}
1073
1074static PyObject *
1075winapi_WaitForMultipleObjects(PyObject* self, PyObject* args)
1076{
1077 DWORD result;
1078 PyObject *handle_seq;
1079 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
1080 HANDLE sigint_event = NULL;
1081 Py_ssize_t nhandles, i;
1082 BOOL wait_flag;
1083 DWORD milliseconds = INFINITE;
1084
1085 if (!PyArg_ParseTuple(args, "O" F_BOOL "|" F_DWORD
1086 ":WaitForMultipleObjects",
1087 &handle_seq, &wait_flag, &milliseconds))
1088 return NULL;
1089
1090 if (!PySequence_Check(handle_seq)) {
1091 PyErr_Format(PyExc_TypeError,
1092 "sequence type expected, got '%s'",
Richard Oudkerk67339272012-08-21 14:54:22 +01001093 Py_TYPE(handle_seq)->tp_name);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001094 return NULL;
1095 }
1096 nhandles = PySequence_Length(handle_seq);
1097 if (nhandles == -1)
1098 return NULL;
1099 if (nhandles < 0 || nhandles >= MAXIMUM_WAIT_OBJECTS - 1) {
1100 PyErr_Format(PyExc_ValueError,
1101 "need at most %zd handles, got a sequence of length %zd",
1102 MAXIMUM_WAIT_OBJECTS - 1, nhandles);
1103 return NULL;
1104 }
1105 for (i = 0; i < nhandles; i++) {
1106 HANDLE h;
1107 PyObject *v = PySequence_GetItem(handle_seq, i);
1108 if (v == NULL)
1109 return NULL;
1110 if (!PyArg_Parse(v, F_HANDLE, &h)) {
1111 Py_DECREF(v);
1112 return NULL;
1113 }
1114 handles[i] = h;
1115 Py_DECREF(v);
1116 }
1117 /* If this is the main thread then make the wait interruptible
1118 by Ctrl-C unless we are waiting for *all* handles */
1119 if (!wait_flag && _PyOS_IsMainThread()) {
1120 sigint_event = _PyOS_SigintEvent();
1121 assert(sigint_event != NULL);
1122 handles[nhandles++] = sigint_event;
1123 }
1124
1125 Py_BEGIN_ALLOW_THREADS
1126 if (sigint_event != NULL)
1127 ResetEvent(sigint_event);
1128 result = WaitForMultipleObjects((DWORD) nhandles, handles,
1129 wait_flag, milliseconds);
1130 Py_END_ALLOW_THREADS
1131
1132 if (result == WAIT_FAILED)
1133 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
1134 else if (sigint_event != NULL && result == WAIT_OBJECT_0 + nhandles - 1) {
1135 errno = EINTR;
1136 return PyErr_SetFromErrno(PyExc_IOError);
1137 }
1138
1139 return PyLong_FromLong((int) result);
1140}
1141
1142PyDoc_STRVAR(WaitForSingleObject_doc,
1143"WaitForSingleObject(handle, timeout) -> result\n\
1144\n\
1145Wait until the specified object is in the signaled state or\n\
1146the time-out interval elapses. The timeout value is specified\n\
1147in milliseconds.");
1148
1149static PyObject *
1150winapi_WaitForSingleObject(PyObject* self, PyObject* args)
1151{
1152 DWORD result;
1153
1154 HANDLE handle;
1155 DWORD milliseconds;
1156 if (! PyArg_ParseTuple(args, F_HANDLE F_DWORD ":WaitForSingleObject",
1157 &handle,
1158 &milliseconds))
1159 return NULL;
1160
1161 Py_BEGIN_ALLOW_THREADS
1162 result = WaitForSingleObject(handle, milliseconds);
1163 Py_END_ALLOW_THREADS
1164
1165 if (result == WAIT_FAILED)
1166 return PyErr_SetFromWindowsErr(GetLastError());
1167
1168 return PyLong_FromUnsignedLong(result);
1169}
1170
1171static PyObject *
1172winapi_WriteFile(PyObject *self, PyObject *args, PyObject *kwds)
1173{
1174 HANDLE handle;
1175 Py_buffer _buf, *buf;
1176 PyObject *bufobj;
Victor Stinner71765772013-06-24 23:13:24 +02001177 DWORD len, written;
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001178 BOOL ret;
1179 int use_overlapped = 0;
1180 DWORD err;
1181 OverlappedObject *overlapped = NULL;
1182 static char *kwlist[] = {"handle", "buffer", "overlapped", NULL};
1183
1184 /* First get handle and use_overlapped to know which Py_buffer to use */
1185 if (!PyArg_ParseTupleAndKeywords(args, kwds,
1186 F_HANDLE "O|i:WriteFile", kwlist,
1187 &handle, &bufobj, &use_overlapped))
1188 return NULL;
1189
1190 if (use_overlapped) {
1191 overlapped = new_overlapped(handle);
1192 if (!overlapped)
1193 return NULL;
1194 buf = &overlapped->write_buffer;
1195 }
1196 else
1197 buf = &_buf;
1198
1199 if (!PyArg_Parse(bufobj, "y*", buf)) {
1200 Py_XDECREF(overlapped);
1201 return NULL;
1202 }
1203
1204 Py_BEGIN_ALLOW_THREADS
Victor Stinner71765772013-06-24 23:13:24 +02001205 len = (DWORD)Py_MIN(buf->len, DWORD_MAX);
1206 ret = WriteFile(handle, buf->buf, len, &written,
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001207 overlapped ? &overlapped->overlapped : NULL);
1208 Py_END_ALLOW_THREADS
1209
1210 err = ret ? 0 : GetLastError();
1211
1212 if (overlapped) {
1213 if (!ret) {
1214 if (err == ERROR_IO_PENDING)
1215 overlapped->pending = 1;
1216 else {
1217 Py_DECREF(overlapped);
1218 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
1219 }
1220 }
1221 return Py_BuildValue("NI", (PyObject *) overlapped, err);
1222 }
1223
1224 PyBuffer_Release(buf);
1225 if (!ret)
1226 return PyErr_SetExcFromWindowsErr(PyExc_IOError, 0);
1227 return Py_BuildValue("II", written, err);
1228}
1229
1230
1231static PyMethodDef winapi_functions[] = {
1232 {"CloseHandle", winapi_CloseHandle, METH_VARARGS,
1233 CloseHandle_doc},
1234 {"ConnectNamedPipe", (PyCFunction)winapi_ConnectNamedPipe,
1235 METH_VARARGS | METH_KEYWORDS, ""},
1236 {"CreateFile", winapi_CreateFile, METH_VARARGS,
1237 ""},
1238 {"CreateNamedPipe", winapi_CreateNamedPipe, METH_VARARGS,
1239 ""},
1240 {"CreatePipe", winapi_CreatePipe, METH_VARARGS,
1241 CreatePipe_doc},
1242 {"CreateProcess", winapi_CreateProcess, METH_VARARGS,
1243 CreateProcess_doc},
1244 {"DuplicateHandle", winapi_DuplicateHandle, METH_VARARGS,
1245 DuplicateHandle_doc},
1246 {"ExitProcess", winapi_ExitProcess, METH_VARARGS,
1247 ""},
1248 {"GetCurrentProcess", winapi_GetCurrentProcess, METH_VARARGS,
1249 GetCurrentProcess_doc},
1250 {"GetExitCodeProcess", winapi_GetExitCodeProcess, METH_VARARGS,
1251 GetExitCodeProcess_doc},
1252 {"GetLastError", winapi_GetLastError, METH_NOARGS,
1253 GetCurrentProcess_doc},
1254 {"GetModuleFileName", winapi_GetModuleFileName, METH_VARARGS,
1255 GetModuleFileName_doc},
1256 {"GetStdHandle", winapi_GetStdHandle, METH_VARARGS,
1257 GetStdHandle_doc},
1258 {"GetVersion", winapi_GetVersion, METH_VARARGS,
1259 GetVersion_doc},
1260 {"OpenProcess", winapi_OpenProcess, METH_VARARGS,
1261 ""},
1262 {"PeekNamedPipe", winapi_PeekNamedPipe, METH_VARARGS,
1263 ""},
1264 {"ReadFile", (PyCFunction)winapi_ReadFile, METH_VARARGS | METH_KEYWORDS,
1265 ""},
1266 {"SetNamedPipeHandleState", winapi_SetNamedPipeHandleState, METH_VARARGS,
1267 ""},
1268 {"TerminateProcess", winapi_TerminateProcess, METH_VARARGS,
1269 TerminateProcess_doc},
1270 {"WaitNamedPipe", winapi_WaitNamedPipe, METH_VARARGS,
1271 ""},
1272 {"WaitForMultipleObjects", winapi_WaitForMultipleObjects, METH_VARARGS,
1273 ""},
1274 {"WaitForSingleObject", winapi_WaitForSingleObject, METH_VARARGS,
1275 WaitForSingleObject_doc},
1276 {"WriteFile", (PyCFunction)winapi_WriteFile, METH_VARARGS | METH_KEYWORDS,
1277 ""},
1278 {NULL, NULL}
1279};
1280
1281static struct PyModuleDef winapi_module = {
1282 PyModuleDef_HEAD_INIT,
1283 "_winapi",
1284 NULL,
1285 -1,
1286 winapi_functions,
1287 NULL,
1288 NULL,
1289 NULL,
1290 NULL
1291};
1292
1293#define WINAPI_CONSTANT(fmt, con) \
1294 PyDict_SetItemString(d, #con, Py_BuildValue(fmt, con))
1295
1296PyMODINIT_FUNC
1297PyInit__winapi(void)
1298{
1299 PyObject *d;
1300 PyObject *m;
1301
1302 if (PyType_Ready(&OverlappedType) < 0)
1303 return NULL;
1304
1305 m = PyModule_Create(&winapi_module);
1306 if (m == NULL)
1307 return NULL;
1308 d = PyModule_GetDict(m);
1309
1310 PyDict_SetItemString(d, "Overlapped", (PyObject *) &OverlappedType);
1311
1312 /* constants */
1313 WINAPI_CONSTANT(F_DWORD, CREATE_NEW_CONSOLE);
1314 WINAPI_CONSTANT(F_DWORD, CREATE_NEW_PROCESS_GROUP);
1315 WINAPI_CONSTANT(F_DWORD, DUPLICATE_SAME_ACCESS);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001316 WINAPI_CONSTANT(F_DWORD, DUPLICATE_CLOSE_SOURCE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001317 WINAPI_CONSTANT(F_DWORD, ERROR_ALREADY_EXISTS);
1318 WINAPI_CONSTANT(F_DWORD, ERROR_BROKEN_PIPE);
1319 WINAPI_CONSTANT(F_DWORD, ERROR_IO_PENDING);
1320 WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
1321 WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
1322 WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001323 WINAPI_CONSTANT(F_DWORD, ERROR_MORE_DATA);
1324 WINAPI_CONSTANT(F_DWORD, ERROR_NETNAME_DELETED);
Richard Oudkerkfdb8dcf2012-05-05 19:45:37 +01001325 WINAPI_CONSTANT(F_DWORD, ERROR_NO_DATA);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001326 WINAPI_CONSTANT(F_DWORD, ERROR_NO_SYSTEM_RESOURCES);
1327 WINAPI_CONSTANT(F_DWORD, ERROR_OPERATION_ABORTED);
1328 WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_BUSY);
1329 WINAPI_CONSTANT(F_DWORD, ERROR_PIPE_CONNECTED);
1330 WINAPI_CONSTANT(F_DWORD, ERROR_SEM_TIMEOUT);
1331 WINAPI_CONSTANT(F_DWORD, FILE_FLAG_FIRST_PIPE_INSTANCE);
1332 WINAPI_CONSTANT(F_DWORD, FILE_FLAG_OVERLAPPED);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001333 WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_READ);
1334 WINAPI_CONSTANT(F_DWORD, FILE_GENERIC_WRITE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001335 WINAPI_CONSTANT(F_DWORD, GENERIC_READ);
1336 WINAPI_CONSTANT(F_DWORD, GENERIC_WRITE);
1337 WINAPI_CONSTANT(F_DWORD, INFINITE);
1338 WINAPI_CONSTANT(F_DWORD, NMPWAIT_WAIT_FOREVER);
1339 WINAPI_CONSTANT(F_DWORD, OPEN_EXISTING);
1340 WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_DUPLEX);
1341 WINAPI_CONSTANT(F_DWORD, PIPE_ACCESS_INBOUND);
1342 WINAPI_CONSTANT(F_DWORD, PIPE_READMODE_MESSAGE);
1343 WINAPI_CONSTANT(F_DWORD, PIPE_TYPE_MESSAGE);
1344 WINAPI_CONSTANT(F_DWORD, PIPE_UNLIMITED_INSTANCES);
1345 WINAPI_CONSTANT(F_DWORD, PIPE_WAIT);
1346 WINAPI_CONSTANT(F_DWORD, PROCESS_ALL_ACCESS);
Antoine Pitrou5438ed12012-04-24 22:56:57 +02001347 WINAPI_CONSTANT(F_DWORD, PROCESS_DUP_HANDLE);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001348 WINAPI_CONSTANT(F_DWORD, STARTF_USESHOWWINDOW);
1349 WINAPI_CONSTANT(F_DWORD, STARTF_USESTDHANDLES);
1350 WINAPI_CONSTANT(F_DWORD, STD_INPUT_HANDLE);
1351 WINAPI_CONSTANT(F_DWORD, STD_OUTPUT_HANDLE);
1352 WINAPI_CONSTANT(F_DWORD, STD_ERROR_HANDLE);
1353 WINAPI_CONSTANT(F_DWORD, STILL_ACTIVE);
1354 WINAPI_CONSTANT(F_DWORD, SW_HIDE);
1355 WINAPI_CONSTANT(F_DWORD, WAIT_OBJECT_0);
Victor Stinner373f0a92014-03-20 09:26:55 +01001356 WINAPI_CONSTANT(F_DWORD, WAIT_ABANDONED_0);
Antoine Pitrou23bba4c2012-04-18 20:51:15 +02001357 WINAPI_CONSTANT(F_DWORD, WAIT_TIMEOUT);
1358
1359 WINAPI_CONSTANT("i", NULL);
1360
1361 return m;
1362}