@node-ctypes/comtypes 0.3.0 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -624
- package/dist/automation/dispatch.js +10 -3
- package/dist/automation/record.js +3 -1
- package/dist/automation/variant.d.ts +4 -4
- package/dist/automation/variant.js +102 -29
- package/dist/client/dynamic.js +7 -8
- package/dist/client/events.js +18 -2
- package/dist/client/index.js +3 -1
- package/dist/commethod.d.ts +1 -1
- package/dist/commethod.js +151 -57
- package/dist/errorinfo.d.ts +10 -3
- package/dist/errorinfo.js +66 -18
- package/dist/errors.d.ts +7 -1
- package/dist/executor/apartment.d.ts +7 -3
- package/dist/executor/apartment.js +65 -29
- package/dist/executor/executor.d.ts +2 -0
- package/dist/executor/executor.js +65 -14
- package/dist/guid.d.ts +1 -1
- package/dist/guid.js +15 -10
- package/dist/index.d.ts +5 -5
- package/dist/index.js +4 -4
- package/dist/interfaces/agile.d.ts +52 -0
- package/dist/interfaces/agile.js +59 -0
- package/dist/interfaces/errorinfo.d.ts +8 -0
- package/dist/interfaces/errorinfo.js +10 -0
- package/dist/interfaces/index.d.ts +2 -1
- package/dist/interfaces/index.js +2 -1
- package/dist/interfaces/typeinfo.js +3 -0
- package/dist/low-level/iids.d.ts +14 -0
- package/dist/low-level/iids.js +14 -0
- package/dist/low-level/index.d.ts +2 -2
- package/dist/low-level/index.js +1 -1
- package/dist/low-level/native.d.ts +23 -0
- package/dist/low-level/native.js +53 -0
- package/dist/low-level/registry.js +4 -2
- package/dist/low-level/vtable.d.ts +10 -0
- package/dist/low-level/vtable.js +32 -5
- package/dist/pointer.d.ts +2 -0
- package/dist/pointer.js +154 -84
- package/dist/pump.d.ts +32 -8
- package/dist/pump.js +87 -15
- package/dist/runtime.d.ts +6 -0
- package/dist/runtime.js +15 -1
- package/dist/server/automation.js +3 -3
- package/dist/server/comobject.d.ts +53 -0
- package/dist/server/comobject.js +319 -27
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +1 -1
- package/dist/server/marshal.d.ts +1 -0
- package/dist/server/marshal.js +16 -8
- package/dist/server/register.js +8 -6
- package/dist/tracking.d.ts +2 -0
- package/dist/tracking.js +13 -0
- package/package.json +6 -2
package/dist/commethod.js
CHANGED
|
@@ -8,7 +8,7 @@ import { Typed, VARIANT, borrowReference, comPointer, comReference, createSafeAr
|
|
|
8
8
|
import { checkHRESULT } from "./errors.js";
|
|
9
9
|
import { GUID, toGUID } from "./guid.js";
|
|
10
10
|
import { HRESULT, derivesFrom } from "./interface.js";
|
|
11
|
-
import { allocBSTR, ole32, oleaut32, readBSTR, takeCoTaskMemString } from "./low-level/native.js";
|
|
11
|
+
import { allocBSTR, flushMirror, isAggregateClass, isPointerType, ole32, oleaut32, readBSTR, refreshMirror, takeCoTaskMemString } from "./low-level/native.js";
|
|
12
12
|
import { rawAddRef, rawQueryInterface, rawRelease } from "./low-level/vtable.js";
|
|
13
13
|
import { COMPointer } from "./pointer.js";
|
|
14
14
|
export const BSTR = Object.freeze({ __comtype: "BSTR" });
|
|
@@ -40,15 +40,9 @@ export function helpstring(text) {
|
|
|
40
40
|
function isInterfaceDef(type) {
|
|
41
41
|
return typeof type === "object" && type !== null && "Pointer" in type && "iid" in type && "slots" in type;
|
|
42
42
|
}
|
|
43
|
-
function isStructureClass(type) {
|
|
44
|
-
return typeof type === "function" && type.prototype instanceof ctypes.Structure;
|
|
45
|
-
}
|
|
46
43
|
function isSimpleType(type) {
|
|
47
44
|
return typeof type === "function" && type._isSimpleCData === true;
|
|
48
45
|
}
|
|
49
|
-
function isPointerType(type) {
|
|
50
|
-
return typeof type === "object" && type !== null && "_type_" in type && "_pointerTo" in type;
|
|
51
|
-
}
|
|
52
46
|
function pointerHolder(arg, read, release, initial = null) {
|
|
53
47
|
const out = new ctypes.c_void_p(initial ?? 0n);
|
|
54
48
|
let consumed = false;
|
|
@@ -246,9 +240,20 @@ function structureCodec(type) {
|
|
|
246
240
|
input: make,
|
|
247
241
|
output(initial, hasInitial) {
|
|
248
242
|
const holder = new type();
|
|
249
|
-
if (hasInitial && initial !== undefined)
|
|
250
|
-
|
|
251
|
-
|
|
243
|
+
if (hasInitial && initial !== undefined) {
|
|
244
|
+
const src = make(initial);
|
|
245
|
+
refreshMirror(src._buffer);
|
|
246
|
+
holder._buffer.set(src._buffer);
|
|
247
|
+
flushMirror(holder._buffer);
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
arg: holder._buffer,
|
|
251
|
+
read: () => {
|
|
252
|
+
refreshMirror(holder._buffer);
|
|
253
|
+
return holder;
|
|
254
|
+
},
|
|
255
|
+
free: () => undefined,
|
|
256
|
+
};
|
|
252
257
|
},
|
|
253
258
|
};
|
|
254
259
|
}
|
|
@@ -274,7 +279,7 @@ function isRawPointerValue(value) {
|
|
|
274
279
|
/** `POINTER(T)` input: raw pointers pass through, plain values are copied to a temporary `T`. */
|
|
275
280
|
function pointerCodec(type) {
|
|
276
281
|
const base = type._type_;
|
|
277
|
-
const target = base === VARIANT || isSimpleType(base) ||
|
|
282
|
+
const target = base === VARIANT || isSimpleType(base) || isAggregateClass(base) ? codecFor(base) : null;
|
|
278
283
|
return {
|
|
279
284
|
ffi: type,
|
|
280
285
|
input(value, cleanup, self) {
|
|
@@ -319,7 +324,7 @@ export function classifyParamType(type) {
|
|
|
319
324
|
return { kind: "variant" };
|
|
320
325
|
if (isInterfaceDef(type))
|
|
321
326
|
return { kind: "interface", iface: type };
|
|
322
|
-
if (
|
|
327
|
+
if (isAggregateClass(type))
|
|
323
328
|
return { kind: "struct", type };
|
|
324
329
|
if (isSimpleType(type))
|
|
325
330
|
return { kind: "simple", type };
|
|
@@ -420,6 +425,7 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
420
425
|
defaultValue: spec[3],
|
|
421
426
|
});
|
|
422
427
|
});
|
|
428
|
+
const paramKinds = infos.map((info) => (info.lcid ? null : classifyParamType(info.type)));
|
|
423
429
|
const codecs = infos.map((info) => {
|
|
424
430
|
if (info.lcid)
|
|
425
431
|
return lcidCodec;
|
|
@@ -432,6 +438,30 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
432
438
|
const inputs = infos.filter((info) => info.input);
|
|
433
439
|
const required = inputs.filter((info) => !info.optional).length;
|
|
434
440
|
const hasOutputs = infos.some((info) => info.output);
|
|
441
|
+
const lowerNames = infos.map((info) => info.name.toLowerCase());
|
|
442
|
+
const validInputNames = new Set(inputs.map((info) => info.name.toLowerCase()));
|
|
443
|
+
const hasInterfaceOutputs = infos.some((info, index) => {
|
|
444
|
+
if (!info.output)
|
|
445
|
+
return false;
|
|
446
|
+
const k = paramKinds[index];
|
|
447
|
+
if (!k)
|
|
448
|
+
return false;
|
|
449
|
+
if (k.kind === "interface" || k.kind === "variant" || k.kind === "safearray")
|
|
450
|
+
return true;
|
|
451
|
+
return k.kind === "pointer" && (k.base.kind === "interface" || k.base.kind === "variant" || k.base.kind === "safearray");
|
|
452
|
+
});
|
|
453
|
+
// Fast-path eligibility:
|
|
454
|
+
// 1) Zero-param method (`Ping()`, `Invoke()`, `Close()`): no allocation at all.
|
|
455
|
+
const isZeroParam = infos.length === 0;
|
|
456
|
+
// 2) Pure positional simple-scalar inputs with a single trailing pure-out simple scalar (`Add(a, b) -> int32`).
|
|
457
|
+
const singleOutSimpleType = !isZeroParam &&
|
|
458
|
+
infos.length >= 1 &&
|
|
459
|
+
infos[infos.length - 1].output &&
|
|
460
|
+
!infos[infos.length - 1].input &&
|
|
461
|
+
paramKinds[infos.length - 1]?.kind === "simple" &&
|
|
462
|
+
infos.slice(0, -1).every((info, i) => info.input && !info.output && !info.lcid && paramKinds[i]?.kind === "simple")
|
|
463
|
+
? paramKinds[infos.length - 1].type
|
|
464
|
+
: null;
|
|
435
465
|
// Converts the arguments; on failure, releases what was already converted.
|
|
436
466
|
const prepare = (args, self) => {
|
|
437
467
|
let positional = args;
|
|
@@ -439,17 +469,21 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
439
469
|
const last = args[args.length - 1];
|
|
440
470
|
if (last instanceof NamedArguments) {
|
|
441
471
|
positional = args.slice(0, -1);
|
|
442
|
-
byName = new Map(
|
|
443
|
-
for (const key of
|
|
444
|
-
|
|
445
|
-
|
|
472
|
+
byName = new Map();
|
|
473
|
+
for (const [key, value] of Object.entries(last.values)) {
|
|
474
|
+
const lower = key.toLowerCase();
|
|
475
|
+
if (!validInputNames.has(lower))
|
|
476
|
+
throw new TypeError(`${name}: unknown argument ${JSON.stringify(lower)}`);
|
|
477
|
+
byName.set(lower, value);
|
|
478
|
+
}
|
|
446
479
|
}
|
|
447
480
|
if (positional.length > inputs.length)
|
|
448
481
|
throw new TypeError(`${name}: expected at most ${inputs.length} arguments, got ${positional.length}`);
|
|
449
|
-
const prepared = { native:
|
|
482
|
+
const prepared = { native: new Array(infos.length), holders: [], cleanup: [] };
|
|
450
483
|
try {
|
|
451
484
|
let position = 0;
|
|
452
|
-
infos.
|
|
485
|
+
for (let index = 0; index < infos.length; index++) {
|
|
486
|
+
const info = infos[index];
|
|
453
487
|
const codec = codecs[index];
|
|
454
488
|
let value;
|
|
455
489
|
let supplied = false;
|
|
@@ -457,8 +491,8 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
457
491
|
if (position < positional.length)
|
|
458
492
|
value = positional[position];
|
|
459
493
|
position++;
|
|
460
|
-
if (value === undefined && byName
|
|
461
|
-
value = byName.get(
|
|
494
|
+
if (value === undefined && byName !== null && byName.has(lowerNames[index]))
|
|
495
|
+
value = byName.get(lowerNames[index]);
|
|
462
496
|
supplied = value !== undefined;
|
|
463
497
|
if (!supplied && info.hasDefault) {
|
|
464
498
|
value = info.defaultValue;
|
|
@@ -470,12 +504,12 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
470
504
|
if (info.output) {
|
|
471
505
|
const holder = codec.output(value, info.input && supplied, self);
|
|
472
506
|
prepared.holders.push(holder);
|
|
473
|
-
prepared.native
|
|
507
|
+
prepared.native[index] = holder.arg;
|
|
474
508
|
}
|
|
475
509
|
else {
|
|
476
|
-
prepared.native
|
|
510
|
+
prepared.native[index] = codec.input(value, prepared.cleanup, self);
|
|
477
511
|
}
|
|
478
|
-
}
|
|
512
|
+
}
|
|
479
513
|
return prepared;
|
|
480
514
|
}
|
|
481
515
|
catch (error) {
|
|
@@ -486,49 +520,109 @@ export function COMMETHOD(idlflags, restype, name, ...params) {
|
|
|
486
520
|
const finish = (prepared, result) => {
|
|
487
521
|
if (!hasOutputs)
|
|
488
522
|
return result;
|
|
489
|
-
const
|
|
490
|
-
|
|
523
|
+
const holders = prepared.holders;
|
|
524
|
+
if (holders.length === 1)
|
|
525
|
+
return wrapOutput(holders[0].read());
|
|
526
|
+
return holders.map((holder) => wrapOutput(holder.read()));
|
|
491
527
|
};
|
|
492
|
-
const
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
528
|
+
const EMPTY_ARGS = Object.freeze([]);
|
|
529
|
+
const EMPTY_RESULTS = Object.freeze([]);
|
|
530
|
+
const marshal = isZeroParam
|
|
531
|
+
? (fn, address, args) => {
|
|
532
|
+
if (args.length === 0)
|
|
533
|
+
return fn(address);
|
|
534
|
+
const prepared = prepare(args, null);
|
|
535
|
+
try {
|
|
536
|
+
return fn(address, ...prepared.native);
|
|
537
|
+
}
|
|
538
|
+
finally {
|
|
539
|
+
release(prepared);
|
|
540
|
+
}
|
|
499
541
|
}
|
|
500
|
-
|
|
542
|
+
: singleOutSimpleType
|
|
543
|
+
? (fn, address, args, self) => {
|
|
544
|
+
if (args.length === inputs.length && (args.length === 0 || !(args[args.length - 1] instanceof NamedArguments))) {
|
|
545
|
+
let allDefined = true;
|
|
546
|
+
for (let i = 0; i < args.length; i++) {
|
|
547
|
+
if (args[i] === undefined) {
|
|
548
|
+
allDefined = false;
|
|
549
|
+
break;
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
if (allDefined) {
|
|
553
|
+
const out = new singleOutSimpleType();
|
|
554
|
+
const ref = ctypes.byref(out);
|
|
555
|
+
if (args.length === 0)
|
|
556
|
+
fn(address, ref);
|
|
557
|
+
else if (args.length === 1)
|
|
558
|
+
fn(address, args[0], ref);
|
|
559
|
+
else if (args.length === 2)
|
|
560
|
+
fn(address, args[0], args[1], ref);
|
|
561
|
+
else
|
|
562
|
+
fn(address, ...args, ref);
|
|
563
|
+
return wrapOutput(out.value);
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
const prepared = prepare(args, self);
|
|
567
|
+
try {
|
|
568
|
+
return finish(prepared, fn(address, ...prepared.native));
|
|
569
|
+
}
|
|
570
|
+
finally {
|
|
571
|
+
release(prepared);
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
: (fn, address, args, self) => {
|
|
575
|
+
const prepared = prepare(args, self);
|
|
576
|
+
try {
|
|
577
|
+
return finish(prepared, fn(address, ...prepared.native));
|
|
578
|
+
}
|
|
579
|
+
finally {
|
|
580
|
+
release(prepared);
|
|
581
|
+
}
|
|
582
|
+
};
|
|
501
583
|
// The converted arguments stay alive (and owned here) until the call completes.
|
|
502
584
|
// Promise steps instead of an async function: this runs for every method call.
|
|
503
|
-
const marshalAsync =
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
pending = run(prepared.native, results);
|
|
515
|
-
}
|
|
516
|
-
catch (error) {
|
|
517
|
-
release(prepared);
|
|
518
|
-
return Promise.reject(error);
|
|
585
|
+
const marshalAsync = isZeroParam
|
|
586
|
+
? (run, args) => {
|
|
587
|
+
if (args.length === 0)
|
|
588
|
+
return run(EMPTY_ARGS, EMPTY_RESULTS);
|
|
589
|
+
try {
|
|
590
|
+
prepare(args, null);
|
|
591
|
+
}
|
|
592
|
+
catch (error) {
|
|
593
|
+
return Promise.reject(error);
|
|
594
|
+
}
|
|
595
|
+
return run(EMPTY_ARGS, EMPTY_RESULTS);
|
|
519
596
|
}
|
|
520
|
-
|
|
597
|
+
: (run, args, self) => {
|
|
598
|
+
let prepared;
|
|
599
|
+
let pending;
|
|
521
600
|
try {
|
|
522
|
-
|
|
601
|
+
prepared = prepare(args, self);
|
|
523
602
|
}
|
|
524
|
-
|
|
603
|
+
catch (error) {
|
|
604
|
+
return Promise.reject(error);
|
|
605
|
+
}
|
|
606
|
+
try {
|
|
607
|
+
const results = hasInterfaceOutputs ? prepared.holders.flatMap((holder) => (holder.result ? [{ location: ctypes.addressof(holder.arg), ...holder.result }] : [])) : EMPTY_RESULTS;
|
|
608
|
+
pending = run(prepared.native, results);
|
|
609
|
+
}
|
|
610
|
+
catch (error) {
|
|
525
611
|
release(prepared);
|
|
612
|
+
return Promise.reject(error);
|
|
526
613
|
}
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
614
|
+
return pending.then((value) => {
|
|
615
|
+
try {
|
|
616
|
+
return finish(prepared, value);
|
|
617
|
+
}
|
|
618
|
+
finally {
|
|
619
|
+
release(prepared);
|
|
620
|
+
}
|
|
621
|
+
}, (error) => {
|
|
622
|
+
release(prepared);
|
|
623
|
+
throw error;
|
|
624
|
+
});
|
|
625
|
+
};
|
|
532
626
|
const methodName = kind === "propget" ? `_get_${name}` : kind === "propput" ? `_set_${name}` : kind === "propputref" ? `_setref_${name}` : name;
|
|
533
627
|
return Object.freeze({
|
|
534
628
|
name: methodName,
|
package/dist/errorinfo.d.ts
CHANGED
|
@@ -5,9 +5,16 @@ import type { COMErrorDetails } from "./errors.ts";
|
|
|
5
5
|
* Returns `null` when no error object is set.
|
|
6
6
|
*/
|
|
7
7
|
export declare function takeErrorInfo(): COMErrorDetails | null;
|
|
8
|
+
/**
|
|
9
|
+
* Take the current thread's `IRestrictedErrorInfo` (`GetRestrictedErrorInfo`),
|
|
10
|
+
* clearing it. When `expectedHresult` is provided, returns `null` if the stored
|
|
11
|
+
* restricted error does not match that HRESULT.
|
|
12
|
+
*/
|
|
13
|
+
export declare function takeRestrictedErrorInfo(expectedHresult?: number): COMErrorDetails | null;
|
|
8
14
|
/**
|
|
9
15
|
* Error details for a failed method of interface `iid` called on `object`:
|
|
10
|
-
*
|
|
11
|
-
* as Python ctypes/comtypes do
|
|
16
|
+
* reads `IErrorInfo` when the object declares `ISupportErrorInfo` support for
|
|
17
|
+
* `iid` (as Python ctypes/comtypes do), and falls back to `IRestrictedErrorInfo`
|
|
18
|
+
* (WinRT / modern COM) when `ISupportErrorInfo` is not implemented.
|
|
12
19
|
*/
|
|
13
|
-
export declare function errorInfoFor(object: bigint, iid: GUID): COMErrorDetails | null;
|
|
20
|
+
export declare function errorInfoFor(object: bigint, iid: GUID, hresult?: number): COMErrorDetails | null;
|
package/dist/errorinfo.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import * as ctypes from "node-ctypes";
|
|
2
2
|
import { GUID } from "./guid.js";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { IID_ISupportErrorInfo } from "./low-level/iids.js";
|
|
4
|
+
import { combase, oleaut32, takeBSTR } from "./low-level/native.js";
|
|
5
|
+
import { functionCache, PointerArgAt, rawQueryInterface, rawRelease, vtableEntry } from "./low-level/vtable.js";
|
|
5
6
|
const { c_int32, c_uint32, c_void_p } = ctypes;
|
|
6
|
-
const
|
|
7
|
-
// IErrorInfo getters and ISupportErrorInfo::InterfaceSupportsErrorInfo share
|
|
8
|
-
// the HRESULT (this, pointer) signature.
|
|
9
|
-
const PointerArgAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p));
|
|
7
|
+
const RestrictedDetailsAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p, c_void_p, c_void_p));
|
|
10
8
|
function readBSTR(errorInfo, slot) {
|
|
11
9
|
const out = new c_void_p();
|
|
12
10
|
if (PointerArgAt(vtableEntry(errorInfo, slot))(errorInfo, ctypes.byref(out)) < 0)
|
|
@@ -39,24 +37,74 @@ export function takeErrorInfo() {
|
|
|
39
37
|
rawRelease(errorInfo);
|
|
40
38
|
}
|
|
41
39
|
}
|
|
40
|
+
/**
|
|
41
|
+
* Take the current thread's `IRestrictedErrorInfo` (`GetRestrictedErrorInfo`),
|
|
42
|
+
* clearing it. When `expectedHresult` is provided, returns `null` if the stored
|
|
43
|
+
* restricted error does not match that HRESULT.
|
|
44
|
+
*/
|
|
45
|
+
export function takeRestrictedErrorInfo(expectedHresult) {
|
|
46
|
+
const api = combase();
|
|
47
|
+
if (!api)
|
|
48
|
+
return null;
|
|
49
|
+
const out = new c_void_p();
|
|
50
|
+
if (api.GetRestrictedErrorInfo(ctypes.byref(out)) !== 0 || !out.value)
|
|
51
|
+
return null;
|
|
52
|
+
const restricted = out.value;
|
|
53
|
+
try {
|
|
54
|
+
const desc = new c_void_p();
|
|
55
|
+
const err = new c_int32();
|
|
56
|
+
const restrictedDesc = new c_void_p();
|
|
57
|
+
const capabilitySid = new c_void_p();
|
|
58
|
+
const hr = RestrictedDetailsAt(vtableEntry(restricted, 3))(restricted, ctypes.byref(desc), ctypes.byref(err), ctypes.byref(restrictedDesc), ctypes.byref(capabilitySid));
|
|
59
|
+
const descriptionText = hr >= 0 ? takeBSTR(desc.value) : null;
|
|
60
|
+
const restrictedText = hr >= 0 ? takeBSTR(restrictedDesc.value) : null;
|
|
61
|
+
const capabilityText = hr >= 0 ? takeBSTR(capabilitySid.value) : null;
|
|
62
|
+
if (hr < 0)
|
|
63
|
+
return null;
|
|
64
|
+
if (expectedHresult !== undefined && err.value !== expectedHresult)
|
|
65
|
+
return null;
|
|
66
|
+
const referenceText = readBSTR(restricted, 4);
|
|
67
|
+
const trimmedRestricted = restrictedText?.trim() || null;
|
|
68
|
+
const trimmedDescription = descriptionText?.trim() || null;
|
|
69
|
+
return {
|
|
70
|
+
guid: null,
|
|
71
|
+
source: null,
|
|
72
|
+
description: trimmedRestricted ?? trimmedDescription,
|
|
73
|
+
helpFile: null,
|
|
74
|
+
helpContext: 0,
|
|
75
|
+
restrictedDescription: trimmedRestricted,
|
|
76
|
+
capabilitySid: capabilityText?.trim() || null,
|
|
77
|
+
reference: referenceText?.trim() || null,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
catch {
|
|
81
|
+
return null;
|
|
82
|
+
}
|
|
83
|
+
finally {
|
|
84
|
+
rawRelease(restricted);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
42
87
|
/**
|
|
43
88
|
* Error details for a failed method of interface `iid` called on `object`:
|
|
44
|
-
*
|
|
45
|
-
* as Python ctypes/comtypes do
|
|
89
|
+
* reads `IErrorInfo` when the object declares `ISupportErrorInfo` support for
|
|
90
|
+
* `iid` (as Python ctypes/comtypes do), and falls back to `IRestrictedErrorInfo`
|
|
91
|
+
* (WinRT / modern COM) when `ISupportErrorInfo` is not implemented.
|
|
46
92
|
*/
|
|
47
|
-
export function errorInfoFor(object, iid) {
|
|
93
|
+
export function errorInfoFor(object, iid, hresult) {
|
|
48
94
|
try {
|
|
49
95
|
const [hr, support] = rawQueryInterface(object, IID_ISupportErrorInfo);
|
|
50
|
-
if (hr
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
96
|
+
if (hr >= 0 && support) {
|
|
97
|
+
let supported;
|
|
98
|
+
try {
|
|
99
|
+
supported = PointerArgAt(vtableEntry(support, 3))(support, iid._buffer) === 0;
|
|
100
|
+
}
|
|
101
|
+
finally {
|
|
102
|
+
rawRelease(support);
|
|
103
|
+
}
|
|
104
|
+
if (supported)
|
|
105
|
+
return takeErrorInfo();
|
|
58
106
|
}
|
|
59
|
-
return
|
|
107
|
+
return takeRestrictedErrorInfo(hresult);
|
|
60
108
|
}
|
|
61
109
|
catch {
|
|
62
110
|
return null;
|
package/dist/errors.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GUID } from "./guid.ts";
|
|
2
|
-
/** Rich error information from `IErrorInfo` (Python: the `details` tuple). */
|
|
2
|
+
/** Rich error information from `IErrorInfo` or `IRestrictedErrorInfo` (Python: the `details` tuple). */
|
|
3
3
|
export interface COMErrorDetails {
|
|
4
4
|
readonly description: string | null;
|
|
5
5
|
readonly source: string | null;
|
|
@@ -10,6 +10,12 @@ export interface COMErrorDetails {
|
|
|
10
10
|
readonly scode?: number;
|
|
11
11
|
/** Index of the offending argument for `DISP_E_TYPEMISMATCH` / `DISP_E_PARAMNOTFOUND`. */
|
|
12
12
|
readonly argumentIndex?: number;
|
|
13
|
+
/** Detailed restricted error description from `IRestrictedErrorInfo` (WinRT / modern COM), when available. */
|
|
14
|
+
readonly restrictedDescription?: string | null;
|
|
15
|
+
/** Missing capability SID reported by `IRestrictedErrorInfo`, when available. */
|
|
16
|
+
readonly capabilitySid?: string | null;
|
|
17
|
+
/** Diagnostic reference string from `IRestrictedErrorInfo::GetReference`, when available. */
|
|
18
|
+
readonly reference?: string | null;
|
|
13
19
|
}
|
|
14
20
|
export interface COMErrorOptions {
|
|
15
21
|
/** System message for the HRESULT; defaults to `FormatMessageW`. */
|
|
@@ -13,10 +13,14 @@ export declare function isAgile(address: bigint): boolean;
|
|
|
13
13
|
export declare function releaseOnExecutor(address: bigint, generation: number): void;
|
|
14
14
|
/**
|
|
15
15
|
* A reference to the home pointer `address` usable on the executor (owned by
|
|
16
|
-
* the executor), through the Global Interface Table
|
|
17
|
-
* interface cannot be
|
|
16
|
+
* the executor), through the Global Interface Table, together with the
|
|
17
|
+
* executor generation that owns it; `null` when the interface cannot be
|
|
18
|
+
* marshaled (no proxy/stub).
|
|
18
19
|
*/
|
|
19
|
-
export declare function referenceForExecutor(address: bigint, iid: GUID): Promise<
|
|
20
|
+
export declare function referenceForExecutor(address: bigint, iid: GUID): Promise<{
|
|
21
|
+
readonly address: bigint;
|
|
22
|
+
readonly generation: number;
|
|
23
|
+
} | null>;
|
|
20
24
|
/** Memory receiving an interface result: an `out` interface pointer, a VARIANT or a `SAFEARRAY*`. */
|
|
21
25
|
export interface ResultLocation {
|
|
22
26
|
readonly location: bigint;
|
|
@@ -10,39 +10,39 @@ import { APTTYPE_MTA, CLSCTX_INPROC_SERVER } from "../constants.js";
|
|
|
10
10
|
import { COMError } from "../errors.js";
|
|
11
11
|
import { GUID } from "../guid.js";
|
|
12
12
|
import { CO_E_NOTINITIALIZED } from "../hresult.js";
|
|
13
|
+
import { CLSID_InProcFreeMarshaler, CLSID_StdGlobalInterfaceTable, IID_IAgileObject, IID_IClientSecurity, IID_IDispatch, IID_IGlobalInterfaceTable, IID_IMarshal, IID_IUnknown } from "../low-level/iids.js";
|
|
13
14
|
import { ole32, oleaut32 } from "../low-level/native.js";
|
|
14
|
-
import { functionCache, rawQueryInterface, rawRelease, vtableEntry } from "../low-level/vtable.js";
|
|
15
|
-
import { apartmentGeneration } from "../tracking.js";
|
|
16
|
-
import { executorAlive, executorGeneration, executorTarget } from "./executor.js";
|
|
15
|
+
import { functionCache, QueryInterfaceAt, rawQueryInterface, rawRelease, RefCountAt, vtableEntry } from "../low-level/vtable.js";
|
|
16
|
+
import { apartmentGeneration, initCount, onApartmentTeardown } from "../tracking.js";
|
|
17
|
+
import { executorAlive, executorGeneration, executorTarget, onExecutorStopping } from "./executor.js";
|
|
17
18
|
const { c_int32, c_uint16, c_uint32, c_void_p } = ctypes;
|
|
18
|
-
const CLSID_StdGlobalInterfaceTable = new GUID("{00000323-0000-0000-C000-000000000046}");
|
|
19
|
-
const IID_IGlobalInterfaceTable = new GUID("{00000146-0000-0000-C000-000000000046}");
|
|
20
|
-
const IID_IAgileObject = new GUID("{94EA2B94-E9CC-49E0-C0FF-EE64CA8F5B90}");
|
|
21
|
-
const IID_IMarshal = new GUID("{00000003-0000-0000-C000-000000000046}");
|
|
22
|
-
const IID_IClientSecurity = new GUID("{0000013D-0000-0000-C000-000000000046}");
|
|
23
|
-
const IID_IDispatch = new GUID("{00020400-0000-0000-C000-000000000046}");
|
|
24
|
-
const IID_IUnknown = new GUID("{00000000-0000-0000-C000-000000000046}");
|
|
25
|
-
const CLSID_InProcFreeMarshaler = new GUID("{0000033A-0000-0000-C000-000000000046}");
|
|
26
19
|
const MSHCTX_INPROC = 3;
|
|
27
20
|
const MSHLFLAGS_NORMAL = 0;
|
|
28
21
|
const RegisterAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p, c_void_p));
|
|
29
22
|
const RevokeAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_uint32));
|
|
30
23
|
const GetAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_uint32, c_void_p, c_void_p));
|
|
31
|
-
const QueryInterfaceAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p));
|
|
32
|
-
const ReleaseAt = functionCache(() => ctypes.WINFUNCTYPE(c_uint32, c_void_p));
|
|
33
24
|
const GetUnmarshalClassAt = functionCache(() => ctypes.WINFUNCTYPE(c_int32, c_void_p, c_void_p, c_void_p, c_uint32, c_void_p, c_uint32, c_void_p));
|
|
25
|
+
let cachedHomeApartment = null;
|
|
34
26
|
/**
|
|
35
27
|
* Apartment of the home thread: `mta` (explicit or implicit: shared with the
|
|
36
28
|
* executor), `sta` (single-threaded, also NA/main STA), or `none` when COM is
|
|
37
29
|
* not initialized there (objects are then called on the home thread).
|
|
38
30
|
*/
|
|
39
31
|
export function homeApartment() {
|
|
32
|
+
const count = initCount();
|
|
33
|
+
const gen = apartmentGeneration();
|
|
34
|
+
if (count > 0 && cachedHomeApartment !== null && cachedHomeApartment.count === count && cachedHomeApartment.generation === gen) {
|
|
35
|
+
return cachedHomeApartment.kind;
|
|
36
|
+
}
|
|
40
37
|
const type = new c_int32();
|
|
41
38
|
const qualifier = new c_int32();
|
|
42
39
|
const hr = ole32().CoGetApartmentType(ctypes.byref(type), ctypes.byref(qualifier));
|
|
43
40
|
if (hr === CO_E_NOTINITIALIZED || hr < 0)
|
|
44
41
|
return "none";
|
|
45
|
-
|
|
42
|
+
const kind = type.value === APTTYPE_MTA ? "mta" : "sta";
|
|
43
|
+
if (count > 0)
|
|
44
|
+
cachedHomeApartment = { kind, count, generation: gen };
|
|
45
|
+
return kind;
|
|
46
46
|
}
|
|
47
47
|
function supports(address, iid) {
|
|
48
48
|
const [hr, pointer] = rawQueryInterface(address, iid);
|
|
@@ -76,6 +76,14 @@ export function isAgile(address) {
|
|
|
76
76
|
}
|
|
77
77
|
// The Global Interface Table of the home thread, per apartment generation.
|
|
78
78
|
let homeTable = null;
|
|
79
|
+
onApartmentTeardown(() => {
|
|
80
|
+
cachedHomeApartment = null;
|
|
81
|
+
if (homeTable) {
|
|
82
|
+
const addr = homeTable.address;
|
|
83
|
+
homeTable = null;
|
|
84
|
+
rawRelease(addr);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
79
87
|
function homeGlobalTable() {
|
|
80
88
|
const generation = apartmentGeneration();
|
|
81
89
|
if (homeTable?.generation === generation)
|
|
@@ -89,35 +97,61 @@ function homeGlobalTable() {
|
|
|
89
97
|
}
|
|
90
98
|
// The executor's own table, per executor generation (created on the executor).
|
|
91
99
|
let executorTable = null;
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
100
|
+
onExecutorStopping((stopping, gen) => {
|
|
101
|
+
if (executorTable?.generation === gen) {
|
|
102
|
+
const entry = executorTable;
|
|
103
|
+
executorTable = null;
|
|
104
|
+
if (entry.resolved) {
|
|
105
|
+
RefCountAt(vtableEntry(entry.resolved, 2))
|
|
106
|
+
.callOn(stopping, entry.resolved)
|
|
107
|
+
.catch(() => undefined);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
function executorGlobalTable(target = executorTarget(), gen = executorGeneration()) {
|
|
112
|
+
if (executorTable?.generation === gen)
|
|
96
113
|
return executorTable.address;
|
|
97
114
|
const out = new c_void_p();
|
|
98
|
-
const
|
|
115
|
+
const entry = {
|
|
116
|
+
address: null,
|
|
117
|
+
resolved: null,
|
|
118
|
+
generation: gen,
|
|
119
|
+
};
|
|
120
|
+
entry.address = ole32()
|
|
99
121
|
.CoCreateInstance.callOn(target, CLSID_StdGlobalInterfaceTable._buffer, null, CLSCTX_INPROC_SERVER, IID_IGlobalInterfaceTable._buffer, ctypes.byref(out))
|
|
100
122
|
.then((hr) => {
|
|
101
123
|
if (hr < 0 || !out.value)
|
|
102
124
|
throw new COMError(hr, { method: "CoCreateInstance(StdGlobalInterfaceTable)" });
|
|
125
|
+
if (executorTable !== entry || !executorAlive(gen)) {
|
|
126
|
+
RefCountAt(vtableEntry(out.value, 2))
|
|
127
|
+
.callOn(target, out.value)
|
|
128
|
+
.catch(() => undefined);
|
|
129
|
+
throw new Error("COM executor stopped while initializing GlobalInterfaceTable");
|
|
130
|
+
}
|
|
131
|
+
entry.resolved = out.value;
|
|
103
132
|
return out.value;
|
|
133
|
+
}, (err) => {
|
|
134
|
+
if (executorTable === entry)
|
|
135
|
+
executorTable = null;
|
|
136
|
+
throw err;
|
|
104
137
|
});
|
|
105
|
-
executorTable =
|
|
106
|
-
return address;
|
|
138
|
+
executorTable = entry;
|
|
139
|
+
return entry.address;
|
|
107
140
|
}
|
|
108
141
|
/** Release a reference owned by the executor thread, after the calls already queued. */
|
|
109
142
|
export function releaseOnExecutor(address, generation) {
|
|
110
143
|
// An executor that has stopped took its references with its apartment.
|
|
111
144
|
if (!executorAlive(generation))
|
|
112
145
|
return;
|
|
113
|
-
|
|
146
|
+
RefCountAt(vtableEntry(address, 2))
|
|
114
147
|
.callOn(executorTarget(), address)
|
|
115
148
|
.catch(() => undefined);
|
|
116
149
|
}
|
|
117
150
|
/**
|
|
118
151
|
* A reference to the home pointer `address` usable on the executor (owned by
|
|
119
|
-
* the executor), through the Global Interface Table
|
|
120
|
-
* interface cannot be
|
|
152
|
+
* the executor), through the Global Interface Table, together with the
|
|
153
|
+
* executor generation that owns it; `null` when the interface cannot be
|
|
154
|
+
* marshaled (no proxy/stub).
|
|
121
155
|
*/
|
|
122
156
|
export async function referenceForExecutor(address, iid) {
|
|
123
157
|
const table = homeGlobalTable();
|
|
@@ -125,10 +159,12 @@ export async function referenceForExecutor(address, iid) {
|
|
|
125
159
|
if (RegisterAt(vtableEntry(table, 3))(table, address, iid._buffer, ctypes.byref(cookie)) < 0)
|
|
126
160
|
return null;
|
|
127
161
|
try {
|
|
128
|
-
const
|
|
162
|
+
const target = executorTarget();
|
|
163
|
+
const gen = executorGeneration();
|
|
164
|
+
const executorTableAddress = await executorGlobalTable(target, gen);
|
|
129
165
|
const out = new c_void_p();
|
|
130
|
-
const hr = await GetAt(vtableEntry(executorTableAddress, 5)).callOn(
|
|
131
|
-
return hr >= 0 && out.value ? out.value : null;
|
|
166
|
+
const hr = await GetAt(vtableEntry(executorTableAddress, 5)).callOn(target, executorTableAddress, cookie.value, iid._buffer, ctypes.byref(out));
|
|
167
|
+
return hr >= 0 && out.value ? { address: out.value, generation: gen } : null;
|
|
132
168
|
}
|
|
133
169
|
finally {
|
|
134
170
|
RevokeAt(vtableEntry(table, 4))(table, cookie.value);
|
|
@@ -144,7 +180,7 @@ async function handOverPointer(location, offset, interfaceId) {
|
|
|
144
180
|
// Agile objects are valid in both apartments: the reference stays as it is.
|
|
145
181
|
const agile = new c_void_p();
|
|
146
182
|
if ((await QueryInterfaceAt(vtableEntry(address, 0)).callOn(target, address, IID_IAgileObject._buffer, ctypes.byref(agile))) >= 0 && agile.value) {
|
|
147
|
-
void
|
|
183
|
+
void RefCountAt(vtableEntry(agile.value, 2))
|
|
148
184
|
.callOn(target, agile.value)
|
|
149
185
|
.catch(() => undefined);
|
|
150
186
|
return;
|
|
@@ -153,7 +189,7 @@ async function handOverPointer(location, offset, interfaceId) {
|
|
|
153
189
|
const cookie = new c_uint32();
|
|
154
190
|
const registered = await RegisterAt(vtableEntry(executorTableAddress, 3)).callOn(target, executorTableAddress, address, interfaceId._buffer, ctypes.byref(cookie));
|
|
155
191
|
// The executor's reference is no longer needed: the table holds its own.
|
|
156
|
-
void
|
|
192
|
+
void RefCountAt(vtableEntry(address, 2))
|
|
157
193
|
.callOn(target, address)
|
|
158
194
|
.catch(() => undefined);
|
|
159
195
|
if (registered < 0) {
|
|
@@ -37,6 +37,8 @@ export interface CallOptions {
|
|
|
37
37
|
/** Records the call in this operation, which `run()` can cancel (used by node-comtypes' own calls). */
|
|
38
38
|
readonly operation?: Operation;
|
|
39
39
|
}
|
|
40
|
+
/** Register a hook invoked right before an executor runs `CoUninitialize` during `finalize()`. */
|
|
41
|
+
export declare function onExecutorStopping(hook: (stopping: ctypes.Executor, gen: number) => void): void;
|
|
40
42
|
/** The running executor (started on demand), for code that queues its own calls. */
|
|
41
43
|
export declare function executorTarget(): ctypes.Executor;
|
|
42
44
|
/** Counters of the COM executor. Python equivalent: node-python's `python.stats()`. */
|