Get the handle and write to the console that launched our process

前端 未结 4 1638
清歌不尽
清歌不尽 2021-02-06 16:54

How could I write to the standard output of some already open console? I find the console I need with this piece of code:

    IntPtr ptr = GetForegroundWindow();         


        
4条回答
  •  清酒与你
    2021-02-06 17:21

    If the intention is to write to the parent console, if any, you can use the AttachConsole function with the ATTACH_PARENT_PROCESS argument. (see msdn attachconsole)

    ATTACH_PARENT_PROCESS (DWORD)-1 : Use the console of the parent of the current process

    And if you do need to check the parent process, you might use the CreateToolhelp32Snapshot and get the parent process thru the th32ParentProcessID member of the PROCESSENTRY32 structure.

提交回复
热议问题