22'use strict' ;
33var ffi = require ( 'ffi' ) ,
44 types = require ( './types' ) ,
5+ advApi = require ( './native/adv_api' ) ,
56 Key = require ( './key' ) ,
67 ref = require ( 'ref' ) ,
78 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' ) ;
4311
4412var api = {
4513 openKeyFromPredefined : function ( preDefinedKey , subKeyName , accessLevel ) {
@@ -49,7 +17,7 @@ var api = {
4917
5018 var pHkey = ref . alloc ( types . PHKEY ) ;
5119 var result = advApi . RegOpenKeyExA ( preDefinedKey , subKeyName , 0 , accessLevel , pHkey ) ;
52- console . log ( 'result:' + result ) ;
20+ debug ( 'result:' + result ) ;
5321 if ( result !== 0 ) {
5422 throw 'Failed to open key error: ' + error [ result ] ;
5523 }
@@ -80,7 +48,6 @@ var api = {
8048 // READ VALUE
8149 var value = new Buffer ( pKeyDataLength . readUInt32LE ( ) ) ,
8250 valueType = pKeyType . readUInt32LE ( ) ;
83- console . log ( valueType === 1 ) ;
8451 switch ( valueType ) {
8552 case windef . REG_VALUE_TYPE . REG_SZ :
8653 case windef . REG_VALUE_TYPE . REG_EXPAND_SZ :
@@ -106,38 +73,49 @@ var api = {
10673 throw 'Failed to open key error: ' + error [ result ] ;
10774 }
10875
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 ;
11083 } ,
11184 setValueForKeyObject : function ( key , valueName , valueType , value ) {
11285 if ( valueType < 1 || valueType > 8 ) {
11386 throw 'Invalid valueType parameter: ' + valueType + ' use values from windef.REG_VALUE_TYPE' ;
11487 }
11588 var buffer ,
116- byte ;
89+ byte ,
90+ result ;
11791
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' ) ) ;
123102 break ;
124- case windef . REG_BINARY :
103+ case windef . REG_VALUE_TYPE . REG_BINARY :
125104 // we assume that the value is a buffer since it should be binary data
126105 buffer = value ;
106+ byte = ref . alloc ( types . LPBYTE , buffer ) ;
107+ result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
127108 break ;
128109 case windef . REG_VALUE_TYPE . REG_DWORD :
129110 case windef . REG_VALUE_TYPE . REG_DWORD_BIG_ENDIAN :
130111 case windef . REG_VALUE_TYPE . REG_DWORD_LITTLE_ENDIAN :
131112 buffer = new Buffer ( 4 , value ) ;
113+ result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
132114 break ;
133115 default :
134116 throw 'The type ' + valueType + ' is currently unsupported' ;
135117 }
136118
137- byte = ref . alloc ( types . LPBYTE , buffer ) ;
138-
139- var result = advApi . RegSetValueExA ( key . handle . deref ( ) , valueName , null , valueType , byte . deref ( ) , buffer . length ) ;
140-
141119 if ( result !== 0 ) {
142120 throw 'Failed to open key error: ' + error [ result ] ;
143121 }
@@ -155,15 +133,15 @@ var api = {
155133 var result = advApi . RegDeleteTreeA ( key . handle . deref ( ) , subKeyName ) ;
156134
157135 if ( result !== 0 ) {
158- throw 'Failed to open key error ' + result + ': + error[result]' ;
136+ throw 'Failed to open key error ' + result + ':' + error [ result ] ;
159137 }
160138 } ,
161139 closeKey : function ( key ) {
162- console . log ( 'KEY:' + key ) ;
140+ debug ( 'KEY:' + key ) ;
163141 var result = advApi . RegCloseKey ( key . handle . deref ( ) ) ;
164142
165143 if ( result !== 0 ) {
166- throw 'Failed to open key error ' + result + ': + error[result]' ;
144+ throw 'Failed to open key error ' + result + ':' + error [ result ] ;
167145 }
168146 }
169147} ;
0 commit comments