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

淺唱寂寞╮ 提交于 2019-12-09 02:31:54

问题


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.

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

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

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

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

PS. Would this be possible with PHP, or (eugh) 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++)


回答1:


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.

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.




回答2:


I have used mongoose to 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.




回答3:


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

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.

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 ;-)

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.




回答4:


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.




回答5:


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!

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




回答6:


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.




回答7:


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.



来源:https://stackoverflow.com/questions/3849858/any-way-to-use-html-as-an-interface-to-a-c-c-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!