반응형

Access Windows Registry using 'pure' Java
(rehash of my old post since all those wonderful blog post drafts I was planning to publish are unfortunately out-dated or irrelevant)

One can use the private code of Sun's Preferences API to access values of type REG_SZ in the Windows Registry. Stupid hack. Maybe I should make a library out of this. I have used this soooo many times.

The java.util.prefs.WindowsPreferences is the concrete implementation of AbstractPreferences in the Windows platform. This class provides methods like WindowsRegQueryValueEx, etc. Using Reflection, one can use the methods in this class to query string values under HKEY_LOCAL_MACHINE and HKEY_CURRENT_USER


Given below is a code-snippet that demonstrates how to get the ProxyServer setting on Windows (this is what IE uses/sets) and the Internet Explorer version


import java.lang.reflect.Method;
import java.util.prefs.Preferences;

public class JavaRegistryHack {

private static final int HKEY_CURRENT_USER = 0x80000001;
private static final int KEY_QUERY_VALUE = 1;
private static final int KEY_SET_VALUE = 2;
private static final int KEY_READ = 0x20019;

public static void main(String args[]) {
final Preferences userRoot = Preferences.userRoot();
final Preferences systemRoot = Preferences.systemRoot();
final Class clz = userRoot.getClass();
try {
final Method openKey = clz.getDeclaredMethod("openKey",
byte[].class, int.class, int.class);
openKey.setAccessible(true);

final Method closeKey = clz.getDeclaredMethod("closeKey",
int.class);
closeKey.setAccessible(true);

final Method winRegQueryValue = clz.getDeclaredMethod(
"WindowsRegQueryValueEx", int.class, byte[].class);
winRegQueryValue.setAccessible(true);
final Method winRegEnumValue = clz.getDeclaredMethod(
"WindowsRegEnumValue1", int.class, int.class, int.class);
winRegEnumValue.setAccessible(true);
final Method winRegQueryInfo = clz.getDeclaredMethod(
"WindowsRegQueryInfoKey1", int.class);
winRegQueryInfo.setAccessible(true);


byte[] valb = null;
String vals = null;
String key = null;
Integer handle = -1;

//Query Internet Settings for Proxy
key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),
toCstr("ProxyServer"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Proxy Server = " + vals);
closeKey.invoke(Preferences.userRoot(), handle);

// Query for IE version
key = "SOFTWARE\\Microsoft\\Internet Explorer";
handle = (Integer) openKey.invoke(systemRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(systemRoot, handle, toCstr("Version"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Internet Explorer Version = " + vals);
closeKey.invoke(Preferences.systemRoot(), handle);

} catch (Exception e) {
e.printStackTrace();
}
}


private static byte[] toCstr(String str) {
byte[] result = new byte[str.length() + 1];
for (int i = 0; i < str.length(); i++) {
result[i] = (byte) str.charAt(i);
}
result[str.length()] = 0;
return result;
 }
}










<출처 : http://lenkite.blogspot.com/2008/05/access-windows-registry-using-java.html >
반응형
자바로 개인 프로젝트를 하다가 윈도우의 레지스트리에 접근하려고 자료를 찾아봤습니다.
일반적으로 시스템쪽을 컨트롤 하려면 JNI를 사용해야 되는것 같더군요.
일단 레지스트리 컨트롤 할 수 있는 라이브러리 관련 자료 정리해 봅니다.(JNI를 이용하여)

http://www.bayequities.com/tech/projects.shtml  <-- 사이트가 사라진듯..안되는군요 ^^;
사용자 삽입 이미지

jRegistryKey is a Java™ Native Interface (JNI) wrapper around the Microsoft® Windows® Win32® application programming interface (API) registry functions, designed to facilitate Windows® registry access for Java™ developers. jRegistry Key User Manual

ClassPath에 포함시킬 파일들 입니다.

invalid-file

jRegistryKey.dll





예제소스입니다.

import ca.beq.util.win32.registry.*;

public class AppMain
{
    public AppMain()
    {
        super();
    }

    public static void main(String[] args)
    {
        RegistryKey r = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "Software\\Ncsoft\\L2Client");
       
        String key = "workingdir";
        if(r.hasValue(key))
        {
           RegistryValue v = r.getValue(key);
           String value = v.toString();
           value = value.replaceAll(v.getName()+":", "").replaceAll("REG_SZ:", "").replaceAll("REG_DWORD:", "");
           System.out.println("lineage2 working dir : " + value);
   
           RegistryValue vs = r.getValue("global_version");
           System.out.println(vs.toString());
        }
        else
            System.out.println("Not Existed Key");
    }
}


레지스트리 편집기를 이용하여 값과 결과 화면을 확인해 보세요.^^
--------------------------------------------------------------------------------------------------------------------------------------
밑에 자료는 다른 패키지를 이용하는 방법이다.

Windows Registry API Native Interface

Release 3.1.3, September 11, 2003

The com.ice.jni.registry package is a Java native interface for the Windows Registry API. This allows Java program to access, modify, and export Windows Registry resources.

The com.ice.jni.registry package has been placed into the public domain. Thus, you have absolutely no licensing issues to consider. You may do anything you wish with the code. Of course, I always appreciate it when you properly credit my work.

The package will work only with Java 1.1 and greater, and uses the Javasoft native interface, not the Netscape interface. The package also includes a DLL that implements the interface. The package has been used with JDK1.2, and JDK1.3, JDK1.4, as well as JDK1.1.8.

The package includes the pre-built DLL (debug and release), source code (both the Java and the DLL's C code), as well as the compiled Java classes.

The original release was posted on November 17, 1997. The current release is 3.1.3, which was posted on September 11, 2003.

http://www.trustice.com/java/jnireg/index.shtml   <-- 여기에서 다운받으면 된다.

--------------------------------------------------------------------------------------------------------------------------------------

http://sourceforge.net/projects/java-registry/   <-- 이것도 다른 패키지인듯...

+ Recent posts