|
2 | 2 |
|
3 | 3 | #define WIN32_LEAN_AND_MEAN |
4 | 4 | #include <windows.h> |
| 5 | +#include <fcntl.h> |
5 | 6 |
|
6 | 7 | #include "Python.h" |
7 | 8 |
|
| 9 | +#ifdef LAUNCHER |
| 10 | +/* Q105305 suggests this routine to adjust the handles. */ |
| 11 | +static void adjust_file(DWORD handle, FILE* f, char* mode) |
| 12 | +{ |
| 13 | + int hCrt; |
| 14 | + FILE *hf; |
| 15 | + hCrt = _open_osfhandle((intptr_t)GetStdHandle(handle), _O_TEXT); |
| 16 | + hf = _fdopen(hCrt, mode); |
| 17 | + *f = *hf; |
| 18 | + setvbuf(f, NULL, _IONBF, 0); |
| 19 | + /* Alternatively, we could use __set_app_type and _set_osfhnd, |
| 20 | + but that appears to be undocumented. */ |
| 21 | +} |
| 22 | +#endif |
| 23 | + |
8 | 24 | int WINAPI WinMain( |
9 | 25 | HINSTANCE hInstance, /* handle to current instance */ |
10 | 26 | HINSTANCE hPrevInstance, /* handle to previous instance */ |
11 | 27 | LPSTR lpCmdLine, /* pointer to command line */ |
12 | 28 | int nCmdShow /* show state of window */ |
13 | 29 | ) |
14 | 30 | { |
| 31 | +#ifdef LAUNCHER |
| 32 | + int i; |
| 33 | + if (__argc > 1 && strcmp(__argv[1], "-console") == 0) { |
| 34 | + /* Allocate a console, and remove the -console argument. */ |
| 35 | + AllocConsole(); |
| 36 | + for (i = 2; i < __argc; i++) |
| 37 | + __argv[i-1] = __argv[i]; |
| 38 | + __argc--; |
| 39 | + /* Make stdin, stdout, stderr use the newly allocated OS handles. */ |
| 40 | + adjust_file(STD_INPUT_HANDLE, stdin, "r"); |
| 41 | + adjust_file(STD_OUTPUT_HANDLE, stdout, "w"); |
| 42 | + adjust_file(STD_ERROR_HANDLE, stderr, "w"); |
| 43 | + } |
| 44 | +#endif |
15 | 45 | return Py_Main(__argc, __argv); |
16 | 46 | } |
0 commit comments