@node-ctypes/comtypes 0.2.0 → 0.3.1

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.
@@ -3,7 +3,7 @@ import { checkHRESULT } from "../errors.js";
3
3
  import { GUID } from "../guid.js";
4
4
  import { DISP_E_PARAMNOTFOUND } from "../hresult.js";
5
5
  import { derivesFrom, findInterface } from "../interface.js";
6
- import { allocBSTR, oleaut32, POINTER_SIZE, readBSTR, readMemory, writeMemory } from "../low-level/native.js";
6
+ import { allocBSTR, flushMirror, oleaut32, POINTER_SIZE, readBSTR, readMemory, refreshMirror, writeMemory } from "../low-level/native.js";
7
7
  import { rawAddRef, rawQueryInterface, rawRelease } from "../low-level/vtable.js";
8
8
  import { RecordInfo, recordParts } from "./record.js";
9
9
  import { COMPointer, setArgumentInspector, sharedWithExecutor } from "../pointer.js";
@@ -95,6 +95,7 @@ export function writeReference(address, vt, value) {
95
95
  else {
96
96
  writeDecimal(view._buffer, 0, value);
97
97
  view._buffer.writeUInt16LE(0, 0);
98
+ flushMirror(view._buffer);
98
99
  }
99
100
  return;
100
101
  }
@@ -244,6 +245,7 @@ function readScalar(vt, buffer, offset) {
244
245
  }
245
246
  /** Convert the VARIANT in `buffer` to a JavaScript value. COM objects are returned as new owned pointers. */
246
247
  export function readVariant(buffer) {
248
+ refreshMirror(buffer);
247
249
  const vt = buffer.readUInt16LE(0);
248
250
  // VT_RECORD (also by reference): pvRecord and pRecInfo.
249
251
  if ((vt & ~VT_BYREF) === VT_RECORD) {
@@ -270,6 +272,7 @@ export function readVariant(buffer) {
270
272
  }
271
273
  /** Exact text of a `VT_DECIMAL` or `VT_CY` VARIANT (the `value` getter returns a number). */
272
274
  export function readVariantExact(buffer) {
275
+ refreshMirror(buffer);
273
276
  const vt = buffer.readUInt16LE(0);
274
277
  if (vt === VT_DECIMAL)
275
278
  return readDecimal(buffer, 0);
@@ -534,6 +537,7 @@ export function writeVariant(buffer, value) {
534
537
  throw new TypeError("byRef() values are IDispatch arguments, not VARIANT values");
535
538
  if (value instanceof VARIANT) {
536
539
  checkHRESULT(oleaut32().VariantCopy(buffer, value._buffer), "VariantCopy");
540
+ refreshMirror(buffer);
537
541
  return;
538
542
  }
539
543
  const vt = inferVartype(value);
@@ -565,6 +569,7 @@ export function writeVariant(buffer, value) {
565
569
  writeScalar(vt, buffer, DATA, payload);
566
570
  }
567
571
  buffer.writeUInt16LE(vt, 0);
572
+ flushMirror(buffer);
568
573
  }
569
574
  function shapeOf(values, dimensions) {
570
575
  const shape = [];
@@ -675,9 +680,11 @@ export function destroySafeArray(psa) {
675
680
  }
676
681
  /** Release the resources of the VARIANT in `buffer` (`VariantClear`) and reset it to `VT_EMPTY`. */
677
682
  export function clearVariant(buffer) {
683
+ refreshMirror(buffer);
678
684
  if (needsClear(buffer.readUInt16LE(0)))
679
685
  checkHRESULT(oleaut32().VariantClear(buffer), "VariantClear");
680
686
  buffer.fill(0, 0, VARIANT_SIZE);
687
+ flushMirror(buffer);
681
688
  }
682
689
  // ── VARIANT ───────────────────────────────────────────────────────────
683
690
  /**
@@ -757,6 +764,7 @@ export class VARIANT extends ctypes.Structure {
757
764
  checkHRESULT(api.VariantChangeType(this._buffer, this._buffer, flags, vt), "VariantChangeType");
758
765
  else
759
766
  checkHRESULT(api.VariantChangeTypeEx(this._buffer, this._buffer, lcid, flags, vt), "VariantChangeTypeEx");
767
+ refreshMirror(this._buffer);
760
768
  return this;
761
769
  }
762
770
  /** Deep copy (`VariantCopy`). */
package/dist/guid.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as ctypes from "node-ctypes";
2
- import { ole32, takeCoTaskMemString } from "./low-level/native.js";
2
+ import { ole32, refreshMirror, takeCoTaskMemString } from "./low-level/native.js";
3
3
  import { checkHRESULT } from "./errors.js";
4
4
  const GUID_PATTERN = /^\{?([0-9a-f]{8})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{4})-([0-9a-f]{12})\}?$/i;
5
5
  /**
@@ -39,6 +39,8 @@ export class GUID extends ctypes.Structure {
39
39
  Buffer.from(match[4] + match[5], "hex").copy(this._buffer, 8);
40
40
  return;
41
41
  }
42
+ if (init instanceof GUID)
43
+ refreshMirror(init._buffer);
42
44
  const bytes = init instanceof GUID ? init._buffer : init;
43
45
  if (!(bytes instanceof Uint8Array) || bytes.length !== 16)
44
46
  throw new TypeError("GUID requires a GUID string, another GUID or exactly 16 bytes");
@@ -69,13 +71,18 @@ export class GUID extends ctypes.Structure {
69
71
  equals(other) {
70
72
  if (other === null || other === undefined)
71
73
  return false;
72
- return toGUID(other)._buffer.equals(this._buffer);
74
+ const target = toGUID(other);
75
+ refreshMirror(target._buffer);
76
+ refreshMirror(this._buffer);
77
+ return target._buffer.equals(this._buffer);
73
78
  }
74
79
  isNull() {
80
+ refreshMirror(this._buffer);
75
81
  return this._buffer.every((byte) => byte === 0);
76
82
  }
77
83
  /** `{XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX}` (uppercase, as `StringFromGUID2`). */
78
84
  toString() {
85
+ refreshMirror(this._buffer);
79
86
  const hex = (value, width) => value.toString(16).padStart(width, "0");
80
87
  const tail = this._buffer.subarray(8, 16).toString("hex");
81
88
  return `{${hex(this.Data1, 8)}-${hex(this.Data2, 4)}-${hex(this.Data3, 4)}-${tail.slice(0, 4)}-${tail.slice(4)}}`.toUpperCase();
@@ -4,6 +4,7 @@ import { VARIANT } from "../automation/variant.js";
4
4
  import { GUID } from "../guid.js";
5
5
  import { BSTR, COMMETHOD, LPOLESTR, REFGUID } from "../commethod.js";
6
6
  import { COMInterface, HRESULT, STDMETHOD } from "../interface.js";
7
+ import { refreshMirror } from "../low-level/native.js";
7
8
  import { IUnknown } from "./unknown.js";
8
9
  const { POINTER, c_int16, c_int32, c_uint16, c_uint32, c_void_p, c_wchar_p } = ctypes;
9
10
  /** Type of a parameter, return value or variable. `lptdesc` overlays `lpadesc` and `hreftype`. */
@@ -14,6 +15,7 @@ export class TYPEDESC extends ctypes.Structure {
14
15
  ];
15
16
  /** `HREFTYPE` for `VT_USERDEFINED`. */
16
17
  get hreftype() {
18
+ refreshMirror(this._buffer);
17
19
  return this._buffer.readUInt32LE(0);
18
20
  }
19
21
  }
@@ -64,6 +66,7 @@ export class VARDESC extends ctypes.Structure {
64
66
  ["varkind", c_int32],
65
67
  ];
66
68
  get oInst() {
69
+ refreshMirror(this._buffer);
67
70
  return this._buffer.readUInt32LE(ctypes.sizeof(c_void_p) === 8 ? 16 : 8);
68
71
  }
69
72
  }
@@ -80,6 +80,16 @@ export declare function copyStruct<T>(type: new (...args: any[]) => T, address:
80
80
  export declare function readMemory(address: bigint, size: number): Buffer;
81
81
  /** Copy `data` to native memory at `address`. */
82
82
  export declare function writeMemory(address: bigint, data: Uint8Array): void;
83
+ /**
84
+ * Copy current native bytes into `buffer` when external buffers are disabled
85
+ * (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
86
+ */
87
+ export declare function refreshMirror(buffer: Buffer): void;
88
+ /**
89
+ * Copy bytes from `buffer` back to native memory when external buffers are disabled
90
+ * (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
91
+ */
92
+ export declare function flushMirror(buffer: Buffer): void;
83
93
  /** Read a CoTaskMem-allocated wide string and free it. */
84
94
  export declare function takeCoTaskMemString(pointer: bigint | null): string | null;
85
95
  export {};
@@ -129,6 +129,26 @@ export function writeMemory(address, data) {
129
129
  if (data.length)
130
130
  ctypes.memmove(address, data, data.length);
131
131
  }
132
+ /**
133
+ * Copy current native bytes into `buffer` when external buffers are disabled
134
+ * (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
135
+ */
136
+ export function refreshMirror(buffer) {
137
+ if (ctypes.EXTERNAL_BUFFERS || !buffer.length)
138
+ return;
139
+ const copy = Buffer.allocUnsafe(buffer.length);
140
+ ctypes.memmove(copy, buffer, buffer.length);
141
+ copy.copy(buffer);
142
+ }
143
+ /**
144
+ * Copy bytes from `buffer` back to native memory when external buffers are disabled
145
+ * (`NODE_CTYPES_NO_EXTERNAL_BUFFERS=1` / Electron). No-op when `EXTERNAL_BUFFERS` is true.
146
+ */
147
+ export function flushMirror(buffer) {
148
+ if (ctypes.EXTERNAL_BUFFERS || !buffer.length)
149
+ return;
150
+ ctypes.memmove(buffer, Buffer.from(buffer), buffer.length);
151
+ }
132
152
  /** Read a CoTaskMem-allocated wide string and free it. */
133
153
  export function takeCoTaskMemString(pointer) {
134
154
  if (!pointer)
@@ -202,8 +202,8 @@ function releaseIncoming(values) {
202
202
  }
203
203
  function fillException(pExcepInfo, object, error) {
204
204
  const type = object.constructor;
205
+ ctypes.memset(pExcepInfo, 0, ctypes.sizeof(EXCEPINFO));
205
206
  const info = EXCEPINFO.from_address(pExcepInfo);
206
- info._buffer.fill(0);
207
207
  const description = error instanceof COMError ? (error.details?.description ?? error.text) : error instanceof Error ? error.message : String(error);
208
208
  info.bstrSource = allocBSTR(type._reg_progid_ ?? type.name);
209
209
  info.bstrDescription = allocBSTR(description);
@@ -8,7 +8,7 @@ import { checkHRESULT } from "../errors.js";
8
8
  import { GUID, toGUID } from "../guid.js";
9
9
  import { DISP_E_PARAMNOTFOUND } from "../hresult.js";
10
10
  import { derivesFrom } from "../interface.js";
11
- import { allocBSTR, ole32, oleaut32, readBSTR, readMemory, writeMemory } from "../low-level/native.js";
11
+ import { allocBSTR, flushMirror, ole32, oleaut32, readBSTR, readMemory, writeMemory } 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
  // Server side of the parameter conversions: what a JavaScript implementation
@@ -172,8 +172,10 @@ export function encodeOutput(kind, address, value, inout, self) {
172
172
  const view = VARIANT.from_address(address);
173
173
  if (inout)
174
174
  clearVariant(view._buffer);
175
- else
175
+ else {
176
176
  view._buffer.fill(0);
177
+ flushMirror(view._buffer);
178
+ }
177
179
  writeVariant(view._buffer, value);
178
180
  return;
179
181
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@node-ctypes/comtypes",
3
- "version": "0.2.0",
3
+ "version": "0.3.1",
4
4
  "description": "Typed Windows COM client and server support for Node.js powered by node-ctypes, inspired by Python comtypes",
5
5
  "author": "Damiano Mazzella",
6
6
  "license": "MIT",
@@ -81,7 +81,7 @@
81
81
  "prepublishOnly": "npm test"
82
82
  },
83
83
  "dependencies": {
84
- "node-ctypes": "^2.12.2"
84
+ "node-ctypes": "^2.15.2"
85
85
  },
86
86
  "devDependencies": {
87
87
  "@types/node": "^26.6.6",