proog128 / jswin

Loading dynamic link libraries in JavaScript
Other
47 stars 11 forks source link

How to reference this project in javascript? #11

Closed bernardoadc closed 8 years ago

bernardoadc commented 9 years ago

I've read de README file, and the project is great! However, I did not understand how to use it.

It allows to write javascript code that references DLLs. But there's no js code to include in my own js project, so how you do it? I need to 'include' the loadLibrary into the javascript environment, otherwise I get "ReferenceError: loadLibrary is not defined".

I'm sure i'm not getting this. Any help apreciated.

EDIT: oh, sorry, forgot to mention: the "my own js project" would be a node.js server script that should access the server's local libraries.

colceagus commented 8 years ago

I subscribe to this issue. I want to load a DLL into a clientside js library.

PAEz commented 8 years ago

@khullah and maybe @danielmihai This is for creating exe's out of js that are capable of calling functions from a dll. It does not allow js running in an other enviroment to call dlls. For Node you want to look at.... https://github.com/node-ffi/node-ffi or maybe, depending on where your running it.... http://tjanczuk.github.io/edge/#/

The first one is pretty much the standard for doing this stuff. The second is if you have .net/mono available and although Ive never used it I hear its awesome.

If your going with the first option you might be interested in this post..... http://blog.scottfrees.com/calling-native-c-dlls-from-a-node-js-web-app

colceagus commented 8 years ago

I want to call a function from within a dll in a clientside javascript app. How can I do this?

PAEz commented 8 years ago

If your talking from the browser then forget it. The only way you could pull that off is with something like a chrome extension that used message passing to an exe that would make the call and relay back. Not cool. But, if your talking an app, then try nwjs or electron. Both use node behind the scenes and you should be able to leverage either of the methods I mentioned.

colceagus commented 8 years ago

I have a SwipeReader 3M CR100 driver dll and i want to be able to read the ID Card data from a front-end app (Angular based). Flow: Swipe ID Card through reader, an event is fired by the driver, catch that event in javascript and display read data.

If you know a way to do this, you're a god.

PAEz commented 8 years ago

Definitely sounds like a great job for electron/nwjs. Never done this sort of thing myself but read a couple of times people doing stuff like this....it was an old serial device if I remember right (usually dont). https://github.com/node-ffi/node-ffi would be what you want. Aslong as you know how the dll works and have some experience working with dll's you shouldnt have to much hassle. Have a look around for what others did with it. If you dont have experience with dll's youve got some learning to do ;P

Hey, does this relate?..... http://jackli.logdown.com/posts/203039-using-c-to-develop-activex-object ...it looks like there is .Net availability which means you could also try Edge, which Ive been told is simpler. Or try and ask that guy for his active-x component, coz then you could use IE...but whyd you wanna do that is beyond me ;P Go for electron or nwjs and the above and you should definately be able to pull it off.

colceagus commented 8 years ago

the users will have IE and Chrome (fortunately and unfortunately)

thank you for your patience and answers ;) let me dig in to it and i'll come back

bernardoadc commented 8 years ago

@PAEz thanks, I was actually looking for a substitute for the node-ffi/edge solutions.

The reason is that my use is a little more complex than usual. I am trying to build desktop apps with js through github's sihorton/appjs-deskshell, which combines node.js (backend) with chromium (frontend). DLL access should be via backend, but either edge and node-ffi are more difficult to use in this environment.

Problems as I remember:

As for now, I'm currently calling (sadly) vbs/js (JScript) scripts from node.js via child_exec/spawn, which has for itself some difficulties. Writing this post came to my mind I'd better use node.js vs jswin (why didn't I saw that earlier??). So I could implement all DLL interaction with this library, and somehow node.js calling it. Any help there?

PAEz commented 8 years ago

Ok starting to get to the end of my ability to help, but here goes..... Whats the benifit of creating something with this over what your doing now with jscript?....I wouldnt have thought itd be much. For one thing this is a very low level (or is it high, can never remember) language. To do most things takes a BUNCH of setup, where as jscript can do a bunch already (even accessing a file is going to require you to code a lot). I dont think this will make your live easier. Think your better off struggling with ffi, the end result would be worth it. I did one thing with ffi (wish I could remember what, I have a SHOCKING memory) and yeah it was a struggle, but I got there and it was rather good.
Really, I dont see whats so bad about making them install .net and whats needed? You would get massive amounts of flexibility with it. Explain that to them. Without it I think your just making more work then it should be.

PAEz commented 8 years ago

And if its an excel thing then doesnt that have an active-x component (YEARS since I looked at excel)? If so maybe jsdb could help?.... http://www.jsdb.org/examples.html

bernardoadc commented 8 years ago

lol.. So I'm pushing you to the limits haha (I'm on mine as well) Ok, let me think in topics:

Anyways, thanks for your help! I'll dig in more with node-ffi, seems to be the better/"right" solution. One observation: the ffi module needs to be built in the desired platform, so the ref module corresponds to the specific OS. That's one of my problems (I can't even user npm here, but that's a whole other subject). Just saying that yes, ffi would allow my universal approach for any platform - unfortunately not on my scenario : > (

colceagus commented 8 years ago

Which do you recommend between nw.js and electron ? I would like something with more support and more applications developed along the path of the framework development. Keep in mind that i want the developed module to be integrated with an angular front-end application.

LE: what about edge.js ?

bernardoadc commented 8 years ago

@danielmihai as far as I know, you can't do that with angular in a browser, since it is on the client-side. This is sanboxed and there's no way to get around it. What you should do is make a request do the server-side to do the job. Eg: a button that will execute a .exe file and get a response of some sort would follow a path somewhat like this: button -> angular -> server -> exe | response -> server -> angular -> browser.

However, if you're not talking about browsers, then yes, it should be possible. It seems with nw.js you could do it in the client, but maybe it would be good practice to follow the path I've mentioned non the less.

As for support, nw.js should be better, since has some relation to Google and Intel.

colceagus commented 8 years ago

I managed to do the ActiveX stuff. it works. accomplished with stoneage tools. advancing to tool age. I have to figure out the V8 Chrome transition. I am currently trying to use Edge.JS. Managed to load dll, and call a function from the dll.

My challenge is to bind event handlers from javascript to .NET events and to keep the c# object alive in the node event loop. I managed to start the swipereader, but on other calls, a string that I set there gets null. I guess the event that handles the function call finishes (exits the event stack and gets solved), so the .NET object gets cleared (garbage collected). Any ideas on how to do this?

colceagus commented 8 years ago

I managed to do it with EdgeJS, finally. EdgeJS creates a bridge between V8 and CLR and I've managed to marshal functions between the two. The functions sent to C# have a specific signature of the form javascript function(input, callback) { //do something with input; callback(); } In C# you have to assign the received function to a C# Func<object, Task<object>>delegate.

You can take a look at the code in my repositories, SwipeReaderNode.

Thank you very much for your assistance, and thanks to tjanczuk for his library and support!

Happy Coding!

PAEz commented 8 years ago

@danielmihai Thats awesome! Well done. I went to put this on reddit and couldnt. I dont know enough about this to write a description and your read me is next to blank. Also, the description the swipereader app done in node doesnt really help. I googled SwipeReader Application and got a whole bunch of stuff. This is for something like the 3m cr100 or something right? You should put that on the readme and say what its capable of. And then you should do a blog post on how you got it to work with edge. Ive got no interest in a card reader, but I sure do on what you went through to make it work. Love seeing node/js used to make real apps. You made something cool, dont make it look like a cowboy project, do a readme ;P

colceagus commented 8 years ago

Ok, I'll start working on the README.md file and comment the code thoroughly. I thought about the blog post, any suggestions where should I post it to? Until my personal site , I'll post it on a tech blog. We can co-edit.

On Thu, Jan 28, 2016 at 2:18 PM, PAEz notifications@github.com wrote:

@danielmihai https://github.com/danielmihai Thats awesome! Well done. I went to put this on reddit and couldnt. I dont know enough about this to write a description and your read me is next to blank. Also, the description the swipereader app done in node doesnt really help. I googled SwipeReader Application and got a whole bunch of stuff. This is for something like the 3m cr100 or something right? You should put that on the readme and say what its capable of. And then you should do a blog post on how you got it to work with edge. Ive got no interest in a card reader, but I sure do on what you went through to make it work. Love seeing node/js used to make real apps. You made something cool, dont make it look like a cowboy project, do a readme ;P

— Reply to this email directly or view it on GitHub https://github.com/proog128/jswin/issues/11#issuecomment-176151900.

bernardoadc commented 8 years ago

Its been a while.. Finally got into electron, and that is very nice. Its actually what I was looking for - having the ability to write full js+web apps for desktop use, platform free. I dont know how evolved it was when I started this topic, but now is sure very solid.

For the active X stuff, I've decided to really use node spawn's to call cscript in Windows, which has full integration with those kind of libraries. The trick was to create a JScript REPL who awaits node's commands, pretty cool. So basically its Frontend -> Node (within Electron) -> cscript spawn -> JScript REPL -> ActiveX object. The child process spawned waits for commands in the app side, while the REPL in the "OS side".

The reason I did this was because node-ffi needs to be compiled in the destination OS. This means you need to get the right electron compilation, and then do the same to node-ffi but manually. I prefer to leave the "hard part" to someone else.. In other words, it would be nicer if node-ffi also had several compilations available for download (it does so by default when you install it via npm, but what if your machine isnt the destination target??). So this turns into a lot of extra work. Since ActiveX will be used in Windows and all of them have already the right cscript in it, this saves a bunch of work. Node-ffi may still be required for more complex situations/other OS's, though.