Guido van Rossum | 639ccae | 1998-08-08 20:00:49 +0000 | [diff] [blame] | 1 | /* Minimal main program -- everything is loaded from the library. */ |
Guido van Rossum | d666eae | 1997-12-10 05:50:18 +0000 | [diff] [blame] | 2 | |
Fredrik Lundh | ddbc118 | 2000-07-08 18:06:41 +0000 | [diff] [blame^] | 3 | #define WINDOWS_LEAN_AND_MEAN |
Guido van Rossum | d666eae | 1997-12-10 05:50:18 +0000 | [diff] [blame] | 4 | #include <windows.h> |
Fredrik Lundh | ddbc118 | 2000-07-08 18:06:41 +0000 | [diff] [blame^] | 5 | #include <fcntl.h> |
| 6 | #include <sys/stat.h> |
| 7 | |
Guido van Rossum | 67ab672 | 1998-08-08 19:58:59 +0000 | [diff] [blame] | 8 | #include "Python.h" |
Guido van Rossum | d666eae | 1997-12-10 05:50:18 +0000 | [diff] [blame] | 9 | |
| 10 | extern int Py_Main(); |
| 11 | |
| 12 | int WINAPI WinMain( |
Fredrik Lundh | ddbc118 | 2000-07-08 18:06:41 +0000 | [diff] [blame^] | 13 | HINSTANCE hInstance, /* handle to current instance */ |
| 14 | HINSTANCE hPrevInstance, /* handle to previous instance */ |
| 15 | LPSTR lpCmdLine, /* pointer to command line */ |
| 16 | int nCmdShow /* show state of window */ |
Guido van Rossum | d666eae | 1997-12-10 05:50:18 +0000 | [diff] [blame] | 17 | ) |
| 18 | { |
Fredrik Lundh | ddbc118 | 2000-07-08 18:06:41 +0000 | [diff] [blame^] | 19 | int null_file; |
| 20 | |
| 21 | /* |
| 22 | * make sure that the C RTL has valid file descriptors for |
| 23 | * stdin, stdout, stderr. Use the NUL device if necessary. |
| 24 | * This allows popen to work under pythonw. |
| 25 | * |
| 26 | * When pythonw.exe starts the C RTL function _ioinit is called |
| 27 | * first. WinMain is called later hence the need to check for |
| 28 | * invalid handles. |
| 29 | * |
| 30 | * Note: FILE stdin, stdout, stderr do not use the file descriptors |
| 31 | * setup here. They are already initialised before WinMain was called. |
| 32 | */ |
| 33 | |
| 34 | null_file = open("NUL", _O_RDWR); |
| 35 | |
| 36 | if (_get_osfhandle(0) == -1) |
| 37 | dup2(null_file, 0); |
| 38 | |
| 39 | if (_get_osfhandle(1) == -1) |
| 40 | dup2(null_file, 1); |
| 41 | |
| 42 | if (_get_osfhandle(2) == -1) |
| 43 | dup2(null_file, 2); |
| 44 | |
| 45 | close(null_file); |
| 46 | |
| 47 | return Py_Main(__argc, __argv); |
Guido van Rossum | d666eae | 1997-12-10 05:50:18 +0000 | [diff] [blame] | 48 | } |