
The rest of the arguments would be passed to the native function. As a simplistic start, I had the first argument specify a string identifying the native function, in the Windbg-inspired syntax of “module!export”, e.g. The CallFunction method of the FunctionWrapper object is callable by JScript clients with arguments of arbitrary count and type of their choosing. HRESULT CallFunction( SAFEARRAY(VARIANT) args, VARIANT* retVal) Helpstring("IFunctionWrapper Interface"), At this point, the exposed interface looks like this: After a short investigation I learned of the IDL vararg attribute, which accomplishes just what I had in mind. I knew you could call JScript functions with less or more arguments than they expect in their definition and figured pulling off the same in a native method I’ll expose to the script would be ideal. I created an ATL COM DLL and gave the coclass the ProgID “FunctionWrapper.FunctionWrapper.1”. In my mind’s eye, I envisioned an in-process COM server accessible to Active Scripting clients (implements IDispatch and associated with a ProgID) providing a call interface to arbitrary native functions. I thought JSctypes is really cool and it then occurred to me it should not be prohibitively difficult to implement a similar adaptation layer for Microsoft’s JScript and possibly other Active Scripting languages. Later, and repeatedly as needed, ffi_call is used to call the actual function with a specific set of argument values, passed in as an array and to retrieve the value returned from the native function. You initialize an ffi_cif (call information?) structure with the ABI type, return value type, argument count and argument types of the native function to be invoked by using the ffi_prep_cif function.

Since libffi is designed to be compiled with a UNIX-style toolchain (has AT&T syntax assembly files, for instance) and Python needs to compile with Visual C++, the author of ctypes, Thomas Heller, ported an old revision of the library to Visual C++. It is called libffi, the Foreign Function Interface library and seems to originate from the gcc project.
#DARKSTAR ONE IPHLPAPI DLL CODE#
I reviewed both JSctypes and Python’s ctypes source code in their respective source code repositories and learned that they both share a common implementation of the lowest component in such a native interface layer.

As the interface layer seeks to support a broader and broader variety of argument types (basic data types, then structures, arrays, then callback functions, etc.) the task becomes increasingly complicated and difficult. Now, with JSctypes, Mozilla’s JavaScript code, when privileged (obviously a native call interface is not appropriate in the context of untrusted web content), can call most native functions with relative ease and without a compiled component, aside from JSctypes itself.Ī native function interface for a dynamic language needs to deal with the relatively complex task of setting up the call stack frame for an arbitrary native API, according to argument counts, types and alignment requirements deduced dynamically at script execution time. However, such an approach has clear disadvantages as every conceivable native functionality needs to be wrapped on a case by case basis by a compiled XPCOM component. Usually, calling native functionality from JavaScript is achieved by exposing an XPCOM component to script. Mozilla has a COM-like architecture at the base of its object model which is called XPCOM. JSctypes takes Python’s ctypes concept into Mozilla’s JavaScript implementation.
#DARKSTAR ONE IPHLPAPI DLL .DLL#
DLL modules can be accessed as attributes of the module attribute matching their calling convention (e.g., 32 or ) and script functions can be passed as callbacks to the native APIs being invoked.

Most of the time, you can just call functions without specifying the number and types of the arguments they receive. Its interface really feels at home in a dynamic language. It’s a great library that allows you to call native C functions dynamically from Python code. If you haven’t heard of ctypes, take a minute to get acquainted. Both the interface and name are inspired by the Python ctypes module, included with the standard distribution since version 2.5.

JSctypes is an XPCOM component for Mozilla that allows calling native (or “foreign”) functions from privileged JavaScript code. It was the first time I had heard of this project. It discussed recent improvements to JSctypes. Sometime in April I stumbled upon this post in Planet Mozilla. Wondering if the Gecko (Mozilla Firefox’s rendering engine) folks are also busy with that, I followed both the Planet WebKit and Planet Mozilla feeds for a few weeks. A few weeks ago I was following the excitement as WebKit, Safari’s browser engine, incrementally passed more and more of the Acid 3 standards test.
