Package com.sun.jna.examples.win32.WinReg

Examples of com.sun.jna.examples.win32.WinReg.HKEYByReference


   *  Regitry key path.
   * @return
   *  Table of values.
   */
  public static TreeMap<String, Object> registryGetValues(HKEY root, String keyPath) {
    HKEYByReference phkKey = new HKEYByReference();
    int rc = Advapi32.INSTANCE.RegOpenKeyEx(root, keyPath, 0, WinNT.KEY_READ, phkKey);
    if (rc != W32Errors.ERROR_SUCCESS) {      throw new Win32Exception(rc);
    }
    try {
      return registryGetValues(phkKey.getValue());
    } finally {
      rc = Advapi32.INSTANCE.RegCloseKey(phkKey.getValue());
      if (rc != W32Errors.ERROR_SUCCESS) {
        throw new Win32Exception(rc);
      }
    }   
  }
View Full Code Here


    public static void main(String[] args) {
        junit.textui.TestRunner.run(NtDllUtilTest.class);
    }

    public void testGetKeyName() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
      assertEquals("Software", NtDllUtil.getKeyName(phKey.getValue()));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));
    }
View Full Code Here

            null, userInfo.usri1_name.toString()));     
    }
    }
   
    public void testRegOpenKeyEx() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft", 0, WinNT.KEY_READ, phKey));
      assertTrue(WinBase.INVALID_HANDLE_VALUE != phKey.getValue());
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));     
    }
View Full Code Here

      assertTrue(WinBase.INVALID_HANDLE_VALUE != phKey.getValue());
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));     
    }
   
    public void testRegQueryValueEx() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings", 0, WinNT.KEY_READ, phKey));
      IntByReference lpcbData = new IntByReference();
      IntByReference lpType = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phKey.getValue(), "User Agent", 0, lpType, (char[]) null, lpcbData));
      assertEquals(WinNT.REG_SZ, lpType.getValue());
      assertTrue(lpcbData.getValue() > 0);
      char[] buffer = new char[lpcbData.getValue()];
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phKey.getValue(), "User Agent", 0, lpType, buffer, lpcbData));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));     
    }
View Full Code Here

      assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Advapi32.INSTANCE.RegDeleteValue(
          WinReg.HKEY_CURRENT_USER, "JNAAdvapi32TestDoesntExist"));
    }
   
    public void testRegSetValueEx_REG_SZ() {
      HKEYByReference phKey = new HKEYByReference();
      // create parent key
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
      HKEYByReference phkTest = new HKEYByReference();
      IntByReference lpdwDisposition = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCreateKeyEx(
          phKey.getValue(), "JNAAdvapi32Test", 0, null, 0, WinNT.KEY_ALL_ACCESS,
          null, phkTest, lpdwDisposition));
      // write a REG_SZ value
      char[] lpData = Native.toCharArray("Test");
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegSetValueEx(
          phkTest.getValue(), "REG_SZ", 0, WinNT.REG_SZ, lpData, lpData.length * 2));
      // re-read the REG_SZ value
      IntByReference lpType = new IntByReference();
      IntByReference lpcbData = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phkTest.getValue(), "REG_SZ", 0, lpType, (char[]) null, lpcbData));
      assertEquals(WinNT.REG_SZ, lpType.getValue());
      assertTrue(lpcbData.getValue() > 0);
      char[] buffer = new char[lpcbData.getValue()];
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phkTest.getValue(), "REG_SZ", 0, lpType, buffer, lpcbData));
      assertEquals("Test", Native.toString(buffer));
      // delete the test key
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(
          phkTest.getValue()));                 
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegDeleteKey(
          phKey.getValue(), "JNAAdvapi32Test"));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));                 
    }
View Full Code Here

          phKey.getValue(), "JNAAdvapi32Test"));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));                 
    }
   
    public void testRegSetValueEx_DWORD() {
      HKEYByReference phKey = new HKEYByReference();
      // create parent key
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
      HKEYByReference phkTest = new HKEYByReference();
      IntByReference lpdwDisposition = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCreateKeyEx(
          phKey.getValue(), "JNAAdvapi32Test", 0, null, 0, WinNT.KEY_ALL_ACCESS,
          null, phkTest, lpdwDisposition));
      // write a REG_DWORD value
      int value = 42145;
        byte[] data = new byte[4];
        data[0] = (byte)(value & 0xff);
        data[1] = (byte)((value >> 8) & 0xff);
        data[2] = (byte)((value >> 16) & 0xff);
        data[3] = (byte)((value >> 24) & 0xff);
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegSetValueEx(
          phkTest.getValue(), "DWORD", 0, WinNT.REG_DWORD, data, 4));     
      // re-read the REG_DWORD value
      IntByReference lpType = new IntByReference();
      IntByReference lpcbData = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phkTest.getValue(), "DWORD", 0, lpType, (char[]) null, lpcbData));
      assertEquals(WinNT.REG_DWORD, lpType.getValue());
      assertEquals(4, lpcbData.getValue());
      IntByReference valueRead = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryValueEx(
          phkTest.getValue(), "DWORD", 0, lpType, valueRead, lpcbData));
      assertEquals(value, valueRead.getValue());
      // delete the test key
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(
          phkTest.getValue()));                 
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegDeleteKey(
          phKey.getValue(), "JNAAdvapi32Test"));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));                 
    }
View Full Code Here

          phKey.getValue(), "JNAAdvapi32Test"));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));                 
    }
   
    public void testRegCreateKeyEx() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
      HKEYByReference phkResult = new HKEYByReference();
      IntByReference lpdwDisposition = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCreateKeyEx(
          phKey.getValue(), "JNAAdvapi32Test", 0, null, 0, WinNT.KEY_ALL_ACCESS,
          null, phkResult, lpdwDisposition));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phkResult.getValue()));                 
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegDeleteKey(
          phKey.getValue(), "JNAAdvapi32Test"));
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));           
    }
View Full Code Here

      assertEquals(W32Errors.ERROR_FILE_NOT_FOUND, Advapi32.INSTANCE.RegDeleteKey(
          WinReg.HKEY_CURRENT_USER, "JNAAdvapi32TestDoesntExist"));
    }
   
    public void testRegEnumKeyEx() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
          0, WinNT.KEY_READ, phKey));
      IntByReference lpcSubKeys = new IntByReference();
      IntByReference lpcMaxSubKeyLen = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryInfoKey(
          phKey.getValue(), null, null, null, lpcSubKeys, lpcMaxSubKeyLen, null, null,
          null, null, null, null));
      char[] name = new char[lpcMaxSubKeyLen.getValue() + 1];
      for (int i = 0; i < lpcSubKeys.getValue(); i++) {
        IntByReference lpcchValueName = new IntByReference(lpcMaxSubKeyLen.getValue() + 1);
          assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegEnumKeyEx(
              phKey.getValue(), i, name, lpcchValueName, null, null, null, null));
          assertEquals(Native.toString(name).length(), lpcchValueName.getValue());
      }
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));           
    }
View Full Code Here

      }
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));           
    }
   
    public void testRegEnumValue() {
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings",
          0, WinNT.KEY_READ, phKey));
      IntByReference lpcValues = new IntByReference();
      IntByReference lpcMaxValueNameLen = new IntByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegQueryInfoKey(
          phKey.getValue(), null, null, null, null, null, null, lpcValues,
          lpcMaxValueNameLen, null, null, null));
      char[] name = new char[lpcMaxValueNameLen.getValue() + 1];
      for (int i = 0; i < lpcValues.getValue(); i++) {
        IntByReference lpcchValueName = new IntByReference(lpcMaxValueNameLen.getValue() + 1);
        IntByReference lpType = new IntByReference();
          assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegEnumValue(
              phKey.getValue(), i, name, lpcchValueName, null,
              lpType, null, null));
          assertEquals(Native.toString(name).length(), lpcchValueName.getValue());
      }
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));     
    }
View Full Code Here

        junit.textui.TestRunner.run(NtDllTest.class);
    }
   
    public void testZwQueryKey() {
      // open a key
      HKEYByReference phKey = new HKEYByReference();
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegOpenKeyEx(
          WinReg.HKEY_CURRENT_USER, "Software", 0, WinNT.KEY_WRITE | WinNT.KEY_READ, phKey));
      // query key info
      IntByReference resultLength = new IntByReference();     
      assertEquals(NTStatus.STATUS_BUFFER_TOO_SMALL, NtDll.INSTANCE.ZwQueryKey(
          phKey.getValue(), KEY_INFORMATION_CLASS.KeyBasicInformation,
          null, 0, resultLength));
      assertTrue(resultLength.getValue() > 0);
      KEY_BASIC_INFORMATION keyInformation = new KEY_BASIC_INFORMATION(resultLength.getValue());
      assertEquals(NTStatus.STATUS_SUCCESS, NtDll.INSTANCE.ZwQueryKey(
          phKey.getValue(), Wdm.KEY_INFORMATION_CLASS.KeyBasicInformation,
          keyInformation, resultLength.getValue(), resultLength));     
      // show
      assertEquals("Software", keyInformation.getName());
      // close key
      assertEquals(W32Errors.ERROR_SUCCESS, Advapi32.INSTANCE.RegCloseKey(phKey.getValue()));                       
    }
View Full Code Here

TOP

Related Classes of com.sun.jna.examples.win32.WinReg.HKEYByReference

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.