有什么方法可以使用 HTML 作为 C/C++ 程序的接口?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3849858/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 04:44:30  来源:igfitidea点击:

Any way to use HTML as an interface to a C/C++ program?

htmlchardwareusbhardware-interface

提问by Bojangles

I'm developing a product that is interfaced over USB. I want to write a control app for it, but I cannot program a GUI for poo, so I came up with the idea of using a web page (local to the app's install directory) as the interface to the program.

我正在开发一种通过 USB 接口的产品。我想为它编写一个控制应用程序,但我无法为 poo 编写 GUI,所以我想出了使用网页(应用程序安装目录的本地页面)作为程序界面的想法。

So, the line of communication would be: User --> HTML page (not hosted - it's local) --> C++ program --> USB port

因此,通信线路将是:用户 --> HTML 页面(未托管 - 它是本地的)--> C++ 程序 --> USB 端口

I'd like to do it this way because it's portable, at least UI wise, and I can write HTML fluently.

我想这样做是因为它是可移植的,至少在 UI 方面是明智的,而且我可以流利地编写 HTML。

My question is this: would it be possible to use an HTML page to talk to the USB port through a C++ program?

我的问题是:是否可以使用 HTML 页面通过 C++ 程序与 USB 端口通信?

If not, I'll learn GTK - it's portable enough.

如果没有,我将学习 GTK - 它足够便携。

PS. Would this be possible with PHP, or (eugh) Java?

附注。这可以用 PHP 或(呃)Java 实现吗?

EDIT: Just realised PHP is a server-side language - it wouldn't work in my situation because the page needs to be run as a program (I.E., just use the web browser and HTML as the GUI, with the actual processing done using C/C++)

编辑:刚刚意识到 PHP 是一种服务器端语言 - 它在我的情况下不起作用,因为页面需要作为程序运行(IE,只需使用网络浏览器和 HTML 作为 GUI,实际处理使用C/C++)

采纳答案by CMircea

You can actually create web applications in C++ using a toolkit called Wt(similar API to Qt). There is a standalone browser as well as a module for Apache. This way you can use C++ as your programming language the same way you would C# with ASP.NET, and thus you can also use your existing C++ code. It's not really elegant though.

实际上,您可以使用名为Wt(类似于 Qt 的 API)的工具包在 C++ 中创建 Web 应用程序。有一个独立的浏览器以及一个用于 Apache 的模块。通过这种方式,您可以像使用 C# 和 ASP.NET 一样使用 C++ 作为您的编程语言,因此您还可以使用现有的 C++ 代码。不过也不是很优雅。

I'd actually recommend you learn Qt and write the whole application on top of it. It's incredibly portable and has an extensive API for just about everything - it's an application framework; GTK+ does just the UI, and in C, although the GTKmm wrapper for C++ is nice. Check it out at http://qt.nokia.com- it's available under the GNU LGPL.

我实际上建议您学习 Qt 并在其上编写整个应用程序。它具有令人难以置信的可移植性,并且具有适用于几乎所有内容的广泛 API - 它是一个应用程序框架;GTK+ 只做 UI 和 C 语言,尽管 C++ 的 GTKmm 包装器很好。在http://qt.nokia.com 上查看- 它在 GNU LGPL 下可用。

回答by Ferruccio

I have used mongooseto control a server through HTTP/HTML. I generated the pages directly in C++, but I don't see why you couldn't invoke the PHP interpreter to render page contents.

我使用猫鼬通过 HTTP/HTML 控制服务器。我直接用 C++ 生成了页面,但我不明白为什么不能调用 PHP 解释器来呈现页面内容。

回答by Steve Jessop

Unless you write a browser plugin (not very portable), a browser viewing a web page can't just call functions in your C++ program.

除非您编写浏览器插件(不是很便携),否则查看网页的浏览器不能只调用 C++ 程序中的函数。

If you're going to control a device from a browser, basically your C++ program needs to incorporate a web server. So, when a link or a button is clicked, the C++ program will receive a connection on a socket it's listening to, and can do whatever.

如果您打算从浏览器控制设备,基本上您的 C++ 程序需要包含一个 Web 服务器。因此,当单击链接或按钮时,C++ 程序将接收到它正在侦听的套接字上的连接,并且可以执行任何操作。

That's a perfectly respectable thing to learn to do (probably using some third-party libraries to help with the web server aspect), but so is learning to write a GUI app, and the latter is more directly to the point here ;-)

这是一件非常值得学习的事情(可能会使用一些第三方库来帮助处理 Web 服务器方面),但是学习编写 GUI 应用程序也是如此,后者更直接地切入这一点;-)

Certainly Java can do the same thing. You can even run PHP from the command line and implement your server in PHP, although that's sort of inside out from the way PHP is normally used.

当然,Java 可以做同样的事情。您甚至可以从命令行运行 PHP 并在 PHP 中实现您的服务器,尽管这与 PHP 的通常使用方式有些不同。

回答by atzz

Check out HTMLayout. The low-level component, HTMLayout itself, is free. It's Windows-only at the moment, as far as I know, but the work is in progress to port it to Linux and Mac. (A Windows Mobile port already exists). They also have Sciter, which is built around HTMLayout and contains scripting facilities. Check out the demos, they are impressive!

查看HTMLayout。低级组件 HTMLayout 本身是免费的。据我所知,目前它仅适用于 Windows,但正在进行将其移植到 Linux 和 Mac 的工作。(Windows Mobile 端口已存在)。他们还有 Sciter,它围绕 HTMLayout 构建并包含脚本工具。查看演示,它们令人印象深刻!

(Sorry if it sounded like a plug. I'm not affiliated with them, but I really like HTMLayout :) ).

(对不起,如果它听起来像一个插件。我不隶属于他们,但我真的很喜欢 HTMLayout :))。

回答by Eimantas

If you'd want to do that - your C++ program would have to take a role of http server. AFAIK - lots of router configuration utilities work this way (except for Apple's Airport Express/Extreme) - they have small httpd running with html pages served via CGI.

如果你想这样做 - 你的 C++ 程序将不得不扮演 http 服务器的角色。AFAIK - 许多路由器配置实用程序都以这种方式工作(Apple 的 Airport Express/Extreme 除外) - 他们有小的 httpd 运行通过 CGI 提供的 html 页面。

回答by Pablo Santa Cruz

The closest thing I can think of is writing a Firefox/Chrome/IE/Safari plugin and have the user install it on his/her browser to run your GUI to control your device.

我能想到的最接近的事情是编写一个 Firefox/Chrome/IE/Safari 插件,并让用户将其安装在他/她的浏览器上以运行您的 GUI 来控制您的设备。

回答by Milan

You could embed a simple http server (for example http://code.google.com/p/mongoose/) that exposes a REST interface to your apps functionality and serves a HTML page which can make calls to the interface.

您可以嵌入一个简单的 http 服务器(例如http://code.google.com/p/mongoose/),它向您的应用程序功能公开一个 REST 接口,并提供一个可以调用该接口的 HTML 页面。