Search in sources :

Example 61 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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 62 with Kernel32

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

the class Kernel32Test method testGetCurrentProcess.

public void testGetCurrentProcess() {
    HANDLE h = Kernel32.INSTANCE.GetCurrentProcess();
    assertNotNull("No current process handle", h);
    assertFalse("Null current process handle", h.equals(0));
    // Calling the CloseHandle function with a pseudo handle has no effect
    Kernel32Util.closeHandle(h);
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 63 with Kernel32

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

the class Kernel32Test method testCreateRemoteThread.

public final void testCreateRemoteThread() throws IOException {
    HANDLE hThrd = Kernel32.INSTANCE.CreateRemoteThread(null, null, 0, null, null, null, null);
    assertNull(hThrd);
    assertEquals(Kernel32.INSTANCE.GetLastError(), WinError.ERROR_INVALID_HANDLE);
}
Also used : HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 64 with Kernel32

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

the class Kernel32Test method testCreatePipe.

public void testCreatePipe() {
    HANDLEByReference hReadPipe = new HANDLEByReference();
    HANDLEByReference hWritePipe = new HANDLEByReference();
    try {
        assertTrue(Kernel32.INSTANCE.CreatePipe(hReadPipe, hWritePipe, null, 0));
    } finally {
        Kernel32Util.closeHandleRefs(hReadPipe, hWritePipe);
    }
}
Also used : HANDLEByReference(com.sun.jna.platform.win32.WinNT.HANDLEByReference)

Example 65 with Kernel32

use of com.sun.jna.platform.win32.Kernel32 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

HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)74 IntByReference (com.sun.jna.ptr.IntByReference)47 File (java.io.File)33 DWORD (com.sun.jna.platform.win32.WinDef.DWORD)30 SC_HANDLE (com.sun.jna.platform.win32.Winsvc.SC_HANDLE)27 HANDLEByReference (com.sun.jna.platform.win32.WinNT.HANDLEByReference)26 Memory (com.sun.jna.Memory)25 PointerByReference (com.sun.jna.ptr.PointerByReference)14 Pointer (com.sun.jna.Pointer)12 EVT_HANDLE (com.sun.jna.platform.win32.Winevt.EVT_HANDLE)7 Test (org.junit.Test)7 SYSTEMTIME (com.sun.jna.platform.win32.WinBase.SYSTEMTIME)6 ArrayList (java.util.ArrayList)6 FILETIME (com.sun.jna.platform.win32.WinBase.FILETIME)5 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)5 PSID (com.sun.jna.platform.win32.WinNT.PSID)5 TOKEN_PRIVILEGES (com.sun.jna.platform.win32.WinNT.TOKEN_PRIVILEGES)5 WString (com.sun.jna.WString)4 DWORDByReference (com.sun.jna.platform.win32.WinDef.DWORDByReference)4 ShortByReference (com.sun.jna.ptr.ShortByReference)4