c# - No console output when using AllocConsole and target architecture x86 -


i have winforms project, , if user want's debug console, allocate console allocconsole().

all console output works target architecture set "any cpu", when change "x86" doesn't output (console.read() still works expected). if open exe directly, output works. looks visual studio redirects it's own "output" window.

i tried this answer, didn't work, tried console.setout(getstdhandle(-11)), didn't work either.

setting target architecture 'any cpu' no option me.

so here 2 questions:

  • why case when target architecture set x86?
  • how can output console when running inside of visual studio?

when "enable native code debugging" enabled, output consoles crated allocconsole redirected debug output window instead.

the reason happens in x86 , not anycpu because can debug native code in x86 application.

note behavior occurs consoles created allocconsole. console application's output not redirected.

edit: other reason console not outputting text when you've written console before calling allocconsole.

regardless of reason, code restore output if redirected, , reopen console in case it's invalid. uses magic number 7 handle of stdout usually.

using system; using system.io; using system.runtime.interopservices;  public static class consolehelper {     public static void createconsole()     {         allocconsole();          // stdout's handle seems equal 7         intptr defaultstdout = new intptr(7);         intptr currentstdout = getstdhandle(stdoutputhandle);          if (currentstdout != defaultstdout)             // reset stdout             setstdhandle(stdoutputhandle, defaultstdout);          // reopen stdout         textwriter writer = new streamwriter(console.openstandardoutput())          { autoflush = true };         console.setout(writer);     }      // p/invoke required:     private const uint32 stdoutputhandle = 0xfffffff5;     [dllimport("kernel32.dll")]     private static extern intptr getstdhandle(uint32 nstdhandle);     [dllimport("kernel32.dll")]     private static extern void setstdhandle(uint32 nstdhandle, intptr handle);     [dllimport("kernel32")]     static extern bool allocconsole(); } 

see how detect if console.in (stdin) has been redirected? way detect if console handles have been redirected.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -