最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

pinvoke - How to call user32.dll methods from javascript - Stack Overflow

programmeradmin1浏览0评论

I have a javascript running on a browser. Is it possible to call a function/method in user32.dll.

This is possible from C# by using pInvoke calls. How do I do the same in JavaScript?

Thanks,

Datte

I have a javascript running on a browser. Is it possible to call a function/method in user32.dll.

This is possible from C# by using pInvoke calls. How do I do the same in JavaScript?

Thanks,

Datte

Share Improve this question asked Oct 11, 2009 at 7:39 dattebayodattebayo 2,0724 gold badges30 silver badges41 bronze badges 1
  • 1 I'd like to know how to do this in jscript in Internet Explorer with the browser given sufficient permissions. – Matthew Lock Commented Apr 12, 2011 at 5:45
Add a comment  | 

6 Answers 6

Reset to default 12

Because of the JavaScript sandbox, you can't do it without a middle layer requiring elevated security permissions, such as a Netscape-style browser plug-in (widely supported), ActiveX control (pretty much IE-only), or .Net control (I assume that's possible; again probably IE-only). In each case, the JavaScript would talk to the control, which would in turn make the USER32 call for you.

None of that will work without the user having granted your application elevated permissions, but I'm guessing as you're requiring Windows, this is for some kind of intranet application where that may be possible.

You definitely need a plug-in, extension or ActiveX of your own installed on the client.

In the case of a firefox extension, you can use jsctypes to wrap the calls easily.
If you use the Jetpack API included with Firefox 4, it will be all JavaScript and won't even require a browser restart.

Here's an exemple from mozilla.org for a basic Hello World :

/* Load JS Ctypes Javascript module */
require("chrome").Cu.import("resource://gre/modules/ctypes.jsm");

/* Load windows api dll */
var lib = ctypes.open("user32.dll");

/* Declare the signature of the function we are going to call */
var msgBox = lib.declare("MessageBoxW",
                         ctypes.stdcall_abi,
                         ctypes.int32_t,
                         ctypes.int32_t,
                         ctypes.ustring,
                         ctypes.ustring,
                         ctypes.int32_t);
var MB_OK = 3;

/* Do it! */
var ret = msgBox(0, "Hello world", "title", MB_OK);

/* Display the returned value */
alert("MessageBox result : "+ret);

lib.close();

On the client - it is not possible for security reasons (imagine every site could run system commands on your computer... end of the world - maybe possible with an ActiveX, but that's IE only, but then again, the DLL is windows only).

If you want to run it on the server you'll need to go trough AJAX and C#.

Run dll methods on the client machine using javascript from a web page? That's what is gonna trigger apocalypse.

If you build your own web browser in C#, you can intercept JavaScript calls and translate them to whatever you want in your browser. Though that won't work if you want it to be available to other browsers.

Write a com object that wraps your call to user32. Invoke it in IE/javascript. Your DynamicWrapperX object would work for this (it would BE that com object, allowing you to just call your dlls as you wish).

发布评论

评论列表(0)

  1. 暂无评论