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