1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WinRegKey.java,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com
.sun
.star
.lib
.loader
;
33 import java
.io
.BufferedInputStream
;
34 import java
.io
.BufferedOutputStream
;
36 import java
.io
.FileOutputStream
;
37 import java
.io
.InputStream
;
41 * This class provides functionality for reading string values from the
42 * Windows Registry. It requires the native library unowinreg.dll.
44 final class WinRegKey
{
46 private String m_rootKeyName
;
47 private String m_subKeyName
;
49 // native methods to access the windows registry
50 private static native boolean winreg_RegOpenClassesRoot( long[] hkresult
);
51 private static native boolean winreg_RegOpenCurrentConfig(
53 private static native boolean winreg_RegOpenCurrentUser( long[] hkresult
);
54 private static native boolean winreg_RegOpenLocalMachine( long[] hkresult
);
55 private static native boolean winreg_RegOpenUsers( long[] hkresult
);
56 private static native boolean winreg_RegOpenKeyEx( long parent
, String name
,
58 private static native boolean winreg_RegCloseKey( long hkey
);
59 private static native boolean winreg_RegQueryValueEx(
60 long hkey
, String value
, long[] type
,
61 byte[] data
, long[] size
);
62 private static native boolean winreg_RegQueryInfoKey(
63 long hkey
, long[] subkeys
, long[] maxSubkeyLen
,
64 long[] values
, long[] maxValueNameLen
,
65 long[] maxValueLen
, long[] secDescriptor
);
67 // load the native library unowinreg.dll
70 ClassLoader cl
= WinRegKey
.class.getClassLoader();
71 InputStream is
= cl
.getResourceAsStream( "win/unowinreg.dll" );
73 // generate a temporary name for lib file and write to temp
75 BufferedInputStream istream
= new BufferedInputStream( is
);
76 File libfile
= File
.createTempFile( "unowinreg", ".dll" );
77 libfile
.deleteOnExit(); // ensure deletion
78 BufferedOutputStream ostream
= new BufferedOutputStream(
79 new FileOutputStream( libfile
) );
80 int bsize
= 2048; int n
= 0;
81 byte[] buffer
= new byte[bsize
];
82 while ( ( n
= istream
.read( buffer
, 0, bsize
) ) != -1 ) {
83 ostream
.write( buffer
, 0, n
);
88 System
.load( libfile
.getPath() );
90 // If the library cannot be found as a class loader resource,
91 // try the global System.loadLibrary(). The JVM will look for
92 // it in the java.library.path.
93 System
.loadLibrary( "unowinreg" );
95 } catch ( java
.lang
.Exception e
) {
96 System
.err
.println( "com.sun.star.lib.loader.WinRegKey: " +
97 "loading of native library failed!" + e
);
102 * Constructs a <code>WinRegKey</code>.
104 public WinRegKey( String rootKeyName
, String subKeyName
) {
105 m_rootKeyName
= rootKeyName
;
106 m_subKeyName
= subKeyName
;
110 * Reads a string value for the specified value name.
112 public String
getStringValue( String valueName
) throws WinRegKeyException
{
113 byte[] data
= getValue( valueName
);
114 // remove terminating null character
115 return new String( data
, 0, data
.length
- 1 );
119 * Reads a value for the specified value name.
121 private byte[] getValue( String valueName
) throws WinRegKeyException
{
123 byte[] result
= null;
126 // open the specified registry key
127 boolean bRet
= false;
129 if ( m_rootKeyName
.equals( "HKEY_CLASSES_ROOT" ) ) {
130 bRet
= winreg_RegOpenClassesRoot( hroot
);
131 } else if ( m_rootKeyName
.equals( "HKEY_CURRENT_CONFIG" ) ) {
132 bRet
= winreg_RegOpenCurrentConfig( hroot
);
133 } else if ( m_rootKeyName
.equals( "HKEY_CURRENT_USER" ) ) {
134 bRet
= winreg_RegOpenCurrentUser( hroot
);
135 } else if ( m_rootKeyName
.equals( "HKEY_LOCAL_MACHINE" ) ) {
136 bRet
= winreg_RegOpenLocalMachine( hroot
);
137 } else if ( m_rootKeyName
.equals( "HKEY_USERS" ) ) {
138 bRet
= winreg_RegOpenUsers( hroot
);
140 throw new WinRegKeyException( "unknown root registry key!");
143 throw new WinRegKeyException( "opening root registry key " +
146 if ( !winreg_RegOpenKeyEx( hroot
[0], m_subKeyName
, hkey
) ) {
147 if ( !winreg_RegCloseKey( hroot
[0] ) ) {
148 throw new WinRegKeyException( "opening registry key and " +
149 "releasing root registry key handle failed!" );
151 throw new WinRegKeyException( "opening registry key failed!" );
154 // get the size of the longest data component among the key's values
155 long[] subkeys
= {0};
156 long[] maxSubkeyLen
= {0};
158 long[] maxValueNameLen
= {0};
159 long[] maxValueLen
= {0};
160 long[] secDescriptor
= {0};
161 if ( !winreg_RegQueryInfoKey( hkey
[0], subkeys
, maxSubkeyLen
,
162 values
, maxValueNameLen
, maxValueLen
, secDescriptor
) ) {
163 if ( !winreg_RegCloseKey( hkey
[0] ) ||
164 !winreg_RegCloseKey( hroot
[0] ) ) {
165 throw new WinRegKeyException( "retrieving information about " +
166 "the registry key and releasing registry key handles " +
169 throw new WinRegKeyException( "retrieving information about " +
170 "the registry key failed!" );
173 // get the data for the specified value name
174 byte[] buffer
= new byte[ (int) maxValueLen
[0] ];
175 long[] size
= new long[1];
176 size
[0] = buffer
.length
;
177 long[] type
= new long[1];
179 if ( !winreg_RegQueryValueEx( hkey
[0], valueName
, type
, buffer
,
181 if ( !winreg_RegCloseKey( hkey
[0] ) ||
182 !winreg_RegCloseKey( hroot
[0] ) ) {
183 throw new WinRegKeyException( "retrieving data for the " +
184 "specified value name and releasing registry key handles " +
187 throw new WinRegKeyException( "retrieving data for the " +
188 "specified value name failed!" );
191 // release registry key handles
192 if ( !winreg_RegCloseKey( hkey
[0] ) ||
193 !winreg_RegCloseKey( hroot
[0] ) ) {
194 throw new WinRegKeyException( "releasing registry key handles " +
198 result
= new byte[ (int) size
[0] ];
199 System
.arraycopy( buffer
, 0, result
, 0, (int)size
[0] );