1
- # Windows Registry Utility Library
1
+ <br >
2
+ <div align =" center " >
3
+ <img width="640" height="240" src="media/winreglib.webp" alt="winreglib">
4
+ </div >
5
+ <br >
2
6
3
- A library for querying and watching the Windows Registry.
7
+ ` winreglib ` is a native C++ addon for Node.js for querying and watching Windows
8
+ Registry keys. It provides a read-only, synchronous API for accessing the
9
+ Windows Registry.
4
10
5
- ## Prerequisites
11
+ ## Features
6
12
7
- ` winreglib ` requires N-API version 3 and the following Node.js versions:
13
+ - Get, list, and watch registry keys _ without_ spawning ` reg.exe `
14
+ - Written in TypeScript
15
+ - Packaged as an ESM module
16
+ - Supports x64 (64-bit) and ia32 (32-bit) CPU architectures
8
17
9
- * v10.2.0 or newer
18
+ ## Requirements
10
19
11
- ## Installation
12
-
13
- npm install winreglib
14
-
15
- ## Introduction
16
-
17
- ` winreglib ` is a native Node.js addon for querying the Windows Registry. The API is synchronous.
18
-
19
- Currently ` winreglib ` only supports read operations. It can support write operations someday if
20
- need and time exists.
20
+ ` winreglib ` requires Node.js >=18.17.0 (Node-API 8) and ships with pre-built
21
+ binaries for x64 and ia32 architectures. ARM-based architectures are not
22
+ officially supported, but should technically work as long as you have the
23
+ build tools installed.
21
24
22
25
## Example
23
26
24
- ``` js
27
+ ``` javascript
25
28
import winreglib from ' winreglib' ;
26
29
27
30
const key = ' HKLM\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion' ;
28
31
const results = winreglib .list (key);
29
32
30
- console .log (` ${ results .root } \\ ${ results .path } ` );
33
+ console .log (` Resolved ${ results .resolvedRoot } ` );
34
+ console .log (` ${ results .key } has ${ results .subkeys .length } subkeys` );
31
35
32
36
console .log (' Subkeys:' );
33
37
for (const subkey of results .subkeys ) {
@@ -40,6 +44,20 @@ for (const valueName of results.values) {
40
44
}
41
45
```
42
46
47
+ ## Supported Root Keys
48
+
49
+ | Name | Abbreviation |
50
+ | ---------------------------------- | ------------ |
51
+ | ` HKEY_CLASSES_ROOT ` | ` HKCR ` |
52
+ | ` HKEY_CURRENT_CONFIG ` | ` HKCC ` |
53
+ | ` HKEY_CURRENT_USER ` | ` HKCU ` |
54
+ | ` HKEY_CURRENT_USER_LOCAL_SETTINGS ` | |
55
+ | ` HKEY_LOCAL_MACHINE ` | ` HKLM ` |
56
+ | ` HKEY_PERFORMANCE_DATA ` | |
57
+ | ` HKEY_PERFORMANCE_NLSTEXT ` | |
58
+ | ` HKEY_PERFORMANCE_TEXT ` | |
59
+ | ` HKEY_USERS ` | ` HKU ` |
60
+
43
61
## API
44
62
45
63
### ` get(key, valueName) `
@@ -51,12 +69,16 @@ Get a value for the given key and value name.
51
69
| ` key ` | String | The key beginning with the root. |
52
70
| ` valueName ` | String | The name of the value to get. |
53
71
54
- Returns a ` String ` , ` Number ` , ` Buffer ` , ` Array.<String> ` , or ` null ` depending on the value.
72
+ Returns a ` String ` , ` Number ` , ` Buffer ` , ` Array.<String> ` , or ` null `
73
+ depending on the value.
55
74
56
75
If ` key ` or ` valueName ` is not found, an ` Error ` is thrown.
57
76
58
77
``` js
59
- const value = winreglib .get (' HKLM\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion' , ' ProgramFilesDir' );
78
+ const value = winreglib .get (
79
+ ' HKLM\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion' ,
80
+ ' ProgramFilesDir'
81
+ );
60
82
61
83
console .log (value);
62
84
```
@@ -73,20 +95,31 @@ Retreives all subkeys and value names for a give key.
73
95
| -------- | ------ | -------------------------------- |
74
96
| ` key ` | String | The key beginning with the root. |
75
97
76
- Returns an ` Object ` with the resolved ` resolvedRoot ` (String), ` key ` (String), ` subkeys `
77
- (Array[ String] ), and ` values ` (Array[ String] ).
98
+ Returns an ` RegistryKey ` object:
99
+
100
+ ```
101
+ type RegistryKey = {
102
+ resolvedRoot: string;
103
+ key: string;
104
+ subkeys: string[];
105
+ values: unknown[];
106
+ };
107
+ ```
78
108
79
109
If ` key ` is not found, an ` Error ` is thrown.
80
110
81
111
``` js
82
- const result = winreglib .list (' HKLM\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Setup' );
112
+ const result = winreglib .list (
113
+ ' HKLM\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Setup
114
+ );
83
115
84
116
console.log(result);
85
117
```
86
118
87
119
```js
88
120
{ resolvedRoot: ' HKEY_LOCAL_MACHINE ' ,
89
- key: ' HKEY_LOCAL_MACHINE\\ SOFTWARE\\ Microsoft\\ Windows\\ CurrentVersion\\ Setup' ,
121
+ key:
122
+ ' HKEY_LOCAL_MACHINE \\SOFTWARE \\Microsoft\\Windows\\CurrentVersion\\Setup' ,
90
123
subkeys:
91
124
[ ' DPI ' ,
92
125
' ImageServicingData' ,
@@ -109,8 +142,8 @@ Watches a key for changes in subkeys or values.
109
142
| -------- | ------ | -------------------------------- |
110
143
| `key` | String | The key beginning with the root. |
111
144
112
- Returns a handle (` EventEmitter ` ) that emits ` "change" ` events. Call ` handle.stop() ` to stop watching
113
- the key.
145
+ Returns a handle (`WinRegLibWatchHandle` which extends `EventEmitter`) that
146
+ emits `"change"` events. Call `handle.stop()` to stop watching the key.
114
147
115
148
```js
116
149
const handle = winreglib.watch(' HKLM \\SOFTWARE ' );
@@ -120,44 +153,92 @@ handle.on('change', evt => {
120
153
});
121
154
```
122
155
123
- The ` "change" ` event object contains a change ` "type" ` and the affected ` "key" ` .
156
+ The `"change"` event object contains a change `"type"` and the affected
157
+ `"key"`.
124
158
125
159
| Event Type | Description |
126
160
| ---------- | ---------------------------------- |
127
161
| `add` | The `key` was added. |
128
162
| `change` | A subkey or value was added, changed, deleted, or permissions modified, but we don' t know exactly what. |
129
163
| ` delete` | The ` key` was deleted. |
130
164
131
- ` watch() ` can track keys that do not exist and when they are created, a change event will be
132
- emitted. You can watch the same key multiple times, however each returned handle is unique and you
133
- must call ` handle.stop() ` for each.
165
+ ` watch()` can track keys that do not exist and when they are created, a
166
+ change event will be emitted . You can watch the same key multiple times,
167
+ however each returned handle is unique and you must call ` handle.stop()` for
168
+ each.
134
169
135
- Due to limitations of the Win32 API, ` watch() ` is unable to determine what actually changed during
136
- a ` change ` event type. You will need to call ` list() ` and cache the subkeys and values, then call
137
- ` list() ` again when a change is emitted and compare the before and after.
170
+ Due to limitations of the Win32 API , ` watch()` is unable to determine what
171
+ actually changed during a ` change` event type . You will need to call ` list()`
172
+ and cache the subkeys and values, then call ` list()` again when a change is
173
+ emitted and compare the before and after.
138
174
139
- Note that ` watch() ` does not support recursively watching for changes.
175
+ Note that ` watch()` does not support recursively watching for changes . The
176
+ Win32 API does support recursive watching, so this could be added in the
177
+ future.
140
178
141
179
## Advanced
142
180
143
181
### Debug Logging
144
182
145
- ` winreglib ` exposes an event emitter that emits debug log messages. This is intended to help debug
146
- issues under the hood. The average user will never need to use this, however it would be handy when
147
- filing a bug.
183
+ ` winreglib` exposes an event emitter that emits debug log messages . This is
184
+ intended to help debug issues under the hood . The average user will never
185
+ need to use this , however it would be handy when filing a bug.
148
186
149
187
` ` ` js
150
188
winreglib.on('log', msg => console.log(msg));
151
189
` ` `
152
190
153
- Alternatively, ` winreglib ` uses the amazing [ ` snooplogg ` ] [ 2 ] debug logger where you simply set the
154
- ` SNOOPLOGG ` environment variable to ` winreglib ` (or ` * ` ) and it will print the debug log to stdout.
191
+ Alternatively, ` winreglib` uses the amazing [` snooplogg` ][2 ] debug logger
192
+ where you simply set the ` SNOOPLOGG` environment variable to ` winreglib` (or
193
+ ` *` ) and it will print the debug log to ` stderr` .
194
+
195
+ ` ` ` bash
196
+ $ SNOOPLOGG=winreglib node myapp.js
197
+ 6.150s winreglib:Watchman Initializing async work (thread 16764576586047274673)
198
+ 6.150s winreglib:Watchman::run Initializing run loop (thread 12502165600786624632)
199
+ 6.151s winreglib:Watchman::run Populating active copy (count=0)
200
+ 6.151s winreglib:Watchman::run Waiting on 2 objects...
201
+ 6.152s winreglib:list key="HKLM" subkey="SOFTWARE\M icrosoft\W indows\C urrentVersion"
202
+ 6.152s winreglib:list 170 keys (max 30), 11 values (max 24)
203
+ ` ` `
204
+
205
+ ### Building ` winreglib`
206
+
207
+ ` winreglib` has two components: the public interface written in TypeScript and
208
+ the native addon written in C ++ .
209
+
210
+ To compile the C ++ code, you will need the Microsoft Visual Studio (not VSCode)
211
+ or the Microsoft Build Tools . You may also need Python 3 installed.
212
+
213
+ | Command | Description |
214
+ | -------------------- | ---------- - |
215
+ | ` pnpm build` | Compiles the TypeScript and the Node .js native C ++ addon for the current architecture |
216
+ | ` pnpm build:bundle` | Compiles only the TypeScript code |
217
+ | ` pnpm build:local` | Compiles only the Node .js native C ++ addon |
218
+ | ` pnpm rebuild:local` | Cleans and re- compiles only the Node .js native C ++ addon |
219
+
220
+ When publishing, the native C ++ addon is prebuilt for x64 and ia32
221
+ architectures . Generally you shouldn' t need be concerned with the prebuilt
222
+ binaries, however the following commands will compile the prebuilds:
223
+
224
+ | Command | Description |
225
+ | --------------------- | ------------------------------ |
226
+ | `pnpm build:prebuild` | Prebuild for x64 and ia32 |
227
+ | `pnpm prebuild-arm` | Prebuild for arm (32-bit) |
228
+ | `pnpm prebuild-arm64` | Prebuild for arm64 |
229
+ | `pnpm prebuild-ia32` | Prebuild for ia32 (x86 32-bit) |
230
+ | `pnpm prebuild-x64` | Prebuild for x64 |
231
+
232
+ ### Architecture
233
+
234
+ When `winreglib` is imported, it immediately spawns a background thread in the
235
+ event the app is going to watch a key. If a key is added/changed/deleted, the
236
+ background thread sends a message to the main thread which emits the change
237
+ event.
155
238
156
- ## License
239
+ ## Legal
157
240
158
- This project is open source under the [ Apache Public License v2] [ 1 ] and is developed by
159
- [ Axway, Inc] ( http://www.axway.com/ ) and the community. Please read the [ ` LICENSE ` ] [ 1 ] file included
160
- in this distribution for more information.
241
+ Titanium is a registered trademark of TiDev Inc. All Titanium trademark and patent rights were transferred and assigned to TiDev Inc. on 4/7/2022. Please see the LEGAL information about using our trademarks, privacy policy, terms of usage and other legal information at https://tidev.io/legal.
161
242
162
- [ 1 ] : https://github.com/appcelerator /winreglib/blob/master/LICENSE
243
+ [1]: https://github.com/tidev /winreglib/blob/master/LICENSE
163
244
[2]: https://www.npmjs.com/package/snooplogg
0 commit comments