1919 * Methods for accessing the Windows Registry. Only String and DWORD values
2020 * supported at the moment.
2121 * <p>
22- * Not sure of where this code came from originally, but it was hacked on
23- * 20 July 2013 to make updates for use with JNA 3.5.2's platform classes.
22+ * Not sure where this code came from originally (if you know the reference,
23+ * please get in touch so that we can add a proper citation). Several changes
24+ * were made to update it for JNA 3.5.2's platform classes and clean up the
25+ * syntax to make it less like a C program. [fry 130720]
2426 */
2527public class WindowsRegistry {
2628 static public enum REGISTRY_ROOT_KEY {
@@ -45,16 +47,15 @@ static public enum REGISTRY_ROOT_KEY {
4547 * @return root key
4648 */
4749 private static HKEY getRegistryRootKey (REGISTRY_ROOT_KEY key ) {
48- Advapi32 advapi32 ;
50+ // Advapi32 advapi32;
4951 //IntByReference pHandle;
50- HKEYByReference pHandle ;
5152 //int handle = 0;
52- HKEY handle = null ;
5353
54- advapi32 = Advapi32 .INSTANCE ;
54+ Advapi32 advapi32 = Advapi32 .INSTANCE ;
5555// pHandle = new IntByReference();
56- pHandle = new WinReg .HKEYByReference ();
56+ HKEYByReference pHandle = new WinReg .HKEYByReference ();
5757
58+ HKEY handle = null ;
5859 if (advapi32 .RegOpenKeyEx (rootKeyMap .get (key ), null , 0 , 0 , pHandle ) == WinError .ERROR_SUCCESS ) {
5960 handle = pHandle .getValue ();
6061 }
@@ -128,13 +129,13 @@ static public String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName
128129 //IntByReference pType, lpcbData;
129130 byte [] lpData = new byte [1 ];
130131 //int handle = 0;
131- String ret = null ;
132132
133133 Advapi32 advapi32 = Advapi32 .INSTANCE ;
134134 IntByReference pType = new IntByReference ();
135135 IntByReference lpcbData = new IntByReference ();
136136 HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ );
137137
138+ String ret = null ;
138139 //if (handle != 0) {
139140 if (handle != null ) {
140141 //if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WinError.ERROR_MORE_DATA) {
@@ -155,26 +156,19 @@ static public String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName
155156 /**
156157 * Read an int value.
157158 *
158- *
159159 * @return int or 0
160160 * @param rootKey root key
161161 * @param subKeyName key name
162162 * @param name value name
163163 */
164164 static public int getIntValue (REGISTRY_ROOT_KEY rootKey , String subKeyName , String name ) {
165- Advapi32 advapi32 ;
166- IntByReference pType ;
167- IntByReference lpcbData ;
168- byte [] lpData = new byte [1 ];
169- //int handle = 0;
170- HKEY handle = null ;
171- int ret = 0 ;
172-
173- advapi32 = Advapi32 .INSTANCE ;
174- pType = new IntByReference ();
175- lpcbData = new IntByReference ();
176- handle = openKey (rootKey , subKeyName , WinNT .KEY_READ );
165+ Advapi32 advapi32 = Advapi32 .INSTANCE ;
166+ IntByReference pType = new IntByReference ();
167+ IntByReference lpcbData = new IntByReference ();
168+ HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ );
177169
170+ int ret = 0 ;
171+ byte [] lpData = new byte [1 ];
178172 //if(handle != 0) {
179173 if (handle != null ) {
180174 //if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WinError.ERROR_MORE_DATA) {
@@ -203,10 +197,10 @@ static public int getIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, Stri
203197 static public boolean deleteValue (REGISTRY_ROOT_KEY rootKey , String subKeyName , String name ) {
204198 Advapi32 advapi32 = Advapi32 .INSTANCE ;
205199 //int handle;
206- boolean ret = true ;
207200
208201 HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ | WinNT .KEY_WRITE );
209202
203+ boolean ret = true ;
210204 //if(handle != 0) {
211205 if (handle != null ) {
212206 if (advapi32 .RegDeleteValue (handle , name ) == WinError .ERROR_SUCCESS ) {
@@ -230,18 +224,18 @@ static public boolean deleteValue(REGISTRY_ROOT_KEY rootKey, String subKeyName,
230224 */
231225 static public boolean setStringValue (REGISTRY_ROOT_KEY rootKey , String subKeyName , String name , String value ) throws UnsupportedEncodingException {
232226 //int handle;
233- byte [] data ;
234- boolean ret = false ;
227+ //byte[] data;
235228
236229 // appears to be Java 1.6 syntax, removing [fry]
237230 //data = Arrays.copyOf(value.getBytes("UTF-16LE"), value.length() * 2 + 2);
238- data = new byte [value .length () * 2 + 2 ];
231+ byte [] data = new byte [value .length () * 2 + 2 ];
239232 byte [] src = value .getBytes ("UTF-16LE" );
240233 System .arraycopy (src , 0 , data , 0 , src .length );
241234
242235 Advapi32 advapi32 = Advapi32 .INSTANCE ;
243236 HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ | WinNT .KEY_WRITE );
244237
238+ boolean ret = false ;
245239 //if(handle != 0) {
246240 if (handle != null ) {
247241 if (advapi32 .RegSetValueEx (handle , name , 0 , WinNT .REG_SZ , data , data .length ) == WinError .ERROR_SUCCESS ) {
@@ -256,27 +250,26 @@ static public boolean setStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyNam
256250 /**
257251 * Writes an int value.
258252 *
259- *
260253 * @return true on success
261254 * @param rootKey root key
262255 * @param subKeyName key name
263256 * @param name value name
264257 * @param value value
265258 */
266259 static public boolean setIntValue (REGISTRY_ROOT_KEY rootKey , String subKeyName , String name , int value ) {
267- Advapi32 advapi32 ;
260+ // Advapi32 advapi32;
268261 //int handle;
269- byte [] data ;
270- boolean ret = false ;
262+ //byte[] data;
271263
272- data = new byte [4 ];
264+ byte [] data = new byte [4 ];
273265 data [0 ] = (byte )(value & 0xff );
274266 data [1 ] = (byte )((value >> 8 ) & 0xff );
275267 data [2 ] = (byte )((value >> 16 ) & 0xff );
276268 data [3 ] = (byte )((value >> 24 ) & 0xff );
277- advapi32 = Advapi32 .INSTANCE ;
269+ Advapi32 advapi32 = Advapi32 .INSTANCE ;
278270 HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ | WinNT .KEY_WRITE );
279271
272+ boolean ret = false ;
280273 //if(handle != 0) {
281274 if (handle != null ) {
282275 if (advapi32 .RegSetValueEx (handle , name , 0 , WinNT .REG_DWORD , data , data .length ) == WinError .ERROR_SUCCESS ) {
@@ -298,16 +291,16 @@ static public boolean setIntValue(REGISTRY_ROOT_KEY rootKey, String subKeyName,
298291 */
299292 static public boolean valueExists (REGISTRY_ROOT_KEY rootKey , String subKeyName , String name ) {
300293 //Advapi32 advapi32;
301- IntByReference pType , lpcbData ;
302- byte [] lpData = new byte [1 ];
294+ //IntByReference pType, lpcbData;
303295 //int handle = 0;
304- boolean ret = false ;
305296
306297 Advapi32 advapi32 = Advapi32 .INSTANCE ;
307- pType = new IntByReference ();
308- lpcbData = new IntByReference ();
298+ IntByReference pType = new IntByReference ();
299+ IntByReference lpcbData = new IntByReference ();
309300 HKEY handle = openKey (rootKey , subKeyName , WinNT .KEY_READ );
310301
302+ byte [] lpData = new byte [1 ];
303+ boolean ret = false ;
311304 //if(handle != 0) {
312305 if (handle != null ) {
313306 //if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) != WinError.ERROR_FILE_NOT_FOUND) {
@@ -335,14 +328,14 @@ static public boolean createKey(REGISTRY_ROOT_KEY rootKey, String parent, String
335328 //Advapi32 advapi32;
336329 //IntByReference hkResult, dwDisposition;
337330 //int handle = 0;
338- boolean ret = false ;
339331
340332 Advapi32 advapi32 = Advapi32 .INSTANCE ;
341333 //IntByReference hkResult = new IntByReference();
342334 HKEYByReference hkResult = new HKEYByReference ();
343335 IntByReference dwDisposition = new IntByReference ();
344336 HKEY handle = openKey (rootKey , parent , WinNT .KEY_READ );
345337
338+ boolean ret = false ;
346339 //if(handle != 0) {
347340 if (handle != null ) {
348341 if (advapi32 .RegCreateKeyEx (handle , name , 0 , null , WinNT .REG_OPTION_NON_VOLATILE , WinNT .KEY_READ , null ,
@@ -370,11 +363,11 @@ static public boolean createKey(REGISTRY_ROOT_KEY rootKey, String parent, String
370363 static public boolean deleteKey (REGISTRY_ROOT_KEY rootKey , String parent , String name ) {
371364 //Advapi32 advapi32;
372365 //int handle = 0;
373- boolean ret = false ;
374366
375367 Advapi32 advapi32 = Advapi32 .INSTANCE ;
376368 HKEY handle = openKey (rootKey , parent , WinNT .KEY_READ );
377369
370+ boolean ret = false ;
378371 //if(handle != 0) {
379372 if (handle != null ) {
380373 if (advapi32 .RegDeleteKey (handle , name ) == WinError .ERROR_SUCCESS ) {
@@ -399,23 +392,22 @@ static public boolean deleteKey(REGISTRY_ROOT_KEY rootKey, String parent, String
399392 static public String [] getSubKeys (REGISTRY_ROOT_KEY rootKey , String parent ) {
400393 //Advapi32 advapi32;
401394 //int handle = 0, dwIndex;
402- char [] lpName ;
403- IntByReference lpcName ;
404- WinBase .FILETIME lpftLastWriteTime ;
395+ // char[] lpName;
396+ // IntByReference lpcName;
397+ // WinBase.FILETIME lpftLastWriteTime;
405398 TreeSet <String > subKeys = new TreeSet <String >();
406-
407399 Advapi32 advapi32 = Advapi32 .INSTANCE ;
408400 HKEY handle = openKey (rootKey , parent , WinNT .KEY_READ );
409- lpName = new char [256 ];
410- lpcName = new IntByReference (256 );
411- lpftLastWriteTime = new WinBase .FILETIME ();
401+ char [] lpName = new char [256 ];
402+ IntByReference lpcName = new IntByReference (256 );
403+ WinBase . FILETIME lpftLastWriteTime = new WinBase .FILETIME ();
412404
413405 //if(handle != 0) {
414406 if (handle != null ) {
415407 int dwIndex = 0 ;
416408
417- while (advapi32 .RegEnumKeyEx (handle , dwIndex , lpName , lpcName , null ,
418- null , null , lpftLastWriteTime ) == WinError .ERROR_SUCCESS ) {
409+ while (advapi32 .RegEnumKeyEx (handle , dwIndex , lpName , lpcName , null ,
410+ null , null , lpftLastWriteTime ) == WinError .ERROR_SUCCESS ) {
419411 subKeys .add (new String (lpName , 0 , lpcName .getValue ()));
420412 lpcName .setValue (256 );
421413 dwIndex ++;
@@ -437,25 +429,27 @@ static public String[] getSubKeys(REGISTRY_ROOT_KEY rootKey, String parent) {
437429 static public TreeMap <String , Object > getValues (REGISTRY_ROOT_KEY rootKey , String key ) throws UnsupportedEncodingException {
438430 //Advapi32 advapi32;
439431 //int handle = 0, dwIndex, result = 0;
440- char [] lpValueName ;
441- byte [] lpData ;
442- IntByReference lpcchValueName , lpType , lpcbData ;
443- String name ;
444- TreeMap <String , Object > values = new TreeMap <String , Object >(String .CASE_INSENSITIVE_ORDER );
432+ //char[] lpValueName;
433+ //byte[] lpData;
434+ //IntByReference lpcchValueName, lpType, lpcbData;
435+ //String name;
436+ TreeMap <String , Object > values =
437+ new TreeMap <String , Object >(String .CASE_INSENSITIVE_ORDER );
445438
446439 Advapi32 advapi32 = Advapi32 .INSTANCE ;
447440 HKEY handle = openKey (rootKey , key , WinNT .KEY_READ );
448- lpValueName = new char [16384 ];
449- lpcchValueName = new IntByReference (16384 );
450- lpType = new IntByReference ();
451- lpData = new byte [1 ];
452- lpcbData = new IntByReference ();
441+ char [] lpValueName = new char [16384 ];
442+ IntByReference lpcchValueName = new IntByReference (16384 );
443+ IntByReference lpType = new IntByReference ();
444+ byte [] lpData = new byte [1 ];
445+ IntByReference lpcbData = new IntByReference ();
453446
454447 //if(handle != 0) {
455448 if (handle != null ) {
456449 int dwIndex = 0 ;
457450 int result = 0 ;
458-
451+ String name ;
452+
459453 do {
460454 lpcbData .setValue (0 );
461455 result = advapi32 .RegEnumValue (handle , dwIndex , lpValueName , lpcchValueName , null ,
0 commit comments