0

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.

7
  • might be eaiser to keep it as C++ and use IPC/redis/0mq/etc to talk to/from JS. Commented Sep 17, 2015 at 4:35
  • What about github.com/node-ffi/node-ffi Commented Sep 17, 2015 at 5:48
  • SWIG does support long long. I've used it years ago on a Python/C++ project. It can support literally any time if you write type maps, but I believe there is native support as well. Callback in your case means the c++ code calling JS code. Commented Sep 17, 2015 at 6:21
  • 1
    What's the context? Other than Node, JavaScript implies a browser. It makes zero sense to use a .dll in a browser. Since you're not familiar with Node, that implies you're not using it. So why JavaScript? Commented Sep 17, 2015 at 6:25
  • @Adam I've added some info. Regarding why js, it seems the trend for developing any apps with a GUI, web app or not, is html5+css+js Commented Sep 17, 2015 at 14:57

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I didn't express my intention clearly. Assume the device is an input device that the GUI needs to respond to. Between the GUI(html+js) and the device, some code need to process the data (thru the C APIs) and bubble up events to GUI for handling (in js). In essence, I'm talking about writing a GUI toolkit. The big problem I see is that the 3rd party lib is provided as binary instead of source code (for other platforms as well, Linux, Android etc). The SaaS approach looks similar to IPC thru socket. Would you explain how to make an app be a wrapper for an HTML element?
I understood that to be your intention.
SaaS approach would definitely be IPC though a socket over HTTP. We tend to call that "viewing a webpage".
All GUI toolkits support embedding an HTML engine. In .Net I believe that is IE's engine. Basically you can make your app's window have a single child, the HTML pane. Then you can do the rest of the UI in HTML. The details on how to do that depend on what language and toolset you choose.
I see your point. Most frameworks (even wxWidgets) provide a 'webview' control for that. I guess that's the best/easiest way at the moment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.