none
How to get stdin/stdout from dlls invoked by current process RRS feed

  • 问题

  • Dear Gurus,

    I want to embed python interpreter in my program. So I can write *.py files outside of program and invoke it inside program as a script file. In order to get information output by the *.py file I need to get the stdin&stdout. I have tried some workflow but not perfect.

    1). I have tried use Allocconsle(). But I can only get the output info printed by std::out & printf() in the current code. all things which are printed by python35.dll are missing. I used print('xxxx') in the *.py file to test the output. Those *.py files are OK in command line mode.

    2). I also tried to derive class basic_streambuf and overwrite the in/out functions. It works only for output from std::out. Text from printf() as well as from dlls are missing.

    3). then I tried to use linker settings as below.

    #pragma comment( linker, "/subsystem:console /entry:wWinMainCRTStartup" )

    A cmd window is created along with the program. everything output from current process and dlls are retrieved successfully as I want. But the cmd window is not easy to control.

    Is there a better way for this purpose? What I want is to redirect those output to a control such as RICHEDIT which can be modified as a output window in my program.

    Thanks in advance for your kind help.

    Best Regards,

    John.



    • 已编辑 CloudBlaze 2017年7月19日 13:23 add more information
    2017年7月19日 13:21

全部回复

  • Hi CloudBlaze,

    感谢在MSDN论坛发帖。

    >>How to get stdin/stdout from dlls invoked by current process

    这里有篇文档关于如何将python interpreter 嵌入到你的程序中。你可以参考一下。

    https://docs.python.org/3.1/faq/windows.html#how-can-i-embed-python-into-a-windows-application

    该论坛是关于解决使用c++开发微软产品时产生的问题,鉴于你的问题和python相关,你可以到python community获取更好的帮助。

    谢谢你的理解与支持。

    Best Regards,

    Sera Yu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    2017年7月20日 1:57
  • Sera,

    多谢回复,我觉得这个功能和VC的实现也很有关系的。如果我要获取本进程内(包括调用的 Dll中产生的标准输出和输入),有什么好的办法么? 因为有一些输出和输入是Dll中产生的,用Linker的设置Console入口点可以实现,但是用AllocConsole就不行。

    如果用子进程重定向的方法是可以实现的,但是子进程中要访问主进程的数据还是比较麻烦,不如在同一个进程内。

    谢谢

    2017年7月20日 3:01
  • Hi CloudBlaze,

    >>多谢回复,我觉得这个功能和VC的实现也很有关系的。如果我要获取本进程内(包括调用的 Dll中产生的标准输出和输入),有什么好的办法么? 因为有一些输出和输入是Dll中产生的,用Linker的设置Console入口点可以实现,但是用AllocConsole就不行。

    如果用子进程重定向的方法是可以实现的,但是子进程中要访问主进程的数据还是比较麻烦,不如在同一个进程内

    你可以尝试使用std::cout.rdbuf, 在调用std::in/out 之前,获取一个指向与流缓冲对象相关联的指针。

    在我理解,DLL不是单独的进程,而是加载到应用程序地址空间中的动态库。 这意味着,当DLL将输出调用到stdout / stderr处理程序中时,将继承stdout / stderr句柄或正在运行的应用程序。如果要从DLL捕获所调用的输出,需要通过一个exe调用你的DLL函数。 这样,可以将stdout / stderr管道重定向到你的进程中,并执行所需的任何操作。

    希望对你有所帮助。

    Best Regards,

    Sera Yu


    MSDN Community Support<br/> Please remember to click &quot;Mark as Answer&quot; the responses that resolved your issue, and to click &quot;Unmark as Answer&quot; if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact <a href="mailto:MSDNFSF@microsoft.com">MSDNFSF@microsoft.com</a>.

    2017年8月4日 7:53