mozilla

Revision 439903 of Standard OS Libraries

  • Revision slug: Mozilla/js-ctypes/Standard_OS_Libraries
  • Revision title: Standard OS Libraries
  • Revision id: 439903
  • Created:
  • Creator: r_boy_
  • Is current revision? No
  • Comment

Revision Content

Windows

To learn about all the Windows functions and which DLL file you will need to call, there are two options. Go to the MSDN website and find the function then scroll down to the "Requirements" section and in it will be a line titled "Library" and "DLL", that is the library you will want to use in the ctypes.open statement.

Alternatively you can download and use the PowerBASIC WIN32.HLP file. Download it here: http://www.powerbasic.com/files/pub/mstools/win32.zip

Example - GetCursorPos

I wanted to get the mouse cursor position without using a MouseMove listener as this is a high overhead event listener. So I searched on Bing, "Get cursor coordinates" and I came across GetCursorPos for Windows. I then searched it on MSDN and saw that it needed the user32.dll.  (MSDN - GetCursorPos)

This example was created on Windows XP SP2. It is a full working example, just copy & paste it to try it out.

Components.utils.import("resource://gre/modules/ctypes.jsm");
var lib = ctypes.open("user32.dll");

/* Declare the signature of the function we are going to call */
const struct_lpPoint = new ctypes.StructType("lpPoint",
                                                       [ { "x": ctypes.int },     //note: if you go to GetCursorPos page on MSDN it says that x is type Long, and in data types Long on MSDN it says it's a 64-byte signed integer, so before ctypes.int I was trying ctypes.int64_t but it wouldnt work, no clue why ctypes.int was the solution
                                                         { "y": ctypes.int } ]);  //note: if you go to GetCursorPos page on MSDN it says that x is type Long, and in data types Long on MSDN it says it's a 64-byte signed integer, so before ctypes.int I was trying ctypes.int64_t but it wouldnt work, no clue why ctypes.int was the solution
var GetCursorPos = lib.declare('GetCursorPos', ctypes.winapi_abi, ctypes.bool, struct_lpPoint.ptr);

/* Use it like this */
var point = new struct_lpPoint;
var ret = GetCursorPos(point.address());
Components.utils.reportError(ret);
Components.utils.reportError(point);

Linux

This is an simple sqaure root logic required in linux o.s

Example-

sqrt.c

/
#include <linux/kernel.h>
#include <linux/module.h>
unsigned long int_sqrt(unsigned long x)
{
          unsigned long op, res, one;

          op = x;
          res = 0;

          one = 1UL << (BITS_PER_LONG - 2);
          while (one > op)
                  one >>= 2;

          while (one != 0) {
                  if (op >= res + one) {
                          op = op - (res + one);
                          res = res +  2 * one;
                  }
                  res /= 2;
                  one /= 4;
         }
          return res;
  }
  EXPORT_SYMBOL(int_sqrt);
 * quote processing is performed.  Multiple whitespace characters are

Mac OS

Mac OS uses Carbon. Documentation is needed.

Revision Source

<h2 id="Windows">Windows</h2>
<p>To learn about all the Windows functions and which DLL file you will need to call, there are two options. Go to the MSDN website and find the function then scroll down to the "Requirements" section and in it will be a line titled "Library" and "DLL", that is the library you will want to use in the ctypes.open statement.</p>
<p>Alternatively you can download and use the PowerBASIC WIN32.HLP file. Download it here: http://www.powerbasic.com/files/pub/mstools/win32.zip</p>
<h3 id="Example_-_GetCursorPos">Example - GetCursorPos</h3>
<p>I wanted to get the mouse cursor position without using a MouseMove listener as this is a high overhead event listener. So I searched on Bing, "Get cursor coordinates" and I came across GetCursorPos for Windows. I then searched it on MSDN and saw that it needed the user32.dll.&nbsp; (<a href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648390%28v=vs.85%29.aspx" title="http://msdn.microsoft.com/en-us/library/windows/desktop/ms648390%28v=vs.85%29.aspx">MSDN - GetCursorPos</a>)</p>
<p>This example was created on Windows XP SP2. It is a full working example, just copy &amp; paste it to try it out.</p>
<pre class="brush: js">
Components.utils.import("resource://gre/modules/ctypes.jsm");
var lib = ctypes.open("user32.dll");

/* Declare the signature of the function we are going to call */
const struct_lpPoint = new ctypes.StructType("lpPoint",
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                [ { "x": ctypes.int },     //note: if you go to GetCursorPos page on MSDN it says that x is type Long, and in data types Long on MSDN it says it's a 64-byte signed integer, so before ctypes.int I was trying ctypes.int64_t but it wouldnt work, no clue why ctypes.int was the solution
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;                                { "y": ctypes.int } ]);  //note: if you go to GetCursorPos page on MSDN it says that x is type Long, and in data types Long on MSDN it says it's a 64-byte signed integer, so before ctypes.int I was trying ctypes.int64_t but it wouldnt work, no clue why ctypes.int was the solution
var GetCursorPos = lib.declare('GetCursorPos', ctypes.winapi_abi, ctypes.bool, struct_lpPoint.ptr);

/* Use it like this */
var point = new struct_lpPoint;
var ret = GetCursorPos(point.address());
Components.utils.reportError(ret);
Components.utils.reportError(point);</pre>
<h2 id="Linux">Linux</h2>
<p>This is an simple sqaure root logic required in linux o.s</p>
<h2 id="Example-">Example-</h2>
<h3 id="sqrt.c">sqrt.c</h3>
<pre class="brush: cpp" id="f3/d5/7c7a5bd38fc757960181fb449eacd532f1c6_3/0">
<span class="comment">/</span>
#include &lt;linux/kernel.h&gt;
#include &lt;linux/module.h&gt;
unsigned long int_sqrt(unsigned long x)
{
          unsigned long op, res, one;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; op = x;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; res = 0;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; one = 1UL &lt;&lt; (BITS_PER_LONG - 2);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (one &gt; op)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; one &gt;&gt;= 2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while (one != 0) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (op &gt;= res + one) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; op = op - (res + one);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; res = res +&nbsp; 2 * one;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; res /= 2;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; one /= 4;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return res;
&nbsp; }
&nbsp; EXPORT_SYMBOL(int_sqrt);
<span class="comment"> * quote processing is performed.  Multiple whitespace characters are</span></pre>
<h2 id="Mac_OS">Mac OS</h2>
<p>Mac OS uses Carbon. Documentation is needed.</p>
Revert to this revision