How can I redirect stdout to some visible display in a Windows Application?

后端 未结 8 1327
时光取名叫无心
时光取名叫无心 2020-11-27 03:31

I have access to a third party library that does \"good stuff.\" It issues status and progress messages to stdout. In a Console application I can see these messages just f

8条回答
  •  时光取名叫无心
    2020-11-27 04:04

    Here we'll set a new entry point consoleMain that overrides your own one.

    1. Determine the entry point of your application. In VisualStudio, select Project Properties/Linker/Advanced/Entry Point. Let us call it defaultMain.
    2. Somewhere in your source code declare the original entry point (so we can chain to it) and the new entry point. Both must be declared extern "C" to prevent name mangling.

      extern "C"
      {
        int defaultMain (void);
        int consoleMain (void);
      }
      
    3. Implement the entry point function.

      __declspec(noinline) int consoleMain (void)
      {
        // __debugbreak(); // Break into the program right at the entry point!
        AllocConsole();    // Create a new console
        freopen("CON", "w", stdout);
        freopen("CON", "w", stderr);
        freopen("CON", "r", stdin); // Note: "r", not "w".
        return defaultMain();
      }
      
    4. Add your test code somewhere, e.g. in a button click action.

      fwprintf(stdout, L"This is a test to stdout\n");
      fwprintf(stderr, L"This is a test to stderr\n");
      cout<<"Enter an Integer Number Followed by ENTER to Continue" << endl;
      _flushall();
      int i = 0;
      int Result = wscanf( L"%d", &i);
      printf ("Read %d from console. Result = %d\n", i, Result);
      
    5. Set consoleMain as the new entry point (Project Properties/Linker/Advanced/Entry Point).

提交回复
热议问题