Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP / requesting feedback: Node 10 support. #62

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
0x2: 'ERROR_FILE_NOT_FOUND',
0x3: 'ERROR_PATH_NOT_FOUND',
0x5: 'ERROR_ACCESS_DENIED',
0x6: 'ERROR_INVALID_HANDLE'
};
2 changes: 1 addition & 1 deletion lib/native/adv_api.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Minimal wrappers for Advapi32.dll to support basic registry manipulations

'use strict';
var ffi = require('ffi'),
var ffi = require('ffi-napi'),
types = require('../types');

// Javascript bindings for native Win32 registry APIs
Expand Down
2 changes: 1 addition & 1 deletion lib/native/shell32.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Minimnal wrappers for shell32.js
var windef = require('../windef'),
ffi = require('ffi'),
ffi = require('ffi-napi'),
ref = require('ref');

var SHELLEXECUTEINFOPtr = ref.refType(windef.SHELLEXECUTEINFO);
Expand Down
46 changes: 25 additions & 21 deletions lib/registry.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* global Buffer */
'use strict';
var ffi = require('ffi'),
var ffi = require('ffi-napi'),
types = require('./types'),
advApi = require('./native/adv_api'),
Key = require('./key'),
Expand All @@ -9,24 +9,28 @@ var ffi = require('ffi'),
windef = require('./windef'),
debug = require('debug')('windows-registry');

function keyError(message, result, ...args) {
return new Error(message + ', error ' + result + (error[result] ? ': ' + error[result] : '') + '. Args: ' + args.join(', '));
}

var api = {
openKeyFromPredefined: function (preDefinedKey, subKeyName, accessLevel) {
if (preDefinedKey < 0x80000000 || preDefinedKey > 0x80000006) {
throw 'The key ' + preDefinedKey + ' is not valid. Use the windef module for the list of predefined keys';
throw new Error('The key ' + preDefinedKey + ' is not valid. Use the windef module for the list of predefined keys');
}

var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
var pHkey = ref.alloc(types.PHKEY, Buffer.alloc(ref.sizeof.pointer));
debug('PHKEY LENGTH: ' + pHkey.deref().length);
var result = advApi.RegOpenKeyExA(preDefinedKey, subKeyName, 0, accessLevel, pHkey);
debug('result:' + result);
if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
throw keyError('Failed to open key', result, preDefinedKey, subKeyName, accessLevel);
}

return new Key(pHkey, subKeyName);
},
openKeyFromKeyObject: function (keyObject, subKeyName, accessLevel) {
var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
var pHkey = ref.alloc(types.PHKEY, Buffer.alloc(ref.sizeof.pointer));

// RegOpenKeyEx can also take an HKEY in addition to a predefined value
var advApi2 = ffi.Library('Advapi32', {
Expand All @@ -35,18 +39,18 @@ var api = {
var result = advApi2.RegOpenKeyExA(keyObject.handle.deref(), subKeyName, 0, accessLevel, pHkey);

if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
throw keyError('Failed to open key', result, keyObject.path, subKeyName, accessLevel);
}

return new Key(pHkey, subKeyName);
},
queryValueForKeyObject: function (key, valueName) {
var pKeyDataLength = ref.alloc(types.LPDWORD, new Buffer(ref.sizeof.pointer)),
pKeyType = ref.alloc(types.LPDWORD, new Buffer(ref.sizeof.pointer));
var pKeyDataLength = ref.alloc(types.LPDWORD, Buffer.alloc(ref.sizeof.pointer)),
pKeyType = ref.alloc(types.LPDWORD, Buffer.alloc(ref.sizeof.pointer));
// QUERY FOR VALUE SIZE & TYPE
var result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, null, pKeyDataLength);
// READ VALUE
var value = new Buffer(pKeyDataLength.readUInt32LE()),
var value = Buffer.alloc(pKeyDataLength.readUInt32LE()),
valueType = pKeyType.readUInt32LE();
switch (valueType) {
case windef.REG_VALUE_TYPE.REG_SZ:
Expand All @@ -63,14 +67,14 @@ var api = {
value.type = types.DWORD;
break;
default:
throw 'The Value Type: ' + valueType + ' is currently unsupported';
throw new Error('The Value Type: ' + valueType + ' is currently unsupported');
}

// READ VALUE
result = advApi.RegQueryValueExA(key.handle.deref(), valueName, null, pKeyType, value, pKeyDataLength);

if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
throw keyError('Failed to query key value', result, key.path, valueName);
}

if (value.type === types.LPTSR) {
Expand All @@ -83,7 +87,7 @@ var api = {
},
setValueForKeyObject: function (key, valueName, valueType, value) {
if (valueType < 1 || valueType > 8) {
throw 'Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE';
throw new Error('Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE');
}
var buffer,
byte,
Expand All @@ -93,7 +97,7 @@ var api = {
case windef.REG_VALUE_TYPE.REG_SZ:
case windef.REG_VALUE_TYPE.REG_EXPAND_SZ:
case windef.REG_VALUE_TYPE.REG_LINK:
buffer = new Buffer(value, 'utf8');
buffer = Buffer.from(value, 'utf8');
byte = ref.alloc(types.LPBYTE, buffer);
debug('content length:' + Buffer.byteLength(value, 'utf8'));
debug(value);
Expand All @@ -109,45 +113,45 @@ var api = {
case windef.REG_VALUE_TYPE.REG_DWORD:
case windef.REG_VALUE_TYPE.REG_DWORD_BIG_ENDIAN:
case windef.REG_VALUE_TYPE.REG_DWORD_LITTLE_ENDIAN:
buffer = new Buffer(4, value);
buffer = Buffer.alloc(4, value);
result = advApi.RegSetValueExA(key.handle.deref(), valueName, null, valueType, byte.deref(), buffer.length);
break;
default:
throw 'The type ' + valueType + ' is currently unsupported';
throw new Error('The type ' + valueType + ' is currently unsupported');
}

if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
throw keyError('Failed to set key value', result, key.path, valueName);
}
},
createKey: function (key, subKeyName, accessLevel) {
var pHkey = ref.alloc(types.PHKEY, new Buffer(ref.sizeof.pointer));
var pHkey = ref.alloc(types.PHKEY, Buffer.alloc(ref.sizeof.pointer));

var result = advApi.RegCreateKeyExA(key.handle.deref(), subKeyName, null, null, windef.REG_OPTION_NON_VOLATILE, accessLevel, null, pHkey, null);

if (result !== 0) {
throw 'Failed to open key error: ' + error[result];
throw keyError('Failed to create key', result, key.path, subKeyName);
}
},
deleteKey: function (key, subKeyName) {
var result = advApi.RegDeleteTreeA(key.handle.deref(), subKeyName);

if (result !== 0) {
throw 'Failed to open key error ' + result + ':' + error[result];
throw keyError('Failed to delete key', result, key.path, subKeyName);
}
},
deleteValue: function (key, value) {
var result = advApi.RegDeleteValueA(key.handle.deref(), value);

if (result !== 0) {
throw 'Failed to delete value error ' + result + ':' + error[result];
throw keyError('Failed to delete key default value', result, key.path);
}
},
closeKey: function (key) {
var result = advApi.RegCloseKey(key.handle.deref());

if (result !== 0) {
throw 'Failed to open key error ' + result + ':' + error[result];
throw keyError('Failed to close key', result, key.path);
}
}
};
Expand Down
Loading