From d47d49c1ac5cad53280dd196f2ec5c6633e36a8f Mon Sep 17 00:00:00 2001 From: myd7349 Date: Sat, 5 Mar 2022 19:49:26 +0800 Subject: [PATCH 1/3] add an option to build PyStand as a console based application GUI-based PyStand is not suitable for CLI scripts: - It doesn't redirect stdin, so user can not call `input`; - Using AttachConsole, user must hit ENTER to exit PyStand.exe when it is executed from command line or PowerShell; --- CMakeLists.txt | 12 +++++++++--- PyStand.cpp | 8 ++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 595c454..7d17618 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,21 @@ cmake_minimum_required(VERSION 3.5) -project (PyStand) +project (PyStand LANGUAGES CXX RC) +option(PYSTAND_CONSOLE_BASED "Build PyStand as a console based application." OFF) # sources -add_executable(PyStand - WIN32 +set(sources PyStand.cpp resource.rc ) +if (PYSTAND_CONSOLE_BASED) + add_executable(PyStand ${sources}) + target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE_BASED) +else() + add_executable(PyStand WIN32 ${sources}) +endif() # static link if (CMAKE_GENERATOR MATCHES "Visual Studio") diff --git a/PyStand.cpp b/PyStand.cpp index c6d9d5a..c9561f5 100644 --- a/PyStand.cpp +++ b/PyStand.cpp @@ -308,6 +308,7 @@ const char *init_script = " ctypes.windll.user32.MessageBoxW(None, str(msg), str(info), 0)\n" " return 0\n" "os.MessageBox = MessageBox\n" +#ifndef PYSTAND_CONSOLE_BASED "try:\n" " fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY)\n" " fp = os.fdopen(fd, 'w')\n" @@ -315,6 +316,7 @@ const char *init_script = " sys.stderr = fp\n" "except Exception as e:\n" " pass\n" +#endif "for n in ['lib', 'site-packages']:\n" " test = os.path.join(PYSTAND_HOME, n)\n" " if os.path.exists(test): sys.path.append(test)\n" @@ -332,17 +334,22 @@ const char *init_script = // main //--------------------------------------------------------------------- +#ifdef PYSTAND_CONSOLE_BASED +int main() +#else //! flag: -static //! src: //! mode: win //! int: objs int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show) +#endif { PyStand ps("runtime"); if (ps.DetectScript() != 0) { return 3; } +#ifndef PYSTAND_CONSOLE_BASED if (AttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); @@ -357,6 +364,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show) SetEnvironmentVariableA("PYSTAND_STDIN", fn.c_str()); } } +#endif int hr = ps.RunString(init_script); // printf("finalize\n"); return hr; From 62fb7045ab74eb5e5ffd0960115d0d3cb4e6eda2 Mon Sep 17 00:00:00 2001 From: myd7349 Date: Sat, 5 Mar 2022 20:55:11 +0800 Subject: [PATCH 2/3] call FreeLibrary in destructor --- PyStand.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/PyStand.cpp b/PyStand.cpp index c9561f5..016f70c 100644 --- a/PyStand.cpp +++ b/PyStand.cpp @@ -26,6 +26,7 @@ //--------------------------------------------------------------------- PyStand::~PyStand() { + FreeLibrary(_hDLL); } From bf44ac669045499cbaa820d8a4df4275fafa8807 Mon Sep 17 00:00:00 2001 From: myd7349 Date: Sun, 6 Mar 2022 10:38:48 +0800 Subject: [PATCH 3/3] rename PYSTAND_CONSOLE_BASED to PYSTAND_CONSOLE --- CMakeLists.txt | 6 +++--- PyStand.cpp | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d17618..f3c75b3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5) project (PyStand LANGUAGES CXX RC) -option(PYSTAND_CONSOLE_BASED "Build PyStand as a console based application." OFF) +option(PYSTAND_CONSOLE "Build PyStand as a console application." OFF) # sources set(sources @@ -10,9 +10,9 @@ set(sources resource.rc ) -if (PYSTAND_CONSOLE_BASED) +if (PYSTAND_CONSOLE) add_executable(PyStand ${sources}) - target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE_BASED) + target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE) else() add_executable(PyStand WIN32 ${sources}) endif() diff --git a/PyStand.cpp b/PyStand.cpp index 016f70c..c0173c1 100644 --- a/PyStand.cpp +++ b/PyStand.cpp @@ -309,7 +309,7 @@ const char *init_script = " ctypes.windll.user32.MessageBoxW(None, str(msg), str(info), 0)\n" " return 0\n" "os.MessageBox = MessageBox\n" -#ifndef PYSTAND_CONSOLE_BASED +#ifndef PYSTAND_CONSOLE "try:\n" " fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY)\n" " fp = os.fdopen(fd, 'w')\n" @@ -335,7 +335,7 @@ const char *init_script = // main //--------------------------------------------------------------------- -#ifdef PYSTAND_CONSOLE_BASED +#ifdef PYSTAND_CONSOLE int main() #else //! flag: -static @@ -350,7 +350,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show) if (ps.DetectScript() != 0) { return 3; } -#ifndef PYSTAND_CONSOLE_BASED +#ifndef PYSTAND_CONSOLE if (AttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr);