wxz
wxz

Reputation: 27

How to use c/c++ library with Javascript?

I have a SDK from a third party. I don't have the source code, just the headers, dll and lib files. This SDK provides C APIs to communicate with a device through USB. I'd like to use this SDK in Javascript.

I've search on this topic and there are a few approaches. However, based on my understanding, each of them has some difficulties to be readily applied to my situation:

  1. emscripten - require all source code to be compiled into bitcode, which I don't have. And it also has limitations on what c++ code can be successfully parsed.
  2. SWIG - has some known issues that are non-trival. E.g., long long is not supported, Javascript callbacks are not supported (I don't understand about this one, does this mean the js code that calls the c/c++ lib cannot use callback function?)
  3. Node gyp - seems you have to be familiar with the V8 source and several other libs before you can get it done.
  4. WebIDL - not quite sure about what its about, how much effort it requires to get it work, if it's possible at all.

Any thoughts/suggestions are welcome!

Edit 1: the context is that I'd like to get that data from the device into a browser, or a stand-alone node-webkit app. Similar to the use case that to get geolocation info in a browser. In general, I don't think my question (use C++ component with JS) is a bizaare idea. For example, Mozilla developed lots of XPCOM components in C++, then wrapped with XPConnect so that those components can be used by Javascript in Firefox, or an XUL project. But this approach is very labor intensive and put too much dependencies on Mozilla's toolchain. Node js looks like a promising alternative. Node-webkit is essentially doing the same thing as XUL: help building/running a cross-platform GUI app based on browser.

Upvotes: 0

Views: 4087

Answers (1)

Adam
Adam

Reputation: 17329

Use the right tool for the job.

Your app will be Windows only anyway, so use the plethora of tools out there for that.

If you want to build the GUI in HTML/JavaScript you still can. Just make your app be a wrapper for an HTML element that you write your GUI in. That way the GUI is HTML, but the rest of the app can natively talk to your .dll. That will give both you and your users a good experience.

If you bring a browser into this you'll get nothing but pain. All browsers are actively working to make what you want impossible. Loading native code means nothing but security, stability and compatibility headaches for the browser makers.

The only other clean way would be to write your app as a sort of a SaaS app. I.e in two parts, a server component written as a native Windows app that loads the .dll and provides an HTTP server that serves up your HTML+JS GUI component. Whether or not this approach makes sense depends on what your app does. If it's expected to be used all on one machine, then this adds unnecessary pain to your users. If it makes sense to have the .dll loaded on one machine and then access it remotely, either on another computer or a mobile app then it might make sense. Some apps that work this way include SABnzbd, Plex media server, Transmission has such a mode, anything meant to run on a NAS, etc.

Upvotes: 1

Related Questions