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;
}
}
}
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);
}
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);
}
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);
}
}
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);
}
}
}
Aggregations