如何制作隐藏的一次性控制台应用程序并与其通信?
我编写了一个小型控制台应用程序来包装有问题的第三方 DLL。 我使用 _popen,只想从控制台程序的标准输出中读取一个值。 这样做时,会短暂显示一个不需要的控制台窗口。
我知道可以通过使用 CreateProcess 的特定 STARTUPINFO 配置来避免这种情况。 但是,我更愿意将修复程序放在控制台应用程序中并仍然使用 _popen。 使用 Windows 子系统(即 WinMain 而不是 main)创建子进程不会使控制台窗口消失。 显然,操作系统分配了一个控制台来方便_popen通信。
请注意,第三方 DLL 不稳定,但对于与专有硬件接口是必需的。 因此,任何将DLL加载到GUI应用程序内存空间中的方法都是不可接受的。
I've written a small console application to wrap a third-party DLL that has issues. I am calling it from a GUI application using _popen, and just want to read a value from the console program's standard output. In doing so, an undesirable console window is briefly displayed.
I am aware that this can be avoided by using a certain STARTUPINFO configuration with CreateProcess. However, I would prefer to put the fix in the console application and still use _popen. Creating the child process using the windows subsystem (i.e. WinMain instead of main) does not make the console window go away. Apparently, a console is allocated by the operating system to facilitate the _popen communication.
Note that the third-party DLL is unstable, but necessary to interface with proprietary hardware. Therefore, any approach the loads the DLL in the GUI applications memory space is unacceptable.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
1)我支持将 STARTUPINFO 设置为隐藏窗口来启动它的想法。 这是 Windows 上的 SOP,因此它比尝试强加 Unix SOP 效果更好。 此外,您可能很想在单独测试时查看输出。
2) 或者您可以制作一个不显示窗口的 GUI 应用程序。 它相当于一个没有控制台的控制台应用程序。
编辑
链接 Indeera发布的内容显示了如何执行第二个选项。
编辑
好的,如果您只想向父级发送信息,最简单的方法是编写一个文件。 保持这种顺序的一种方法是让父级传递一个唯一的文件名作为子级写入的命令行参数。 家长可以监控该文件并对其内容采取行动。
有很多替代方案。 到目前为止讨论的最接近的两种方法是命名管道,但您也可以使用各种形式的 IPC,包括基于套接字的 IPC。
但是,您不能在没有控制台的情况下访问 stdin/stdout/stderr。 您可以使用隐藏的控制台启动,但它必须确实有一个。
编辑
lua是否允许直接运行可执行文件,而不是通过cmd.exe?
1) I support the idea of launching it with STARTUPINFO set to hide the window. This is SOP on Windows, so it'll work better than trying to impose Unix SOP. Moreover, you may well want to see the output when testing it in isolation.
2) Or you can make a GUI app that doesn't ever show a window. It amounts to a console app without a console.
edit
The link Indeera posted shows how to do the second option.
edit
Ok, if all you want is to send information to the parent, the simplest way is to write a file. One way to keep this orderly is to have the parent pass a unique filename as a command line parameter, which the child writes to. The parent can monitor the file and act on its contents.
There are many alternativers. The closest to the two approaches discussed so far would be named pipes, but you could also use various forms of IPC, including socket-based ones.
What you can't do, however, is access stdin/stdout/stderr without a console. You can launch with a hidden console, but it has to actually have one.
edit
Does lua allow running the executable directly, instead of through cmd.exe?
要在父 GUI 应用程序和隐藏的子控制台应用程序之间进行通信,请参阅以下 MSDN 文章:
To communicate between a parent GUI application and a hidden child console application reference the following MSDN articles:
我认为您无法在不启动控制台的情况下使用 stdin/stdout 。 不过,您可以做的是将包装应用程序编写为不显示任何窗口的 GUI 应用程序(只需将 main() 切换到 winmain()),然后使用某种类型的 IPC 机制,如套接字或共享内存。
I don't think you can use stdin/stdout without it starting up the console. What you can do though, is write the wrapper app as a GUI app that doesn't show any window (just switch main() to winmain()) and then use some type of IPC mechanism like a socket or shared memory.