node-ctypes 1.13.0 → 2.0.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.
@@ -1,128 +1,368 @@
1
1
  /**
2
2
  * @file win_types.js
3
3
  * @module platform/win_types
4
- * @description Windows API type aliases (Python `ctypes.wintypes` parity).
5
- *
6
- * Alias puri dei primitivi C, senza behavior aggiuntivo. Permettono di
7
- * scrivere signature Win32 leggibili e ricopiabili dalle header MSDN
8
- * senza tradurre manualmente (DWORD → c_uint32, ecc.).
9
- *
10
- * La factory `createWinTypes(prims)` cattura le primitive già caricate
11
- * da `createPrimitiveTypes` — coerente con gli altri modules della lib.
12
- *
13
- * Copre la maggior parte delle typedef `windef.h` / `minwindef.h`
14
- * utilizzate nelle API Win32, incluse quelle pointer-sized (HANDLE,
15
- * LPARAM/WPARAM) che variano tra x86 e x64.
4
+ * @description Windows API types, mirroring Python's `ctypes.wintypes`:
5
+ * the same names, aliases, structures (`RECT`, `POINT`, `MSG`, ...) and
6
+ * pointer types (`LPDWORD = POINTER(DWORD)`, ...), plus a few common extras
7
+ * (`QWORD`, `LRESULT`, `UINT_PTR`, `LSTATUS`, ...).
16
8
  *
17
9
  * Python ctypes parity:
18
- * `from ctypes.wintypes import DWORD, HANDLE, HWND`
10
+ * `from ctypes.wintypes import DWORD, HANDLE, HWND, RECT`
19
11
  *
20
12
  * Node-ctypes:
21
13
  * `import { wintypes } from 'node-ctypes';`
22
- * `const { DWORD, HANDLE, HWND } = wintypes;`
14
+ * `const { DWORD, HANDLE, HWND, RECT } = wintypes;`
23
15
  */
24
16
 
25
17
  /**
26
- * @param {Object} prims - primitive types (output di `createPrimitiveTypes`)
27
- * @returns {Object} namespace con tutti gli alias Win32
18
+ * @param {Object} prims - Primitive types (output of `createPrimitiveTypes`)
19
+ * @param {Object} deps - `{ Structure, POINTER, array }`
20
+ * @returns {Object} Namespace with all the Win32 types
28
21
  */
29
- export function createWinTypes(prims) {
30
- const {
31
- c_int, c_uint, c_long, c_ulong,
32
- c_short, c_ushort, c_ubyte,
33
- c_char_p, c_wchar, c_wchar_p,
34
- c_void_p, c_size_t, c_ssize_t,
35
- c_int64, c_uint64,
36
- } = prims;
37
-
38
- // Primitive numeric types
22
+ export function createWinTypes(prims, { Structure, POINTER, array }) {
23
+ const { c_int, c_uint, c_long, c_ulong, c_short, c_ushort, c_ubyte, c_char, c_char_p, c_wchar, c_wchar_p, c_void_p, c_size_t, c_ssize_t, c_int64, c_uint64, c_float, c_double } = prims;
24
+
25
+ // Numeric types
39
26
  const BYTE = c_ubyte;
40
27
  const WORD = c_ushort;
41
28
  const DWORD = c_ulong;
42
- const QWORD = c_uint64;
43
- const DWORDLONG = c_uint64;
44
- const LONGLONG = c_int64;
45
- const ULONGLONG = c_uint64;
46
- // Win32 BOOL è definito `typedef int BOOL` in windef.h; su Win LLP64
47
- // int e long sono entrambi 32-bit. Usiamo c_long per coerenza con
48
- // l'implementazione storica negli examples/.
49
- const BOOL = c_long;
50
- const INT = c_int;
29
+ const CHAR = c_char;
30
+ const WCHAR = c_wchar;
51
31
  const UINT = c_uint;
52
- const LONG = c_long;
32
+ const INT = c_int;
33
+ const DOUBLE = c_double;
34
+ const FLOAT = c_float;
35
+ const BOOLEAN = BYTE;
36
+ const BOOL = c_long;
53
37
  const ULONG = c_ulong;
54
- const SHORT = c_short;
38
+ const LONG = c_long;
55
39
  const USHORT = c_ushort;
56
- const ATOM = c_ushort;
57
- const COLORREF = c_ulong;
40
+ const SHORT = c_short;
41
+ const LARGE_INTEGER = c_int64;
42
+ const ULARGE_INTEGER = c_uint64;
58
43
 
59
- // Character / string types
60
- const CHAR = c_ubyte;
61
- const WCHAR = c_wchar;
62
- const LPSTR = c_char_p;
63
- const LPCSTR = c_char_p;
64
- const LPWSTR = c_wchar_p;
65
- const LPCWSTR = c_wchar_p;
44
+ // VARIANT_BOOL: 16-bit, VARIANT_TRUE = -1. Reads as a boolean, as Python.
45
+ class VARIANT_BOOL extends c_short {
46
+ static _reader = (buf, off) => buf.readInt16LE(off) !== 0;
47
+ static _writer = (buf, off, val) => buf.writeInt16LE(val ? -1 : 0, off);
48
+ static _readerBE = (buf, off) => buf.readInt16BE(off) !== 0;
49
+ static _writerBE = (buf, off, val) => buf.writeInt16BE(val ? -1 : 0, off);
50
+ static from_param(val) {
51
+ return val ? -1 : 0;
52
+ }
53
+ }
66
54
 
67
- // Void / generic pointer
68
- const LPVOID = c_void_p;
55
+ // String types
56
+ const LPCOLESTR = c_wchar_p;
57
+ const LPOLESTR = c_wchar_p;
58
+ const OLESTR = c_wchar_p;
59
+ const LPCWSTR = c_wchar_p;
60
+ const LPWSTR = c_wchar_p;
61
+ const LPCSTR = c_char_p;
62
+ const LPSTR = c_char_p;
69
63
  const LPCVOID = c_void_p;
70
- const PVOID = c_void_p;
64
+ const LPVOID = c_void_p;
71
65
 
72
- // HANDLE e suoi alias (tutti void* a runtime)
73
- const HANDLE = c_void_p;
74
- const HWND = c_void_p;
75
- const HINSTANCE = c_void_p;
76
- const HMODULE = c_void_p;
77
- const HICON = c_void_p;
78
- const HCURSOR = c_void_p;
79
- const HBRUSH = c_void_p;
80
- const HMENU = c_void_p;
81
- const HFONT = c_void_p;
82
- const HBITMAP = c_void_p;
83
- const HGDIOBJ = c_void_p;
84
- const HKEY = c_void_p;
85
- const HDC = c_void_p;
86
- const HRGN = c_void_p;
87
- const HGLOBAL = c_void_p;
88
-
89
- // Pointer-sized integers (x64: 8 byte, x86: 4 byte).
90
- // c_size_t / c_ssize_t sono già platform-aware.
66
+ // Pointer-sized integers (8 bytes on 64-bit, 4 on 32-bit)
91
67
  const WPARAM = c_size_t;
92
68
  const LPARAM = c_ssize_t;
93
- const LRESULT = c_ssize_t;
94
- const UINT_PTR = c_size_t;
95
- const LONG_PTR = c_ssize_t;
96
- const ULONG_PTR = c_size_t;
97
-
98
- // Advapi / registry
99
- const LSTATUS = c_long; // LONG error code (0 = success)
100
- const REGSAM = c_ulong; // access mask
101
-
102
- // Pointer-to-primitive aliases (per *_OUT params / byref usage).
103
- // Sono void* a livello FFI; il tipo semantico è documentato dal nome.
104
- const PBYTE = c_void_p;
105
- const LPBYTE = c_void_p;
106
- const PDWORD = c_void_p;
107
- const LPDWORD = c_void_p;
108
- const PHKEY = c_void_p;
69
+
70
+ const ATOM = WORD;
71
+ const LANGID = WORD;
72
+ const COLORREF = DWORD;
73
+ const LGRPID = DWORD;
74
+ const LCTYPE = DWORD;
75
+ const LCID = DWORD;
76
+
77
+ // Handles
78
+ const HANDLE = c_void_p;
79
+ const handles = {};
80
+ for (const name of [
81
+ "HACCEL",
82
+ "HBITMAP",
83
+ "HBRUSH",
84
+ "HCOLORSPACE",
85
+ "HDC",
86
+ "HDESK",
87
+ "HDWP",
88
+ "HENHMETAFILE",
89
+ "HFONT",
90
+ "HGDIOBJ",
91
+ "HGLOBAL",
92
+ "HHOOK",
93
+ "HICON",
94
+ "HINSTANCE",
95
+ "HKEY",
96
+ "HKL",
97
+ "HLOCAL",
98
+ "HMENU",
99
+ "HMETAFILE",
100
+ "HMODULE",
101
+ "HMONITOR",
102
+ "HPALETTE",
103
+ "HPEN",
104
+ "HRGN",
105
+ "HRSRC",
106
+ "HSTR",
107
+ "HTASK",
108
+ "HWINSTA",
109
+ "HWND",
110
+ "SC_HANDLE",
111
+ "SERVICE_STATUS_HANDLE",
112
+ ]) {
113
+ handles[name] = HANDLE;
114
+ }
115
+
116
+ // Structures
117
+ class RECT extends Structure {
118
+ static _fields_ = [
119
+ ["left", LONG],
120
+ ["top", LONG],
121
+ ["right", LONG],
122
+ ["bottom", LONG],
123
+ ];
124
+ }
125
+
126
+ class SMALL_RECT extends Structure {
127
+ static _fields_ = [
128
+ ["Left", SHORT],
129
+ ["Top", SHORT],
130
+ ["Right", SHORT],
131
+ ["Bottom", SHORT],
132
+ ];
133
+ }
134
+
135
+ class _COORD extends Structure {
136
+ static _fields_ = [
137
+ ["X", SHORT],
138
+ ["Y", SHORT],
139
+ ];
140
+ }
141
+
142
+ class POINT extends Structure {
143
+ static _fields_ = [
144
+ ["x", LONG],
145
+ ["y", LONG],
146
+ ];
147
+ }
148
+
149
+ class SIZE extends Structure {
150
+ static _fields_ = [
151
+ ["cx", LONG],
152
+ ["cy", LONG],
153
+ ];
154
+ }
155
+
156
+ class FILETIME extends Structure {
157
+ static _fields_ = [
158
+ ["dwLowDateTime", DWORD],
159
+ ["dwHighDateTime", DWORD],
160
+ ];
161
+ }
162
+
163
+ class MSG extends Structure {
164
+ static _fields_ = [
165
+ ["hWnd", handles.HWND],
166
+ ["message", UINT],
167
+ ["wParam", WPARAM],
168
+ ["lParam", LPARAM],
169
+ ["time", DWORD],
170
+ ["pt", POINT],
171
+ ];
172
+ }
173
+
174
+ const MAX_PATH = 260;
175
+
176
+ class WIN32_FIND_DATAA extends Structure {
177
+ static _fields_ = [
178
+ ["dwFileAttributes", DWORD],
179
+ ["ftCreationTime", FILETIME],
180
+ ["ftLastAccessTime", FILETIME],
181
+ ["ftLastWriteTime", FILETIME],
182
+ ["nFileSizeHigh", DWORD],
183
+ ["nFileSizeLow", DWORD],
184
+ ["dwReserved0", DWORD],
185
+ ["dwReserved1", DWORD],
186
+ ["cFileName", array(CHAR, MAX_PATH)],
187
+ ["cAlternateFileName", array(CHAR, 14)],
188
+ ];
189
+ }
190
+
191
+ class WIN32_FIND_DATAW extends Structure {
192
+ static _fields_ = [
193
+ ["dwFileAttributes", DWORD],
194
+ ["ftCreationTime", FILETIME],
195
+ ["ftLastAccessTime", FILETIME],
196
+ ["ftLastWriteTime", FILETIME],
197
+ ["nFileSizeHigh", DWORD],
198
+ ["nFileSizeLow", DWORD],
199
+ ["dwReserved0", DWORD],
200
+ ["dwReserved1", DWORD],
201
+ ["cFileName", array(WCHAR, MAX_PATH)],
202
+ ["cAlternateFileName", array(WCHAR, 14)],
203
+ ];
204
+ }
205
+
206
+ const RGB = (red, green, blue) => red + (green << 8) + (blue << 16);
207
+
208
+ // Pointer types
209
+ const LPBOOL = POINTER(BOOL);
210
+ const PBOOLEAN = POINTER(BOOLEAN);
211
+ const LPBYTE = POINTER(BYTE);
212
+ const PCHAR = POINTER(CHAR);
213
+ const LPCOLORREF = POINTER(COLORREF);
214
+ const LPDWORD = POINTER(DWORD);
215
+ const LPFILETIME = POINTER(FILETIME);
216
+ const PFLOAT = POINTER(FLOAT);
217
+ const LPHANDLE = POINTER(HANDLE);
218
+ const PHKEY = POINTER(handles.HKEY);
219
+ const LPHKL = POINTER(handles.HKL);
220
+ const LPINT = POINTER(INT);
221
+ const PLARGE_INTEGER = POINTER(LARGE_INTEGER);
222
+ const PLCID = POINTER(LCID);
223
+ const LPLONG = POINTER(LONG);
224
+ const LPMSG = POINTER(MSG);
225
+ const LPPOINT = POINTER(POINT);
226
+ const LPRECT = POINTER(RECT);
227
+ const LPSC_HANDLE = POINTER(handles.SC_HANDLE);
228
+ const PSHORT = POINTER(SHORT);
229
+ const LPSIZE = POINTER(SIZE);
230
+ const PSMALL_RECT = POINTER(SMALL_RECT);
231
+ const LPUINT = POINTER(UINT);
232
+ const PULARGE_INTEGER = POINTER(ULARGE_INTEGER);
233
+ const PULONG = POINTER(ULONG);
234
+ const PUSHORT = POINTER(USHORT);
235
+ const PWCHAR = POINTER(WCHAR);
236
+ const LPWIN32_FIND_DATAA = POINTER(WIN32_FIND_DATAA);
237
+ const LPWIN32_FIND_DATAW = POINTER(WIN32_FIND_DATAW);
238
+ const LPWORD = POINTER(WORD);
109
239
 
110
240
  return {
111
- // numeric
112
- BYTE, WORD, DWORD, QWORD, DWORDLONG, LONGLONG, ULONGLONG,
113
- BOOL, INT, UINT, LONG, ULONG, SHORT, USHORT, ATOM, COLORREF,
114
- // character/string
115
- CHAR, WCHAR, LPSTR, LPCSTR, LPWSTR, LPCWSTR,
116
- // void pointers
117
- LPVOID, LPCVOID, PVOID,
118
- // handles
119
- HANDLE, HWND, HINSTANCE, HMODULE, HICON, HCURSOR, HBRUSH,
120
- HMENU, HFONT, HBITMAP, HGDIOBJ, HKEY, HDC, HRGN, HGLOBAL,
121
- // pointer-sized integers
122
- WPARAM, LPARAM, LRESULT, UINT_PTR, LONG_PTR, ULONG_PTR,
123
- // registry
124
- LSTATUS, REGSAM,
125
- // pointer-to-primitive
126
- PBYTE, LPBYTE, PDWORD, LPDWORD, PHKEY,
241
+ // Python ctypes.wintypes
242
+ BYTE,
243
+ WORD,
244
+ DWORD,
245
+ CHAR,
246
+ WCHAR,
247
+ UINT,
248
+ INT,
249
+ DOUBLE,
250
+ FLOAT,
251
+ BOOLEAN,
252
+ BOOL,
253
+ VARIANT_BOOL,
254
+ ULONG,
255
+ LONG,
256
+ USHORT,
257
+ SHORT,
258
+ _LARGE_INTEGER: LARGE_INTEGER,
259
+ LARGE_INTEGER,
260
+ _ULARGE_INTEGER: ULARGE_INTEGER,
261
+ ULARGE_INTEGER,
262
+ LPCOLESTR,
263
+ LPOLESTR,
264
+ OLESTR,
265
+ LPCWSTR,
266
+ LPWSTR,
267
+ LPCSTR,
268
+ LPSTR,
269
+ LPCVOID,
270
+ LPVOID,
271
+ WPARAM,
272
+ LPARAM,
273
+ ATOM,
274
+ LANGID,
275
+ COLORREF,
276
+ LGRPID,
277
+ LCTYPE,
278
+ LCID,
279
+ HANDLE,
280
+ ...handles,
281
+ RECT,
282
+ tagRECT: RECT,
283
+ _RECTL: RECT,
284
+ RECTL: RECT,
285
+ _SMALL_RECT: SMALL_RECT,
286
+ SMALL_RECT,
287
+ _COORD,
288
+ POINT,
289
+ tagPOINT: POINT,
290
+ _POINTL: POINT,
291
+ POINTL: POINT,
292
+ SIZE,
293
+ tagSIZE: SIZE,
294
+ SIZEL: SIZE,
295
+ RGB,
296
+ FILETIME,
297
+ _FILETIME: FILETIME,
298
+ MSG,
299
+ tagMSG: MSG,
300
+ MAX_PATH,
301
+ WIN32_FIND_DATAA,
302
+ WIN32_FIND_DATAW,
303
+ LPBOOL,
304
+ PBOOL: LPBOOL,
305
+ PBOOLEAN,
306
+ LPBYTE,
307
+ PBYTE: LPBYTE,
308
+ PCHAR,
309
+ LPCOLORREF,
310
+ LPDWORD,
311
+ PDWORD: LPDWORD,
312
+ LPFILETIME,
313
+ PFILETIME: LPFILETIME,
314
+ PFLOAT,
315
+ LPHANDLE,
316
+ PHANDLE: LPHANDLE,
317
+ PHKEY,
318
+ LPHKL,
319
+ LPINT,
320
+ PINT: LPINT,
321
+ PLARGE_INTEGER,
322
+ PLCID,
323
+ LPLONG,
324
+ PLONG: LPLONG,
325
+ LPMSG,
326
+ PMSG: LPMSG,
327
+ LPPOINT,
328
+ PPOINT: LPPOINT,
329
+ PPOINTL: LPPOINT,
330
+ LPRECT,
331
+ PRECT: LPRECT,
332
+ LPRECTL: LPRECT,
333
+ PRECTL: LPRECT,
334
+ LPSC_HANDLE,
335
+ PSHORT,
336
+ LPSIZE,
337
+ PSIZE: LPSIZE,
338
+ LPSIZEL: LPSIZE,
339
+ PSIZEL: LPSIZE,
340
+ PSMALL_RECT,
341
+ LPUINT,
342
+ PUINT: LPUINT,
343
+ PULARGE_INTEGER,
344
+ PULONG,
345
+ PUSHORT,
346
+ PWCHAR,
347
+ LPWIN32_FIND_DATAA,
348
+ PWIN32_FIND_DATAA: LPWIN32_FIND_DATAA,
349
+ LPWIN32_FIND_DATAW,
350
+ PWIN32_FIND_DATAW: LPWIN32_FIND_DATAW,
351
+ LPWORD,
352
+ PWORD: LPWORD,
353
+
354
+ // Extras (not in Python's wintypes)
355
+ QWORD: c_uint64,
356
+ DWORDLONG: c_uint64,
357
+ LONGLONG: c_int64,
358
+ ULONGLONG: c_uint64,
359
+ PVOID: c_void_p,
360
+ HCURSOR: HANDLE,
361
+ LRESULT: c_ssize_t,
362
+ UINT_PTR: c_size_t,
363
+ LONG_PTR: c_ssize_t,
364
+ ULONG_PTR: c_size_t,
365
+ LSTATUS: c_long,
366
+ REGSAM: c_ulong,
127
367
  };
128
368
  }