Node.js ActiveXObject 的实现
Windows C++ Node.JS addon, that implements COM IDispatch object wrapper, analog ActiveXObject on cscript.exeUsing ITypeInfo for conflict resolution between property and method (for example !rs.EOF not working without type information, becose need define EOF as an object)
Using optional parameters on constructor call
var con = new ActiveXObject("Object.Name", {
activate: false, // Allow activate existance object instance (through CoGetObject), false by default
getobject: false, // Allow using name of the file in the ROT (through GetAccessibleObject), false by default
type: true // Allow using type information, true by default
});var com_obj = new ActiveXObject({
text: test_value,
obj: { params: test_value },
arr: [ test_value, test_value, test_value ],
func: function(v) { return v*2; }
});…Additional dignostic propeties:
Full WScript Emulation Support through nodewscript
Install package throw NPM (see below Building for details)
npm install winax
npm install winax --msvs_version=2015
npm install winax --msvs_version=2017Create ADO Connection throw global function
require('winax');
var con = new ActiveXObject('ADODB.Connection');Or using Object prototype
var winax = require('winax');
var con = new winax.Object('ADODB.Connection');Open connection and create simple table
con.Open('Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\tmp;Extended Properties="DBASE IV;"', '', '');
con.Execute("Create Table persons.dbf (Name char(50), City char(50), Phone char(20), Zip decimal(5))");
con.Execute("Insert into persons.dbf Values('John', 'London','123-45-67','14589')");
con.Execute("Insert into persons.dbf Values('Andrew', 'Paris','333-44-55','38215')");
con.Execute("Insert into persons.dbf Values('Romeo', 'Rom','222-33-44','54323')");Select query and return RecordSet object
var rs = con.Execute("Select * from persons.dbf");
var reccnt = rs.RecordCount;Inspect RecordSet fields
var fields = rs.Fields;
var fldcnt = fields.Count;Process records
rs.MoveFirst();
while (!rs.EOF) {
var name = fields("Name").value;
var town = fields("City").value;
var phone = fields("Phone").value;
var zip = fields("Zip").value;
console.log("> Person: " + name + " from " + town + " phone: " + phone + " zip: " + zip);
rs.MoveNext();
}Or using fields by index
rs.MoveFirst();
while (rs.EOF != false) {
var name = fields[0].value;
var town = fields[1].value;
var phone = fields[2].value;
var zip = fields[3].value;
console.log("> Person: " + name + " from " + town + " phone: " + phone + " zip: " + zip);
rs.MoveNext();
}Release COM objects (but other temporary objects may be keep references too)
winax.release(con, rs, fields)Working with Excel ranges using two dimension arrays (from 1.18.0 version)
…Your tsconfig.json needs at least the following configuration:
moduleResolution needs to be something other than classic. Depending on your target and module this might be the default. For example:{
"compilerOptions": {
"target": "esnext",
"module": "nodenext"
}
}lib needs to exclude ScriptHost. It contains a conflicting global ActiveXObject type and is included by default. For example:{
"compilerOptions": {
"lib": [ "ESNext" ]
}
}The repo allows to re-use some of its code as a library for your own native node addon.
To include it, install it as a normal node-dependency and then add it
to the "dependencies" section of your binding.gyp file like this:
"dependencies": [
"This makes functions like Variant2Value or Value2Variant that translate between COM VARIANT and node types
available in your code.
Note that providing this library functionality is not the core target of this repo however,
so importing it eg. currently declares
all methods in the global namespace and opens the namespaces v8 and node.
Check the source code src/disp.h and src/utils.h for details.
This project uses Visual C++ 2013 (or later versions then support C++11 standard). Bulding also requires node-gyp and python 2.6 (or later) to be installed. Supported NodeJS Versions (x86 or x64): 10, 12, 16, 18, 20, 22, 23 You can do this with npm:
npm install --global --production windows-build-toolsTo obtain and build use console commands:
git clone git://github.com/durs/node-axtivex.git
cd node-activex
npm installor debug version
npm install --debugor using node-gyp directly
npm install -g node_gyp
node-gyp configure
node-gyp build
node-gyp rebuildFor Electron users, need rebuild with a different V8 version:
npm rebuild winax --runtime=electron --target=41.1.0 --arch=x64 --dist-url=https://electronjs.org/headersor using electron rebuild module
npm install --save-dev @electron/rebuild
.\node_modules\.bin\electron-rebuild.cmd
Change --target value to your electron version. See also Electron Documentation: Using Native Node Modules.
WScript is designed as a runtime, so WSH scripts should be executed from command line. WScript emulation mode allows one to use standard WScript features and mix them with nodejs and ES6 parts.
There is one significant difference between WScript and NodeJS in general. NodeJS is designed to be non-blocking and execution is done when last statement is done AND all pending promises and callbacks are resolved. WScript runtime is linear. It is done when last statement is executed.
So NodeJS is more convenient for writing things like async web services. Also with NodeJS you get access to invaluable collection of npm modules.
WScript has impressive collection of Windows-specific APIs available through ActiveX (such as WScript.Shell, FileSystemObject etc). Also it supports application events to implement two way communication with external apps and processes.
This module allows taking the best from the two worlds - you may now use npm modules in your WScript scripts. Or, if you have bunch of legacy JScript sources, you may now execute them using this NodeJS-based runtime.
If you install this package with -g key, you will have nodewscript command.
Usage:
nodewscript [options] Its direct analog in the windows scripting host is cscript.exe (it it outputs to the console, and WScript.Echo writes a line instead of showing a popup window).
Where Filename.js is a file designed for windows scripting host.
This is a key drawback of v8 engine compared to MS. Consider an example:
var a = WScript.CreateObject(...)
if( a.Prop )
{
// If 'Prop' is a dynamic property (i.e. it is not defined in TypeInfo and
// not marked explicitly as a property, then execution never gets here. Even if a.Prop is null or false.
}var WshShell = new ActiveXObject("WScript.Shell");
var processEnv = WshShell.Environment("PROCESS");
processEnv("ENV_VAR") = "CUSTOM_VALUE"; // This syntax is valid for JScript, but will throw syntax error on v8Default WScript engine checks for events every time you do some sync operation or Sleep. In this implementation we check for events when WScript.Sleep is executed.
It is using the same bitness as installed version of the nodejs. So if your JScript files are designed for 32 bit, make sure to have nodejs x86 version installed before installing the winax.
mocha is required to run unit tests.
npm install -g mocha
mocha --expose-gc test暂无开放 Issues,或尚未同步最近议题。