JSctypes: Difference between revisions

458 bytes added ,  22 October 2009
m
Updated the link to the devmo page
m (Updated the link to the devmo page)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{Note|This article is outdated. See the devmo pages for the current version.https://developer.mozilla.org/en/JavaScript_code_modules/ctypes.jsm}}
== Overview ==
== Overview ==


Line 16: Line 18:


<pre>
<pre>
const nsINativeTypes = Components.interfaces.nsINativeTypes;
/* Load JS Ctypes Javascript module */
const MB_OK = 3;
Components.utils.import("resource://gre/modules/ctypes.jsm");
 
function messageBox() {
/* Load windows api dll */
  // create the XPCOM js-ctypes instance
var lib = ctypes.open("C:\\WINDOWS\\system32\\user32.dll");
  var library = Components.classes["@developer.mozilla.org/js-ctypes;1"]
 
                          .createInstance(nsINativeTypes);
/* 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();
</pre>


  // load the native shared library
Instead of defining the whole path, you may also just give the file name.
  library.open("user32.dll");


  // declare the native method
<pre>
  var msgBox = library.declare("MessageBoxW",          /* function name */
var lib = ctypes.open("user32.dll");
                              nsINativeTypes.STDCALL,  /* call type */
</pre>
                              nsINativeTypes.INT32,    /* return type */
                              nsINativeTypes.INT32,    /* arg 1 */
                              nsINativeTypes.WSTRING,  /* arg 2 */
                              nsINativeTypes.WSTRING,  /* arg 3 */
                              nsINativeTypes.INT32);   /* arg 4 */


  // call the native method
Or even without the extension.
  var ret = msgBox(0, "This is the coolest message", "My Title", MB_OK);


  // test the return value
<pre>
  alert(ret);
var lib = ctypes.open("user32");
}
</pre>
</pre>
If the full path is not given, Windows uses the following search order to locate the DLL:
#The current directory of the task that is using the DLL.
#The \WINNT directory.
#The \WINNT\SYSTEM directory.
#The \WINNT\SYSTEM32 directory.
#The directory of the executable for the task that is using the DLL.
#A directory listed in the PATH environment variable.
(taken from http://support.microsoft.com/kb/164501/)


{{Note|This functionality cannot be accessed from JavaScript used in web content. Only JavaScript that runs with chrome privileges (extensions and Firefox UI for example) can use js-ctypes.}}
{{Note|This functionality cannot be accessed from JavaScript used in web content. Only JavaScript that runs with chrome privileges (extensions and Firefox UI for example) can use js-ctypes.}}
20

edits