⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 registry.java

📁 Java调用Windows API,支持Office
💻 JAVA
字号:
//******************************************************************
// Released under the DevelopMentor OpenSource Software License.
// Please consult the LICENSE file in the project root directory,
// or at http://www.develop.com for details before using this
// software.
//******************************************************************

package org.jawin.donated.win32;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import org.jawin.COMError;
import org.jawin.COMException;
import org.jawin.FuncPtr;
import org.jawin.ReturnFlags;
import org.jawin.io.LittleEndianInputStream;
import org.jawin.io.LittleEndianOutputStream;
import org.jawin.io.NakedByteStream;
import org.jawin.marshal.StructConverter;

public class Registry {

  static private final FuncPtr fpOK;
  static private final FuncPtr fpQV;
  static private final FuncPtr fpCK;
  static private final FuncPtr fpCrK;
  static private final FuncPtr fpDK;
  static private final FuncPtr fpEnum;
  static private final FuncPtr fpEnumV;
  static 
  {
    try {
    fpOK = new FuncPtr("ADVAPI32.DLL", "RegOpenKeyW");
    fpQV = new FuncPtr("ADVAPI32.DLL", "RegQueryValueExW");
    fpCK = new FuncPtr("ADVAPI32.DLL", "RegCloseKey");
    fpCrK = new FuncPtr("ADVAPI32.DLL", "RegCreateKeyW");
    fpDK = new FuncPtr("ADVAPI32.DLL", "RegDeleteKeyW");
    fpEnum = new FuncPtr("ADVAPI32.DLL", "RegEnumKeyW");
    fpEnumV = new FuncPtr("ADVAPI32.DLL", "RegEnumValueW");
    
    } catch (COMException ce) {
      throw new COMError("Unable to load registry entry points");
    }
  } 
	
	public static int OpenKey(int key, String subkey) throws IOException, COMException {
		return fpOK.invoke_OI(key, subkey, ReturnFlags.CHECK_W32);
	}

	public static int CreateKey(int key, String subkey) throws IOException, COMException {
		return fpCrK.invoke_OI(key, subkey, ReturnFlags.CHECK_W32);
	}

	public static void DeleteKey(int key, String subkey) throws IOException, COMException {
		fpDK.invoke_I(key, subkey, ReturnFlags.CHECK_W32);
	}

  public static String QueryStringValue(int key, String subkey)
    throws IOException, COMException
  {
    byte[] raw = RawQueryValue(key, subkey);
    LittleEndianInputStream leis = new LittleEndianInputStream
      (new ByteArrayInputStream(raw));
    return leis.readUnicodeSz(raw.length/2);
  }
	
  public static byte[] RawQueryValue(int key, String subkey)
    throws IOException, COMException
  {
    NakedByteStream nbs = new NakedByteStream();
    LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
    //top level args
    leos.writeInt(key);
    leos.writeStringUnicode(subkey);
    leos.writeInt(0);	//reserved
    leos.writeInt(0);	//type (ignore)

    byte[] sizeRes = fpQV.invoke("IGIIkA:T7:L20n4", 
					     24, nbs, 
					     null, ReturnFlags.CHECK_NONE); // to get the buffer size
    int size = StructConverter.bytesIntoInt(sizeRes, 0);
    leos.writeInt(size);
    byte[] res = fpQV.invoke("IGIIM" + size +"P4:T7:L16n" + size, 
					 24, nbs, 
					 null, ReturnFlags.CHECK_W32);

    return res;		
  }
	
	public static void CloseKey(int key) throws IOException, COMException {
		fpCK.invoke_I(key, ReturnFlags.CHECK_W32);
	}

  public static String RegEnumKey(int key, int index)
    throws Exception
  {
    NakedByteStream nbs = new NakedByteStream();
    LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
	
    int size = 520;
	
    // top level args
    leos.writeInt(key);
    leos.writeInt(index);
    leos.writeInt(size);
	
    byte[] result;
    try
      {
	result = fpEnum.invoke("IIM" + size + "I:T7:L8n" + size, 
					 16, nbs, 
					 null, ReturnFlags.CHECK_W32);
      }
    catch (COMException e)
      {
	if (e.hresult == 0x80070103)
	  {
	    return null;
	  }
	throw e;
      }
	
    LittleEndianInputStream leis = new LittleEndianInputStream
      (new ByteArrayInputStream(result));
    return leis.readUnicodeSz(result.length / 2);
  }
  public static String RegEnumValue(int key, int index)
    throws Exception
  {
    NakedByteStream nbs = new NakedByteStream();
    LittleEndianOutputStream leos = new LittleEndianOutputStream(nbs);
	
    int size = 520;
	
    // top level args
    leos.writeInt(key);
    leos.writeInt(index);
    leos.writeInt(size);
	
    byte[] result;
    try
      {
	result = fpEnumV.invoke("IIA" + size + "I:T7:L8n" + size, //need to finish
					 32, nbs, 
					 null, ReturnFlags.CHECK_W32);
      }
    catch (COMException e)
      {
	if (e.hresult == 0x80070103)
	  {
	    return null;
	  }
	throw e;
      }
	
    LittleEndianInputStream leis = new LittleEndianInputStream
      (new ByteArrayInputStream(result));
    return leis.readUnicodeSz(result.length / 2);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -