Skip to content

Commit a59443e

Browse files
author
Steven Edouard
committed
Merge pull request #3 from CatalystCode/feat_file_association
feat(lib/file_association) - file associations!
2 parents 8c781fa + ca9101a commit a59443e

File tree

4 files changed

+62
-1
lines changed

4 files changed

+62
-1
lines changed

lib/file_association.js

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict';
2+
var registry = require('./registry'),
3+
windef = require('./windef');
4+
module.exports = {
5+
associateExeForFile: function (handlerName, handlerDescription, iconPath, exePath, extensionName) {
6+
var key = registry.openKeyFromPredefined(windef.HKEY.HKEY_CLASSES_ROOT, '', windef.KEY_ACCESS.KEY_ALL_ACCESS);
7+
8+
registry.createKey(key, extensionName, windef.KEY_ACCESS.KEY_ALL_ACCESS);
9+
10+
var appKey = registry.openKeyFromKeyObject(key, extensionName, windef.KEY_ACCESS.KEY_ALL_ACCESS);
11+
12+
registry.setValueForKeyObject(appKey, '', windef.REG_VALUE_TYPE.REG_SZ, handlerName);
13+
appKey.close();
14+
15+
registry.createKey(key, handlerName, windef.KEY_ACCESS.KEY_ALL_ACCESS);
16+
var handlerKey = registry.openKeyFromKeyObject(key, handlerName, windef.KEY_ACCESS.KEY_ALL_ACCESS);
17+
18+
registry.setValueForKeyObject(handlerKey, '', windef.REG_VALUE_TYPE.REG_SZ, handlerDescription);
19+
registry.createKey(handlerKey, 'DefaultIcon', windef.KEY_ACCESS.KEY_ALL_ACCESS);
20+
21+
var defaultIconKey = registry.openKeyFromKeyObject(handlerKey, 'DefaultIcon', windef.KEY_ACCESS.KEY_ALL_ACCESS);
22+
23+
registry.setValueForKeyObject(defaultIconKey, '', windef.REG_VALUE_TYPE.REG_SZ, iconPath);
24+
25+
registry.createKey(handlerKey, 'shell\\Open\\Command', windef.KEY_ACCESS.KEY_ALL_ACCESS);
26+
27+
var commandKey = registry.openKeyFromKeyObject(handlerKey, 'shell\\Open\\Command', windef.KEY_ACCESS.KEY_ALL_ACCESS);
28+
registry.setValueForKeyObject(commandKey, '', windef.REG_VALUE_TYPE.REG_SZ, exePath);
29+
30+
commandKey.close();
31+
handlerKey.close();
32+
}
33+
};

lib/key.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
'use strict';
2+
23
class Key {
34
constructor (pHKey, path) {
45
this.handle = pHKey;
56
this.path = path;
67
}
78
close () {
9+
var registry = require('./registry');
10+
registry.closeKey(this);
811
}
912

1013
toString () {

lib/registry.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ var advApi = ffi.Library('Advapi32', {
3232
_In_opt_ LPCTSTR lpSubKey
3333
);
3434
*/
35-
RegDeleteTreeA: ['uint64', [types.HKEY, 'string']]
35+
RegDeleteTreeA: ['uint64', [types.HKEY, 'string']],
36+
/*
37+
LONG WINAPI RegCloseKey(
38+
_In_ HKEY hKey
39+
);
40+
*/
41+
RegCloseKey: ['uint64', [types.HKEY]]
3642
});
3743

3844
var api = {
@@ -148,6 +154,14 @@ var api = {
148154
deleteKey: function (key, subKeyName) {
149155
var result = advApi.RegDeleteTreeA(key.handle.deref(), subKeyName);
150156

157+
if (result !== 0) {
158+
throw 'Failed to open key error ' + result + ': + error[result]';
159+
}
160+
},
161+
closeKey: function (key) {
162+
console.log('KEY:' + key);
163+
var result = advApi.RegCloseKey(key.handle.deref());
164+
151165
if (result !== 0) {
152166
throw 'Failed to open key error ' + result + ': + error[result]';
153167
}

test/file_association.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/* global describe, it */
2+
'use strict';
3+
var fileAssociation = require('../lib/file_association'),
4+
assert = require('assert');
5+
6+
describe('File Association Test', () => {
7+
it('Should create a file association for *.zzz files', () => {
8+
fileAssociation.associateExeForFile('myTestHandler', 'A test handler for unit tests', '', 'C:\\Program Files\\nodejs\\node.exe %1', '.zzz');
9+
assert(true);
10+
});
11+
});

0 commit comments

Comments
 (0)