Skip to content

Commit 190b6d3

Browse files
authored
Merge pull request #9 from myd7349/console-based
add an option to build PyStand as a console based application
2 parents c6e45a1 + bf44ac6 commit 190b6d3

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

CMakeLists.txt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
cmake_minimum_required(VERSION 3.5)
22

3-
project (PyStand)
3+
project (PyStand LANGUAGES CXX RC)
44

5+
option(PYSTAND_CONSOLE "Build PyStand as a console application." OFF)
56

67
# sources
7-
add_executable(PyStand
8-
WIN32
8+
set(sources
99
PyStand.cpp
1010
resource.rc
1111
)
1212

13+
if (PYSTAND_CONSOLE)
14+
add_executable(PyStand ${sources})
15+
target_compile_definitions(PyStand PRIVATE PYSTAND_CONSOLE)
16+
else()
17+
add_executable(PyStand WIN32 ${sources})
18+
endif()
1319

1420
# static link
1521
if (CMAKE_GENERATOR MATCHES "Visual Studio")

PyStand.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
//---------------------------------------------------------------------
2727
PyStand::~PyStand()
2828
{
29+
FreeLibrary(_hDLL);
2930
}
3031

3132

@@ -308,13 +309,15 @@ const char *init_script =
308309
" ctypes.windll.user32.MessageBoxW(None, str(msg), str(info), 0)\n"
309310
" return 0\n"
310311
"os.MessageBox = MessageBox\n"
312+
#ifndef PYSTAND_CONSOLE
311313
"try:\n"
312314
" fd = os.open('CONOUT$', os.O_RDWR | os.O_BINARY)\n"
313315
" fp = os.fdopen(fd, 'w')\n"
314316
" sys.stdout = fp\n"
315317
" sys.stderr = fp\n"
316318
"except Exception as e:\n"
317319
" pass\n"
320+
#endif
318321
"for n in ['lib', 'site-packages']:\n"
319322
" test = os.path.join(PYSTAND_HOME, n)\n"
320323
" if os.path.exists(test): sys.path.append(test)\n"
@@ -332,17 +335,22 @@ const char *init_script =
332335
// main
333336
//---------------------------------------------------------------------
334337

338+
#ifdef PYSTAND_CONSOLE
339+
int main()
340+
#else
335341
//! flag: -static
336342
//! src:
337343
//! mode: win
338344
//! int: objs
339345
int WINAPI
340346
WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
347+
#endif
341348
{
342349
PyStand ps("runtime");
343350
if (ps.DetectScript() != 0) {
344351
return 3;
345352
}
353+
#ifndef PYSTAND_CONSOLE
346354
if (AttachConsole(ATTACH_PARENT_PROCESS)) {
347355
freopen("CONOUT$", "w", stdout);
348356
freopen("CONOUT$", "w", stderr);
@@ -357,6 +365,7 @@ WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR args, int show)
357365
SetEnvironmentVariableA("PYSTAND_STDIN", fn.c_str());
358366
}
359367
}
368+
#endif
360369
int hr = ps.RunString(init_script);
361370
// printf("finalize\n");
362371
return hr;

0 commit comments

Comments
 (0)