百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

node-activex

> 编程语言
开源

Node.js ActiveXObject 的实现

356 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Node.js ActiveXObject 的实现

Name

Windows C++ Node.JS addon, that implements COM IDispatch object wrapper, analog ActiveXObject on cscript.exe

Features

  • Using 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

javascript
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
});
  • Create COM object from JS object and may be send as argument (for example send to Excel procedure)
javascript
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 COM variant types support:
    • int - VT_INT
    • uint - VT_UINT
    • int8, char - VT_I1
    • uint8, uchar, byte - VT_UI1
    • int16, short - VT_I2
    • uint16, ushort - VT_UI2
    • int32 - VT_I4
    • uint32 - VT_UI4
    • int64, long - VT_I8
    • uint64, ulong - VT_UI8
    • currency - VT_CY
    • float - VT_R4
    • double - VT_R8
    • string - VT_BSTR
    • date - VT_DATE
    • decimal - VT_DECIMAL
    • variant - VT_VARIANT
    • null - VT_NULL
    • empty - VT_EMPTY
    • byref - VT_BYREF or use prefix 'p' to indicate reference to the current type
…
  • Additional dignostic propeties:

    • __id - dispatch identity, for exmplae: [email protected]
    • __value - value of dispatch object, equiles valueOf()
    • __type - array all member items with their properties
    • __methods - list member mathods by names (ITypeInfo::GetFuncDesc)
    • __vars - list member variables by names (ITypeInfo::GetVarDesc)
  • Full WScript Emulation Support through nodewscript

    • ActiveXObject type
    • WScript.CreateObject
    • WScript.ConnectObject
    • WScript.DisconnectObject
    • WScript.Sleep
    • WScript.Arguments
    • WScript.Version
    • GetObject
    • Enumerator

Usage example

Install package throw NPM (see below Building for details)

bash
npm install winax
npm install winax --msvs_version=2015
npm install winax --msvs_version=2017

Create ADO Connection throw global function

javascript
require('winax');
var con = new ActiveXObject('ADODB.Connection');

Or using Object prototype

javascript
var winax = require('winax');
var con = new winax.Object('ADODB.Connection');

Open connection and create simple table

javascript
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

javascript
var rs = con.Execute("Select * from persons.dbf"); 
var reccnt = rs.RecordCount;

Inspect RecordSet fields

javascript
var fields = rs.Fields;
var fldcnt = fields.Count;

Process records

javascript
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

javascript
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)

javascript
winax.release(con, rs, fields)

Working with Excel ranges using two dimension arrays (from 1.18.0 version)

  • The second dimension is only deduced from the first array.
  • If data is missing at the time of SafeArrayPutElement, VT_EMPTY is used.
  • Best way to explicitly pass VT_EMPTY is to use null
  • If the SAFEARRAY dims are smaller than those of the range, the missing cells are emptied.
  • Exception to 4! Excel may process the SAFEARRAY as it does when processing a copy/paste operation
…

TypeScript

Your tsconfig.json needs at least the following configuration:

  1. moduleResolution needs to be something other than classic. Depending on your target and module this might be the default. For example:
json
{
	"compilerOptions": {
		"target": "esnext",
		"module": "nodenext"
	}
}
  1. lib needs to exclude ScriptHost. It contains a conflicting global ActiveXObject type and is included by default. For example:
json
{
	"compilerOptions": {
		"lib": [ "ESNext" ]
	}
}

Tutorial and Examples

  • examples/ado.js

Usage as a library

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.

Building

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:

bash
npm install --global --production windows-build-tools

To obtain and build use console commands:

bash
git clone git://github.com/durs/node-axtivex.git
cd node-activex
npm install

or debug version

bash
npm install --debug

or using node-gyp directly

bash
npm install -g node_gyp
node-gyp configure
node-gyp build
node-gyp rebuild

For Electron users, need rebuild with a different V8 version:

bash
npm rebuild winax --runtime=electron --target=41.1.0 --arch=x64 --dist-url=https://electronjs.org/headers

or using electron rebuild module

bash
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

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.

WScript Limitations

Implicit Properties

This is a key drawback of v8 engine compared to MS. Consider an example:

javascript
    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.
    }

Function Setters

javascript
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 v8

Events & Sleep

Default 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.

32 vs 64 Bit

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.

Tests

mocha is required to run unit tests.

bash
npm install -g mocha
mocha --expose-gc test

Contributors

  • durs
  • somanuell
  • Daniel-Userlane
  • alexeygrinevich

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月17日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言