1
+ /* global Buffer */
1
2
'use strict' ;
2
3
var ffi = require ( 'ffi' ) ,
3
- types = require ( './types' ) ;
4
+ types = require ( './types' ) ,
5
+ Key = require ( './key' ) ,
6
+ ref = require ( 'ref' ) ,
7
+ error = require ( './error' ) ,
8
+ windef = require ( './windef' ) ;
4
9
5
10
var advApi = ffi . Library ( 'Advapi32' , {
6
- 'RegOpenCurrentUser' : [ 'uint64' , [ types . REGSAM , types . PHKEY ] ] ,
7
- 'RegQueryValueExA' : [ 'uint64' , [ types . HKEY , 'string' , 'pointer' , types . LPDWORD , types . LPBYTE , types . LPDWORD ] ] ,
8
- 'RegOpenKeyExA' : [ 'uint64' , [ 'uint64' , 'string' , types . DWORD , types . REGSAM , types . PHKEY ] ] ,
9
- 'RegSetValueExA' : [ 'uint64' , [ types . HKEY , 'string' , 'pointer' , types . DWORD , types . LPBYTE , types . DWORD ] ]
11
+ RegOpenCurrentUser : [ 'uint64' , [ types . REGSAM , types . PHKEY ] ] ,
12
+ RegQueryValueExA : [ 'uint64' , [ types . HKEY , 'string' , 'pointer' , types . LPDWORD , types . LPBYTE , types . LPDWORD ] ] ,
13
+ RegOpenKeyExA : [ 'uint64' , [ 'uint64' , 'string' , types . DWORD , types . REGSAM , types . PHKEY ] ] ,
14
+ RegSetValueExA : [ 'uint64' , [ types . HKEY , 'string' , 'pointer' , types . DWORD , types . LPBYTE , types . DWORD ] ] ,
15
+ /**
16
+ * LONG WINAPI RegCreateKeyEx(
17
+ _In_ HKEY hKey,
18
+ _In_ LPCTSTR lpSubKey,
19
+ _Reserved_ DWORD Reserved,
20
+ _In_opt_ LPTSTR lpClass,
21
+ _In_ DWORD dwOptions,
22
+ _In_ REGSAM samDesired,
23
+ _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
24
+ _Out_ PHKEY phkResult,
25
+ _Out_opt_ LPDWORD lpdwDisposition
26
+ );
27
+ */
28
+ RegCreateKeyExA : [ 'uint64' , [ types . HKEY , 'string' , 'pointer' , 'pointer' , types . DWORD , types . REGSAM , 'pointer' , types . PHKEY , 'pointer' ] ] ,
29
+ /*
30
+ LONG WINAPI RegDeleteTree(
31
+ _In_ HKEY hKey,
32
+ _In_opt_ LPCTSTR lpSubKey
33
+ );
34
+ */
35
+ RegDeleteTreeA : [ 'uint64' , [ types . HKEY , 'string' ] ]
10
36
} ) ;
11
37
12
- class Key {
13
- constructor ( pHkey , path ) {
14
-
15
- }
16
- }
17
-
18
38
var api = {
19
- openKey : function ( key , subKeyName ) {
20
-
21
- } ,
22
- queryValue : function ( key , valueName ) {
23
-
24
- } ,
25
- setValue : function ( key , valueName , valueType , value ) {
26
-
27
- }
28
- }
29
-
30
- module . exports = api ;
39
+ openKeyFromPredefined : function ( preDefinedKey , subKeyName , accessLevel ) {
40
+ if ( preDefinedKey < 0x80000000 || preDefinedKey > 0x80000006 ) {
41
+ throw 'The key ' + preDefinedKey + ' is not valid. Use the windef module for the list of predefined keys' ;
42
+ }
43
+
44
+ var pHkey = ref . alloc ( types . PHKEY ) ;
45
+ var result = advApi . RegOpenKeyExA ( preDefinedKey , subKeyName , 0 , accessLevel , pHkey ) ;
46
+ console . log ( 'result:' + result ) ;
47
+ if ( result !== 0 ) {
48
+ throw 'Failed to open key error: ' + error [ result ] ;
49
+ }
50
+
51
+ return new Key ( pHkey , subKeyName ) ;
52
+ } ,
53
+ openKeyFromKeyObject : function ( keyObject , subKeyName , accessLevel ) {
54
+ var pHkey = ref . alloc ( types . PHKEY ) ;
55
+
56
+ // RegOpenKeyEx can also take an HKEY in addition to a predefined value
57
+ var advApi2 = ffi . Library ( 'Advapi32' , {
58
+ RegOpenKeyExA : [ 'uint64' , [ types . HKEY , 'string' , types . DWORD , types . REGSAM , types . PHKEY ] ]
59
+ } ) ;
60
+ var result = advApi2 . RegOpenKeyExA ( keyObject . handle . deref ( ) , subKeyName , 0 , accessLevel , pHkey ) ;
61
+
62
+ if ( result !== 0 ) {
63
+ throw 'Failed to open key error: ' + error [ result ] ;
64
+ }
65
+
66
+ return new Key ( pHkey , subKeyName ) ;
67
+ } ,
68
+ queryValueForKeyObject : function ( key , valueName ) {
69
+ var pKeyDataLength = ref . alloc ( types . LPDWORD ) ,
70
+ pKeyType = ref . alloc ( types . LPDWORD ) ;
71
+ // QUERY FOR VALUE SIZE & TYPE
72
+ var result = advApi . RegQueryValueExA ( key . handle . deref ( ) , valueName , null , pKeyType , null , pKeyDataLength ) ;
73
+
74
+ // READ VALUE
75
+ var value = new Buffer ( pKeyDataLength . readUInt32LE ( ) ) ,
76
+ valueType = pKeyType . readUInt32LE ( ) ;
77
+ console . log ( valueType === 1 ) ;
78
+ switch ( valueType ) {
79
+ case windef . REG_VALUE_TYPE . REG_SZ :
80
+ case windef . REG_VALUE_TYPE . REG_EXPAND_SZ :
81
+ case windef . REG_VALUE_TYPE . REG_LINK :
82
+ value . type = types . LPCTSR ;
83
+ break ;
84
+ case windef . REG_VALUE_TYPE . REG_BINARY :
85
+ value . type = types . PVOID ;
86
+ break ;
87
+ case windef . REG_VALUE_TYPE . REG_DWORD :
88
+ case windef . REG_VALUE_TYPE . REG_DWORD_BIG_ENDIAN :
89
+ case windef . REG_VALUE_TYPE . REG_DWORD_LITTLE_ENDIAN :
90
+ value . type = types . DWORD ;
91
+ break ;
92
+ default :
93
+ throw 'The Value Type: ' + valueType + ' is currently unsupported' ;
94
+ }
95
+
96
+ // READ VALUE
97
+ result = advApi . RegQueryValueExA ( key . handle . deref ( ) , valueName , null , pKeyType , value , pKeyDataLength ) ;
98
+
99
+ if ( result !== 0 ) {
100
+ throw 'Failed to open key error: ' + error [ result ] ;
101
+ }
102
+
103
+ return value . toString ( ) ;
104
+ } ,
105
+ setValueForKeyObject : function ( key , valueName , valueType , value ) {
106
+ if ( valueType < 1 || valueType > 8 ) {
107
+ throw 'Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE' ;
108
+ }
109
+ var buffer ,
110
+ byte ;
111
+
112
+ switch ( windef . REG_VALUE_TYPE [ valueType ] ) {
113
+ case windef . REG_SZ :
114
+ case windef . REG_EXPAND_SZ :
115
+ case windef . REG_LINK :
116
+ buffer = new Buffer ( value , 'ascii' ) ;
117
+ break ;
118
+ case windef . REG_BINARY :
119
+ // we assume that the value is a buffer since it should be binary data
120
+ buffer = value ;
121
+ break ;
122
+ case windef . REG_VALUE_TYPE . REG_DWORD :
123
+ case windef . REG_VALUE_TYPE . REG_DWORD_BIG_ENDIAN :
124
+ case windef . REG_VALUE_TYPE . REG_DWORD_LITTLE_ENDIAN :
125
+ buffer = new Buffer ( 4 , value ) ;
126
+ break ;
127
+ default :
128
+ throw 'The type ' + valueType + ' is currently unsupported' ;
129
+ }
130
+
131
+ byte = ref . alloc ( types . LPBYTE , buffer ) ;
132
+
133
+ var result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
134
+
135
+ if ( result !== 0 ) {
136
+ throw 'Failed to open key error: ' + error [ result ] ;
137
+ }
138
+ } ,
139
+ createKey : function ( key , subKeyName , accessLevel ) {
140
+ var pHkey = ref . alloc ( types . PHKEY ) ;
141
+
142
+ var result = advApi . RegCreateKeyExA ( key . handle . deref ( ) , subKeyName , null , null , 0 /*REG_OPTION_NON_VOLATILE*/ , accessLevel , null , pHkey , null ) ;
143
+
144
+ if ( result !== 0 ) {
145
+ throw 'Failed to open key error: ' + error [ result ] ;
146
+ }
147
+ } ,
148
+ deleteKey : function ( key , subKeyName ) {
149
+ var result = advApi . RegDeleteTreeA ( key . handle . deref ( ) , subKeyName ) ;
150
+
151
+ if ( result !== 0 ) {
152
+ throw 'Failed to open key error ' + result + ': + error[result]' ;
153
+ }
154
+ }
155
+ } ;
156
+
157
+ module . exports = api ;
0 commit comments