Using STDIN with an AllocConsole()

泄露秘密 提交于 2019-12-14 03:41:57

问题


I have a third-party dll that I load into software that isn't mine, and I'm using AllocConsole() to create the standard windows CLI window so I have an easy means of outputting debug messages.

My problem is though, is that it ignores any kind of input. I just want to be able to use the console I allocated and enable the ability for me to give it some input.


回答1:


Thanks to Ben Voigt, I was able to cause the console to take input after I allocated it by doing:

freopen("CONIN$", "r", stdin); 
freopen("CONOUT$", "w", stdout); 
freopen("CONOUT$", "w", stderr); 

This also directs the stdout and strerr to the same console window, in case they are directed someplace else for some reason.




回答2:


Do you also redirect the stdoutand stderrto your console?

I used this code to get access to the JavaVM output from a Windows app.

if(::AllocConsole())
    {
        int hCrt = ::_open_osfhandle((intptr_t) ::GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
        FILE *hf = ::_fdopen( hCrt, "w" );
        *stdout = *hf;
        ::setvbuf(stdout, NULL, _IONBF, 0 );

        hCrt = ::_open_osfhandle((intptr_t) ::GetStdHandle(STD_ERROR_HANDLE), _O_TEXT);
        hf = ::_fdopen( hCrt, "w" );
        *stderr = *hf;
        ::setvbuf(stderr, NULL, _IONBF, 0 );
    }

After this I can see all stdoutand stderr outputs from the VM.




回答3:


This is the code that works for me:

freopen("CONOUT$", "w", stdout);

You can probably do something similar with CONIN$ and stdin (Open for read, of course).




回答4:


This is what I use.

    FILE *file = nullptr;
freopen_s(&file,"CONIN$", "r", stdin);
freopen_s(&file, "CONOUT$", "w", stdout);

Just hopes this helps someone.



来源:https://stackoverflow.com/questions/9020790/using-stdin-with-an-allocconsole

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!