diff --git a/CMakeLists.txt b/CMakeLists.txt index 595c454..f3c75b3 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 "Build PyStand as a console application." OFF) # sources -add_executable(PyStand - WIN32 +set(sources PyStand.cpp resource.rc ) +if (PYSTAND_CONSOLE) + add_executable(PyStand ${sources}) + target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE) +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..c0173c1 100644 --- a/PyStand.cpp +++ b/PyStand.cpp @@ -26,6 +26,7 @@ //--------------------------------------------------------------------- PyStand::~PyStand() { + FreeLibrary(_hDLL); } @@ -308,6 +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 "try:\n" " fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY)\n" " fp = os.fdopen(fd, 'w')\n" @@ -315,6 +317,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 +335,22 @@ const char *init_script = // main //--------------------------------------------------------------------- +#ifdef PYSTAND_CONSOLE +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 if (AttachConsole(ATTACH_PARENT_PROCESS)) { freopen("CONOUT$", "w", stdout); freopen("CONOUT$", "w", stderr); @@ -357,6 +365,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;