No console output when using AllocConsole and target architecture x86

前端 未结 5 1346
-上瘾入骨i
-上瘾入骨i 2020-11-27 17:05

I have a WinForms project, and if the user want\'s a debug console, I allocate a console with AllocConsole().

All console output works normally with the

5条回答
  •  不知归路
    2020-11-27 17:38

    Just wanted to post the answer from Visual studio Developer community. https://developercommunity.visualstudio.com/content/problem/12166/console-output-is-gone-in-vs2017-works-fine-when-d.html Go to this link and look at the answer from Ramkumar Ramesh. I have tested this code in VS 2017. I spent one day to find this answer. Hope it helps you as well.

    Edit-- As suggessted by Mike to include some description. I would like to suggesst some corrections in Zuniar answer. He tested with VS 2015. But that would not work in VS 2017. Instead of GetStdHandle, Please use CreateFile reference from kernel32.dll

    IntPtr stdHandle = CreateFile("CONOUT$", GENERIC_WRITE, FILE_SHARE_WRITE, 0, 
    OPEN_EXISTING, 0, 0);
    

    Before adding above code, please declare

    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern IntPtr CreateFile(string lpFileName, uint 
    dwDesiredAccess, uint dwShareMode, uint lpSecurityAttributes, uint 
    dwCreationDisposition, uint dwFlagsAndAttributes, uint hTemplateFile);
    
    private const int MY_CODE_PAGE = 437;
    private const uint GENERIC_WRITE = 0x40000000;
    private const uint FILE_SHARE_WRITE = 0x2;        
    private const uint OPEN_EXISTING = 0x3;
    

    i have taken this code from the given link.

提交回复
热议问题