2
2
'use strict' ;
3
3
var ffi = require ( 'ffi' ) ,
4
4
types = require ( './types' ) ,
5
+ advApi = require ( './native/adv_api' ) ,
5
6
Key = require ( './key' ) ,
6
7
ref = require ( 'ref' ) ,
7
8
error = require ( './error' ) ,
8
- windef = require ( './windef' ) ;
9
-
10
- var advApi = ffi . Library ( 'Advapi32' , {
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' ] ] ,
36
- /*
37
- LONG WINAPI RegCloseKey(
38
- _In_ HKEY hKey
39
- );
40
- */
41
- RegCloseKey : [ 'uint64' , [ types . HKEY ] ]
42
- } ) ;
9
+ windef = require ( './windef' ) ,
10
+ debug = require ( 'debug' ) ( 'windows-registry' ) ;
43
11
44
12
var api = {
45
13
openKeyFromPredefined : function ( preDefinedKey , subKeyName , accessLevel ) {
@@ -49,7 +17,7 @@ var api = {
49
17
50
18
var pHkey = ref . alloc ( types . PHKEY ) ;
51
19
var result = advApi . RegOpenKeyExA ( preDefinedKey , subKeyName , 0 , accessLevel , pHkey ) ;
52
- console . log ( 'result:' + result ) ;
20
+ debug ( 'result:' + result ) ;
53
21
if ( result !== 0 ) {
54
22
throw 'Failed to open key error: ' + error [ result ] ;
55
23
}
@@ -80,7 +48,6 @@ var api = {
80
48
// READ VALUE
81
49
var value = new Buffer ( pKeyDataLength . readUInt32LE ( ) ) ,
82
50
valueType = pKeyType . readUInt32LE ( ) ;
83
- console . log ( valueType === 1 ) ;
84
51
switch ( valueType ) {
85
52
case windef . REG_VALUE_TYPE . REG_SZ :
86
53
case windef . REG_VALUE_TYPE . REG_EXPAND_SZ :
@@ -106,38 +73,49 @@ var api = {
106
73
throw 'Failed to open key error: ' + error [ result ] ;
107
74
}
108
75
109
- return value . toString ( ) ;
76
+ if ( value . type === types . LPTSR ) {
77
+ // TODO not sure why buffer's utf8 parsing leaves in the unicode null
78
+ // escape sequence. This is a work-around (at least on node 4.1)
79
+ value = value . toString ( ) . replace ( '\u0000' , '' ) ;
80
+ }
81
+
82
+ return value ;
110
83
} ,
111
84
setValueForKeyObject : function ( key , valueName , valueType , value ) {
112
85
if ( valueType < 1 || valueType > 8 ) {
113
86
throw 'Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE' ;
114
87
}
115
88
var buffer ,
116
- byte ;
89
+ byte ,
90
+ result ;
117
91
118
- switch ( windef . REG_VALUE_TYPE [ valueType ] ) {
119
- case windef . REG_SZ :
120
- case windef . REG_EXPAND_SZ :
121
- case windef . REG_LINK :
122
- buffer = new Buffer ( value , 'ascii' ) ;
92
+ switch ( valueType ) {
93
+ case windef . REG_VALUE_TYPE . REG_SZ :
94
+ case windef . REG_VALUE_TYPE . REG_EXPAND_SZ :
95
+ case windef . REG_VALUE_TYPE . REG_LINK :
96
+ buffer = new Buffer ( value , 'utf8' ) ;
97
+ byte = ref . alloc ( types . LPBYTE , buffer ) ;
98
+ debug ( 'content length:' + Buffer . byteLength ( value , 'utf8' ) ) ;
99
+ debug ( value ) ;
100
+ debug ( buffer . length ) ;
101
+ result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , Buffer . byteLength ( value , 'utf8' ) ) ;
123
102
break ;
124
- case windef . REG_BINARY :
103
+ case windef . REG_VALUE_TYPE . REG_BINARY :
125
104
// we assume that the value is a buffer since it should be binary data
126
105
buffer = value ;
106
+ byte = ref . alloc ( types . LPBYTE , buffer ) ;
107
+ result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
127
108
break ;
128
109
case windef . REG_VALUE_TYPE . REG_DWORD :
129
110
case windef . REG_VALUE_TYPE . REG_DWORD_BIG_ENDIAN :
130
111
case windef . REG_VALUE_TYPE . REG_DWORD_LITTLE_ENDIAN :
131
112
buffer = new Buffer ( 4 , value ) ;
113
+ result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
132
114
break ;
133
115
default :
134
116
throw 'The type ' + valueType + ' is currently unsupported' ;
135
117
}
136
118
137
- byte = ref . alloc ( types . LPBYTE , buffer ) ;
138
-
139
- var result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
140
-
141
119
if ( result !== 0 ) {
142
120
throw 'Failed to open key error: ' + error [ result ] ;
143
121
}
@@ -155,15 +133,15 @@ var api = {
155
133
var result = advApi . RegDeleteTreeA ( key . handle . deref ( ) , subKeyName ) ;
156
134
157
135
if ( result !== 0 ) {
158
- throw 'Failed to open key error ' + result + ': + error[result]' ;
136
+ throw 'Failed to open key error ' + result + ':' + error [ result ] ;
159
137
}
160
138
} ,
161
139
closeKey : function ( key ) {
162
- console . log ( 'KEY:' + key ) ;
140
+ debug ( 'KEY:' + key ) ;
163
141
var result = advApi . RegCloseKey ( key . handle . deref ( ) ) ;
164
142
165
143
if ( result !== 0 ) {
166
- throw 'Failed to open key error ' + result + ': + error[result]' ;
144
+ throw 'Failed to open key error ' + result + ':' + error [ result ] ;
167
145
}
168
146
}
169
147
} ;
0 commit comments