如何运行本地 Windows 应用程序并将输出通过管道传输到浏览器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2716284/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I run a local Windows Application and have the output be piped into the Browser
提问by Trey Sherrill
I have Windows Application (.EXE file is written in C and built with MS-Visual Studio), that outputs ASCII text to stdout. I'm looking to enhance the ASCII text to include limited HTML with a few links. I'd like to invoke this application (.EXE File) and take the output of that application and pipe it into a Browser. This is not a one time thing, each new web page would be another run of the Local Application!
我有 Windows 应用程序(.EXE 文件用 C 编写并用 MS-Visual Studio 构建),它将 ASCII 文本输出到标准输出。我希望增强 ASCII 文本以包含带有一些链接的有限 HTML。我想调用这个应用程序(.EXE 文件)并获取该应用程序的输出并将其通过管道传输到浏览器中。这不是一次性的,每个新网页都是本地应用程序的又一次运行!
The HTML/java-script application below has worked for me to execute the application, but the output has gone into a DOS Box windows and not to pipe it into the Browser. I'd like to update this HTML Application to enable the Browser to capture that text (that is enhanced with HTML) and display it with the browser.
下面的 HTML/java 脚本应用程序让我可以执行该应用程序,但输出已进入 DOS 框窗口,而不是通过管道输入浏览器。我想更新这个 HTML 应用程序,使浏览器能够捕获该文本(用 HTML 增强)并在浏览器中显示它。
<body>
<script>
function go() {
w = new ActiveXObject("WScript.Shell");
w.run('C:/DL/Browser/mk_html.exe');
return true;
}
</script>
<form>
Run My Application (Window with explorer only)
<input type="button" value="Go"
onClick="return go()">
</FORM>
</body>
回答by Erik Hermansen
- Have the executable listen on a port following the HTTP protocol.
- Then have the web page make AJAX-style HTTP requests to the local port with JAvascript.
- The executable returns text.
- The web page updates itself through DOM manipulation in Javascript.
- 让可执行文件侦听遵循 HTTP 协议的端口。
- 然后让网页使用 JAvascript 向本地端口发出 AJAX 样式的 HTTP 请求。
- 可执行文件返回文本。
- 网页通过 Javascript 中的 DOM 操作来更新自身。
Yes, this works. It is happening 5 feet away from me right now in another cubicle.
是的,这有效。它现在发生在离我 5 英尺远的另一个隔间里。
回答by Alex K.
Your already using WScript to launch, it can also read StdOut.
您已经使用 WScript 启动,它也可以读取StdOut。
<html>
<head>
<script type="text/javascript">
function foo() {
var WshShell = new ActiveXObject("WScript.Shell");
var oExec = WshShell.Exec("ipconfig.exe");
var input = "";
while (!oExec.StdOut.AtEndOfStream) {
input += oExec.StdOut.ReadLine() + "<br />";
}
if (input)
document.getElementById("plop").innerHTML = input;
}
</script>
</head>
<body onload="foo();">
<code id="plop"></code>
</body>
</html>
回答by David
It would be easier to have your EXE create a temp file containing the HTML, then just tell Windows to open the temp HTML file in the browser.
让您的 EXE 创建一个包含 HTML 的临时文件会更容易,然后只需告诉 Windows 在浏览器中打开临时 HTML 文件。