Skip to content

Commit 19f6656

Browse files
committed
Merge pull request microsoft#38 from ritazh/docs
Add NPM Module for Windows Registry
2 parents 5fb7263 + 6c1cd80 commit 19f6656

File tree

3 files changed

+119
-1
lines changed

3 files changed

+119
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Note that this is not intended to be a comprehensive set of recommendations. Rat
3131
* [MAX_PATH explanation and workarounds](windows-environment.md#max_path-explanation-and-workarounds)
3232
* [Compiling native Addon modules](windows-environment.md#compiling-native-addon-modules)
3333
* [Building for cross-platform](building-for-cross-platform.md)
34+
* [Windows services and settings](windows-services-and-settings.md)
3435
* [Deployment](deployment.md)
3536
* [Application-specific tips](application-tips.md)
3637
* [Node.js + Microsoft products, services, and contributions](README.md#nodejs--microsoft-products-services-and-contributions)

application-tips.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,6 @@ Sometimes you need to access plaform functionality for which no suitable module
9494
1. Create a native module add-on by wrapping code in C binding boilerplate code [using the V8 sdk and dev tools](https://nodejs.org/api/addons.html).
9595
2. Use [ref](https://github.com/TooTallNate/ref) and [node-ffi](https://github.com/node-ffi/node-ffi) modules to access C buffers and call shared library (DLL) functions from javascript.
9696

97-
There are plenty of examples of [creating native modules](http://www.martinchristen.ch/node/tutorial11), including our notes on [Compiling native addon modules](windows-environment.md#compiling-native-addon-modules) but less on [using ref and node-ffi](http://opendirective.net/blog/2015/10/working-with-windows-native-code-from-node-js).
97+
There are plenty of examples of [creating native modules](http://www.martinchristen.ch/node/tutorial11), including our notes on [Compiling native addon modules](windows-environment.md#compiling-native-addon-modules) but less on [using ref and node-ffi](http://opendirective.net/blog/2015/10/working-with-windows-native-code-from-node-js). [NPM module for Windows Registry](https://github.com/CatalystCode/windows-registry-node) contains examples of how it leveraged [node-ffi](https://www.npmjs.com/package/ffi), [ref](https://www.npmjs.com/package/ref),[ref-struct](https://www.npmjs.com/package/ref-struct), [Advapi32.dll](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724875(v=vs.85).aspx), and [Shell32.dll](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx) to enable Node.js applications to communicate with Windows interfaces.
9898

9999
> :bulb: Note it is good practice to ensure published modules are [written to run on all platforms](building-for-cross-platform.md#writing-cross-platform-modules-and apps), even though this can be conciderable extra work. Modules that only work on Windows or Linux are likely to unpopular so such code is best restricted to private modules or app code.

windows-services-and-settings.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
## Working with Windows Services and Settings
2+
3+
### Reading and Writing to the Windows Registry in-process from Node.js
4+
As cross-platform application developers, we often need the ability to update the Windows Registry when installing our applications to Windows. [NPM module for Windows Registry](https://www.npmjs.com/package/windows-registry) is an npm package that can be included in your node applications to allow reading and writing keys to Windows registry, creating file associations, and elevating processes to run as an administrator in-process from Node.js.
5+
6+
This library leveraged [node-ffi](https://www.npmjs.com/package/ffi), [ref](https://www.npmjs.com/package/ref),[ref-struct](https://www.npmjs.com/package/ref-struct), [Advapi32.dll](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724875(v=vs.85).aspx), and [Shell32.dll](https://msdn.microsoft.com/en-us/library/windows/desktop/bb762154(v=vs.85).aspx) to enable Node.js applications to communicate with Windows interfaces.
7+
8+
#### Prerequisites
9+
Follow the [Environment setup and configuration](https://github.com/Microsoft/nodejs-guidelines/blob/master/windows-environment.md#environment-setup-and-configuration) section to compile native addon modules.
10+
11+
#### Install
12+
13+
This library interacts with native Windows APIs. To add this module to your Node application, install the package:
14+
15+
```
16+
npm install windows-registry
17+
18+
```
19+
20+
#### Creating File Associations
21+
22+
To create a file association, you can call the `fileAssociation.associateExeForFile` API, which will make windows assign a default program for an arbitrary file extension:
23+
24+
```js
25+
var utils = require('windows-registry').utils;
26+
utils.associateExeForFile('myTestHandler', 'A test handler for unit tests', 'C:\\path\\to\\icon', 'C:\\Program Files\\nodejs\\node.exe %1', '.zzz');
27+
28+
```
29+
After running the code above, you will see files with the extension of `.zzz` will be automatically associated with the Node program and their file icon will be changed to the Node file icon.
30+
31+
#### Reading and Writing to the Windows Registry
32+
33+
This library implements only a few of the basic registry commands, which allow you to do basic CRUD
34+
operations for keys in the registry.
35+
36+
#### Opening a Registry Key
37+
38+
Registry keys can be opened by either opening a predefined registry key defined in the [windef](lib/windef.js) module:
39+
40+
```js
41+
var Key = require('windows-registry').Key;
42+
var key = new Key(windef.HKEY.HKEY_CLASSES_ROOT, '.txt', windef.KEY_ACCESS.KEY_ALL_ACCESS);
43+
44+
```
45+
46+
Or you can open a sub key from an already opened key:
47+
48+
```js
49+
var Key = require('windows-registry').Key;
50+
var key = new Key(windef.HKEY.HKEY_CLASSES_ROOT, '', windef.KEY_ACCESS.KEY_ALL_ACCESS);
51+
var key2 = key.openSubKey('.txt', windef.KEY_ACCESS.KEY_ALL_ACCESS);
52+
53+
```
54+
55+
And don't forget to close your key when you're done. Otherwise, you will leak native resources:
56+
57+
```js
58+
key.close();
59+
60+
```
61+
62+
#### Creating a Key
63+
64+
Creating a key just requires that you have a [Key](lib/key.js) object by either using the [predefined keys](https://github.com/CatalystCode/windows-registry-node/blob/master/lib/windef.js#L27) within the `windef.HKEY` or opening a subkey from an existing key.
65+
66+
```js
67+
var Key = require('windows-registry').Key;
68+
// predefined key
69+
var key = new Key(windef.HKEY.HKEY_CLASSES_ROOT, '', windef.KEY_ACCESS.KEY_ALL_ACCESS);
70+
var createdKey = key.createSubKey('\test_key_name', windef.KEY_ACCESS.KEY_ALL_ACCESS);
71+
72+
```
73+
74+
#### Deleting a Key
75+
To delete a key just call the `Key.deleteKey()` function.
76+
77+
```js
78+
createdKey.deleteKey();
79+
80+
```
81+
82+
#### Writing a Value to a Key
83+
84+
To write a value, you will again need a [Key](lib/key.js) object and just need to call the `Key.setValue` function:
85+
86+
```js
87+
var Key = require('windows-registry').Key,
88+
windef = require('windows-registry').windef;
89+
90+
var key = new Key(windef.HKEY.HKEY_CLASSES_ROOT, '.txt', windef.KEY_ACCESS.KEY_ALL_ACCESS);
91+
key.setValue('test_value_name', windef.REG_VALUE_TYPE.REG_SZ, 'test_value');
92+
93+
```
94+
95+
#### Get a Value From a Key
96+
97+
To get a value from a key, just call `Key.getValue`:
98+
99+
```js
100+
var value = key.getValue('test_value_name');
101+
```
102+
103+
The return value depends on the type of the key (REG_SZ for example will give you a string).
104+
105+
#### Launching a Process as an Admin
106+
107+
To launch a process as an Administrator, you can call the `utils.elevate` API, which will launch a process as an Administrator causing the UAC (User Account Control) elevation prompt to appear if required. This is similar to the Windows Explorer command "Run as administrator". Pass in `FILEPATH` to the process you want to elevate. Pass in any`PARAMETERS` to run with the process. Since this is an asynchronous call, pass in a callback to handle user's selection.
108+
109+
```js
110+
var utils = require('windows-registry').utils;
111+
utils.elevate('C:\\Program Files\\nodejs\\node.exe', 'index.js', function (err, result){console.log(result);});
112+
113+
```
114+
The process you want to launch with admin access will only be launched after the callback is called and only if the user clicks Yes in the UAC prompt. Otherwise, the process will not be launched. If the user is already running as an admin, the UAC prompt will not be triggered and the process you provided will be launched as an administrator automatically.
115+
116+
#### More Examples and Docs?
117+
Check out implementation details of this module: https://github.com/CatalystCode/windows-registry-node.

0 commit comments

Comments
 (0)