Search in sources :

Example 31 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project jna by java-native-access.

the class WTypesTest method testBSTRBasic.

public void testBSTRBasic() {
    String demoString = "inputÖäÜ?!";
    // Allocation via system and the "correct" way
    BSTR sysAllocated = OleAuto.INSTANCE.SysAllocString(demoString);
    // Java based allocation - not suitable if passed via automation
    BSTR javaAllocated = new BSTR(demoString);
    // Ensure encoding roundtripping works
    assertEquals(demoString, sysAllocated.getValue());
    assertEquals(demoString, javaAllocated.getValue());
    // BSTR is encoded as UTF-16/UCS2, so byte length is 2 * char count
    assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(sysAllocated));
    assertEquals(demoString.length(), OleAuto.INSTANCE.SysStringLen(javaAllocated));
    assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(sysAllocated));
    assertEquals(2 * demoString.length(), OleAuto.INSTANCE.SysStringByteLen(javaAllocated));
    // The BSTR Pointer points 4 bytes into the data itself (beginning of data
    // string, the 4 preceding bytes code the string length (in bytes)
    assertEquals(2 * demoString.length(), sysAllocated.getPointer().getInt(-4));
    assertEquals(2 * demoString.length(), javaAllocated.getPointer().getInt(-4));
    OleAuto.INSTANCE.SysFreeString(sysAllocated);
// javaAllocated is allocated via Memory and will be freeed by the
// garbadge collector automaticly
}
Also used : BSTR(com.sun.jna.platform.win32.WTypes.BSTR)

Example 32 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project processing by processing.

the class WindowsRegistry method getStringValue.

/**
   * Read a String value.
   *
   * @param rootKey root key
   * @param subKeyName key name
   * @param name value name
   * @throws java.io.UnsupportedEncodingException on error
   * @return String or null
   */
public static String getStringValue(REGISTRY_ROOT_KEY rootKey, String subKeyName, String name) throws UnsupportedEncodingException {
    //Advapi32 advapi32;
    //IntByReference pType, lpcbData;
    byte[] lpData = new byte[1];
    //int handle = 0;
    Advapi32 advapi32 = Advapi32.INSTANCE;
    IntByReference pType = new IntByReference();
    IntByReference lpcbData = new IntByReference();
    HKEY handle = openKey(rootKey, subKeyName, WinNT.KEY_READ);
    String ret = null;
    //if (handle != 0) {
    if (handle != null) {
        //if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WinError.ERROR_MORE_DATA) {
        if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WinError.ERROR_MORE_DATA) {
            lpData = new byte[lpcbData.getValue()];
            //if (advapi32.RegQueryValueEx(handle, name, null, pType, lpData, lpcbData) == WinError.ERROR_SUCCESS) {
            if (advapi32.RegQueryValueEx(handle, name, 0, pType, lpData, lpcbData) == WinError.ERROR_SUCCESS) {
                ret = convertBufferToString(lpData);
            }
        }
        advapi32.RegCloseKey(handle);
    }
    return ret;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Advapi32(com.sun.jna.platform.win32.Advapi32) HKEY(com.sun.jna.platform.win32.WinReg.HKEY)

Example 33 with BYTE

use of com.sun.jna.platform.win32.WinDef.BYTE in project processing by processing.

the class WindowsRegistry method getValues.

/**
   * Get all values under a key.
   *
   * @param rootKey root key
   * @param key jey name
   * @throws java.io.UnsupportedEncodingException on error
   * @return TreeMap with name and value pairs
   */
public static TreeMap<String, Object> getValues(REGISTRY_ROOT_KEY rootKey, String key) throws UnsupportedEncodingException {
    //Advapi32 advapi32;
    //int handle = 0, dwIndex, result = 0;
    //char[] lpValueName;
    //byte[] lpData;
    //IntByReference lpcchValueName, lpType, lpcbData;
    //String name;
    TreeMap<String, Object> values = new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);
    Advapi32 advapi32 = Advapi32.INSTANCE;
    HKEY handle = openKey(rootKey, key, WinNT.KEY_READ);
    char[] lpValueName = new char[16384];
    IntByReference lpcchValueName = new IntByReference(16384);
    IntByReference lpType = new IntByReference();
    byte[] lpData = new byte[1];
    IntByReference lpcbData = new IntByReference();
    //if(handle != 0) {
    if (handle != null) {
        int dwIndex = 0;
        int result = 0;
        String name;
        do {
            lpcbData.setValue(0);
            result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);
            if (result == WinError.ERROR_MORE_DATA) {
                lpData = new byte[lpcbData.getValue()];
                lpcchValueName = new IntByReference(16384);
                result = advapi32.RegEnumValue(handle, dwIndex, lpValueName, lpcchValueName, null, lpType, lpData, lpcbData);
                if (result == WinError.ERROR_SUCCESS) {
                    name = new String(lpValueName, 0, lpcchValueName.getValue());
                    switch(lpType.getValue()) {
                        case WinNT.REG_SZ:
                            values.put(name, convertBufferToString(lpData));
                            break;
                        case WinNT.REG_DWORD:
                            values.put(name, convertBufferToInt(lpData));
                            break;
                        default:
                            break;
                    }
                }
            }
            dwIndex++;
        } while (result == WinError.ERROR_SUCCESS);
        advapi32.RegCloseKey(handle);
    }
    return values;
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Advapi32(com.sun.jna.platform.win32.Advapi32) HKEY(com.sun.jna.platform.win32.WinReg.HKEY) TreeMap(java.util.TreeMap)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)12 Pointer (com.sun.jna.Pointer)8 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)8 Advapi32 (com.sun.jna.platform.win32.Advapi32)6 HKEY (com.sun.jna.platform.win32.WinReg.HKEY)6 PointerByReference (com.sun.jna.ptr.PointerByReference)5 Memory (com.sun.jna.Memory)4 GUID (com.sun.jna.platform.win32.Guid.GUID)4 BSTR (com.sun.jna.platform.win32.WTypes.BSTR)4 File (java.io.File)4 Date (java.util.Date)4 Test (org.junit.Test)4 DATE (com.sun.jna.platform.win32.OaIdl.DATE)3 VARIANT (com.sun.jna.platform.win32.Variant.VARIANT)3 FE_EXPORT_FUNC (com.sun.jna.platform.win32.WinBase.FE_EXPORT_FUNC)3 BYTE (com.sun.jna.platform.win32.WinDef.BYTE)3 CHAR (com.sun.jna.platform.win32.WinDef.CHAR)3 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)3 LONG (com.sun.jna.platform.win32.WinDef.LONG)3 SHORT (com.sun.jna.platform.win32.WinDef.SHORT)3