Utiliser STDIN avec un allocation ()
-
14-11-2019 - |
Question
J'ai une DLL tiers que je charge dans des logiciels qui ne sont pas les miens, et j'utilise Alloconsole () pour créer la fenêtre Standard Windows CLI, donc j'ai un moyen facile de produire des messages de débogage.
Mon problème est cependant, c'est qu'il ignore tout type d'entrée. Je veux juste pouvoir utiliser la console que j'ai allouée et permet à moi de lui donner une entrée.
La solution
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.
Autres conseils
Do you also redirect the stdout
and stderr
to 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 stdout
and stderr
outputs from the VM.
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).
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.