Search in sources :

Example 21 with Win32Exception

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

the class Kernel32Test method testModule32FirstW.

public void testModule32FirstW() {
    HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(Kernel32.INSTANCE.GetCurrentProcessId()));
    if (snapshot == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception we = null;
    Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
    try {
        if (!Kernel32.INSTANCE.Module32FirstW(snapshot, first)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // not sure if this will be run against java.exe or javaw.exe but this
        // check tests both
        assertTrue("The first module in the current process should be java.exe or javaw.exe", first.szModule().startsWith("java"));
        assertEquals("The process ID of the module ID should be our process ID", Kernel32.INSTANCE.GetCurrentProcessId(), first.th32ProcessID.intValue());
    } catch (Win32Exception e) {
        we = e;
        // re-throw so finally block is executed
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(snapshot);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 22 with Win32Exception

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

the class Kernel32Test method testModule32NextW.

public void testModule32NextW() {
    HANDLE snapshot = Kernel32.INSTANCE.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPMODULE, new DWORD(Kernel32.INSTANCE.GetCurrentProcessId()));
    if (snapshot == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception we = null;
    Tlhelp32.MODULEENTRY32W first = new Tlhelp32.MODULEENTRY32W();
    try {
        if (!Kernel32.INSTANCE.Module32NextW(snapshot, first)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // not sure if this will be run against java.exe or javaw.exe but this
        // check tests both
        assertTrue("The first module in the current process should be java.exe or javaw.exe", first.szModule().startsWith("java"));
        assertEquals("The process ID of the module ID should be our process ID", Kernel32.INSTANCE.GetCurrentProcessId(), first.th32ProcessID.intValue());
    } catch (Win32Exception e) {
        we = e;
        // re-throw so finally block is executed
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(snapshot);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : DWORD(com.sun.jna.platform.win32.WinDef.DWORD) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 23 with Win32Exception

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

the class Kernel32UtilTest method testFreeGlobalMemory.

public void testFreeGlobalMemory() {
    try {
        Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
        Kernel32Util.freeGlobalMemory(ptr);
        fail("Unexpected success to free bad global memory");
    } catch (Win32Exception e) {
        HRESULT hr = e.getHR();
        int code = W32Errors.HRESULT_CODE(hr.intValue());
        assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) Pointer(com.sun.jna.Pointer)

Example 24 with Win32Exception

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

the class Kernel32UtilTest method testFreeLocalMemory.

public void testFreeLocalMemory() {
    try {
        Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
        Kernel32Util.freeLocalMemory(ptr);
        fail("Unexpected success to free bad local memory");
    } catch (Win32Exception e) {
        HRESULT hr = e.getHR();
        int code = W32Errors.HRESULT_CODE(hr.intValue());
        assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) Pointer(com.sun.jna.Pointer)

Example 25 with Win32Exception

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

the class Crypt32Util method cryptProtectData.

/**
     * Protect a blob of data.
     * @param data
     *  Data to protect.
     * @param entropy
     *  Optional entropy.
     * @param flags
     *  Optional flags.
     * @param description
     *  Optional description.
     * @param prompt
     *  Prompt structure.
     * @return
     *  Protected bytes.
     */
public static byte[] cryptProtectData(byte[] data, byte[] entropy, int flags, String description, CRYPTPROTECT_PROMPTSTRUCT prompt) {
    DATA_BLOB pDataIn = new DATA_BLOB(data);
    DATA_BLOB pDataProtected = new DATA_BLOB();
    DATA_BLOB pEntropy = (entropy == null) ? null : new DATA_BLOB(entropy);
    try {
        if (!Crypt32.INSTANCE.CryptProtectData(pDataIn, description, pEntropy, null, prompt, flags, pDataProtected)) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        return pDataProtected.getData();
    } finally {
        if (pDataProtected.pbData != null) {
            Kernel32Util.freeLocalMemory(pDataProtected.pbData);
        }
    }
}
Also used : DATA_BLOB(com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)

Aggregations

IntByReference (com.sun.jna.ptr.IntByReference)35 HKEYByReference (com.sun.jna.platform.win32.WinReg.HKEYByReference)18 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)17 Memory (com.sun.jna.Memory)15 PointerByReference (com.sun.jna.ptr.PointerByReference)11 ArrayList (java.util.ArrayList)11 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Pointer (com.sun.jna.Pointer)6 File (java.io.File)6 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)5 Test (org.junit.Test)5 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)4 HRESULT (com.sun.jna.platform.win32.WinNT.HRESULT)4 PSID (com.sun.jna.platform.win32.WinNT.PSID)4 Win32Exception (com.sun.jna.platform.win32.Win32Exception)3 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)3 LOCALGROUP_INFO_1 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_INFO_1)2 LOCALGROUP_USERS_INFO_0 (com.sun.jna.platform.win32.LMAccess.LOCALGROUP_USERS_INFO_0)2 DATA_BLOB (com.sun.jna.platform.win32.WinCrypt.DATA_BLOB)2 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)2