Cross-Platform build scripts for audio plugins
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
3.1KB

  1. diff --git a/source/bases/Win32GUI.c b/source/bases/Win32GUI.c
  2. index 490cdd3..f350d5f 100644
  3. --- a/source/bases/Win32GUI.c
  4. +++ b/source/bases/Win32GUI.c
  5. @@ -220,26 +220,85 @@ static int FatalScriptError()
  6. #include "Common.c"
  7. +/* the code below is based on https://www.tillett.info/2013/05/13/how-to-create-a-windows-program-that-works-as-both-as-a-gui-and-console-application/
  8. + */
  9. +
  10. +static BOOL has_console = FALSE;
  11. +
  12. +// Attach output of application to parent console
  13. +static BOOL attachOutputToConsole(void)
  14. +{
  15. + HANDLE consoleHandleOut, consoleHandleError;
  16. +
  17. + if (AttachConsole(ATTACH_PARENT_PROCESS))
  18. + {
  19. + // Redirect unbuffered STDOUT to the console
  20. + consoleHandleOut = GetStdHandle(STD_OUTPUT_HANDLE);
  21. + if (consoleHandleOut != INVALID_HANDLE_VALUE)
  22. + {
  23. + freopen("CONOUT$", "w", stdout);
  24. + setvbuf(stdout, NULL, _IONBF, 0);
  25. + }
  26. +
  27. + // Redirect unbuffered STDERR to the console
  28. + consoleHandleError = GetStdHandle(STD_ERROR_HANDLE);
  29. + if (consoleHandleError != INVALID_HANDLE_VALUE)
  30. + {
  31. + freopen("CONOUT$", "w", stderr);
  32. + setvbuf(stderr, NULL, _IONBF, 0);
  33. + }
  34. +
  35. + return TRUE;
  36. + }
  37. +
  38. + //Not a console application
  39. + return FALSE;
  40. +}
  41. +
  42. +static void closeConsole()
  43. +{
  44. + // Send "enter" to release application from the console
  45. + // This is a hack, but if not used the console doesn't know the application has
  46. + // returned. The "enter" key only sent if the console window is in focus.
  47. + if (has_console && (GetConsoleWindow() == GetForegroundWindow() || SetFocus(GetConsoleWindow()) != NULL))
  48. + {
  49. + INPUT ip;
  50. + // Set up a generic keyboard event.
  51. + ip.type = INPUT_KEYBOARD;
  52. + ip.ki.wScan = 0; // hardware scan code for key
  53. + ip.ki.time = 0;
  54. + ip.ki.dwExtraInfo = 0;
  55. +
  56. + // Send the "Enter" key
  57. + ip.ki.wVk = 0x0D; // virtual-key code for the "Enter" key
  58. + ip.ki.dwFlags = 0; // 0 for key press
  59. + SendInput(1, &ip, sizeof(INPUT));
  60. +
  61. + // Release the "Enter" key
  62. + ip.ki.dwFlags = KEYEVENTF_KEYUP; // KEYEVENTF_KEYUP for key release
  63. + SendInput(1, &ip, sizeof(INPUT));
  64. + }
  65. +}
  66. +
  67. //-----------------------------------------------------------------------------
  68. // WinMain()
  69. // Main routine for the executable in Windows.
  70. //-----------------------------------------------------------------------------
  71. -int WINAPI wWinMain(
  72. - HINSTANCE instance, // handle to application
  73. - HINSTANCE prevInstance, // previous handle to application
  74. - wchar_t *commandLine, // command line
  75. - int showFlag) // show flag
  76. +int wmain(int argc, wchar_t **argv)
  77. {
  78. + has_console = attachOutputToConsole();
  79. + atexit(closeConsole);
  80. int status = 0;
  81. // initialize Python
  82. - if (InitializePython(__argc, __wargv) < 0)
  83. + if (InitializePython(argc, argv) < 0)
  84. status = 1;
  85. // do the work
  86. if (status == 0 && ExecuteScript() < 0)
  87. status = 1;
  88. Py_Finalize();
  89. +
  90. return status;
  91. }