use of com.sun.jna.Pointer in project jna by java-native-access.
the class WindowUtilsTest method testGetWindowIcon.
public void testGetWindowIcon() throws Exception {
final JFrame w = new JFrame();
try {
final BufferedImage expectedIcon = ImageIO.read(new FileInputStream(new File(getClass().getResource("/res/test_icon.png").getPath())));
w.setIconImage(expectedIcon);
w.setVisible(true);
Pointer p = Native.getComponentPointer(w);
assertNotNull("Couldn't obtain window HANDLE from JFrame", p);
HWND hwnd = new HWND(p);
final BufferedImage obtainedIcon = WindowUtils.getWindowIcon(hwnd);
assertTrue(obtainedIcon.getWidth() > 0);
assertTrue(obtainedIcon.getHeight() > 0);
int[] expectedColors = getPixelColor(expectedIcon, 10, 10);
assertPixelColor(obtainedIcon, 10, 10, expectedColors[0], expectedColors[1], expectedColors[2]);
expectedColors = getPixelColor(expectedIcon, expectedIcon.getWidth() - 10, 10);
assertPixelColor(obtainedIcon, obtainedIcon.getWidth() - 10, 10, expectedColors[0], expectedColors[1], expectedColors[2]);
expectedColors = getPixelColor(expectedIcon, expectedIcon.getWidth() - 10, expectedIcon.getHeight() - 10);
assertPixelColor(obtainedIcon, obtainedIcon.getWidth() - 10, obtainedIcon.getHeight() - 10, expectedColors[0], expectedColors[1], expectedColors[2]);
expectedColors = getPixelColor(expectedIcon, 10, expectedIcon.getHeight() - 10);
assertPixelColor(obtainedIcon, 10, obtainedIcon.getHeight() - 10, expectedColors[0], expectedColors[1], expectedColors[2]);
} finally {
w.dispose();
}
}
use of com.sun.jna.Pointer in project jna by java-native-access.
the class KeyHook method main.
public static void main(String[] args) {
final User32 lib = User32.INSTANCE;
HMODULE hMod = Kernel32.INSTANCE.GetModuleHandle(null);
keyboardHook = new LowLevelKeyboardProc() {
@Override
public LRESULT callback(int nCode, WPARAM wParam, KBDLLHOOKSTRUCT info) {
if (nCode >= 0) {
switch(wParam.intValue()) {
case WinUser.WM_KEYUP:
case WinUser.WM_KEYDOWN:
case WinUser.WM_SYSKEYUP:
case WinUser.WM_SYSKEYDOWN:
System.err.println("in callback, key=" + info.vkCode);
if (info.vkCode == 81) {
quit = true;
}
}
}
Pointer ptr = info.getPointer();
long peer = Pointer.nativeValue(ptr);
return lib.CallNextHookEx(hhk, nCode, wParam, new LPARAM(peer));
}
};
hhk = lib.SetWindowsHookEx(WinUser.WH_KEYBOARD_LL, keyboardHook, hMod, 0);
System.out.println("Keyboard hook installed, type anywhere, 'q' to quit");
new Thread() {
@Override
public void run() {
while (!quit) {
try {
Thread.sleep(10);
} catch (Exception e) {
}
}
System.err.println("unhook and exit");
lib.UnhookWindowsHookEx(hhk);
System.exit(0);
}
}.start();
// This bit never returns from GetMessage
int result;
MSG msg = new MSG();
while ((result = lib.GetMessage(msg, null, 0, 0)) != 0) {
if (result == -1) {
System.err.println("error in get message");
break;
} else {
System.err.println("got message");
lib.TranslateMessage(msg);
lib.DispatchMessage(msg);
}
}
lib.UnhookWindowsHookEx(hhk);
}
use of com.sun.jna.Pointer in project elasticsearch by elastic.
the class SystemCallFilter method macImpl.
/** try to install our custom rule profile into sandbox_init() to block execution */
private static void macImpl(Path tmpFile) throws IOException {
// first be defensive: we can give nice errors this way, at the very least.
boolean supported = Constants.MAC_OS_X;
if (supported == false) {
throw new IllegalStateException("bug: should not be trying to initialize seatbelt for an unsupported OS");
}
// we couldn't link methods, could be some really ancient OS X (< Leopard) or some bug
if (libc_mac == null) {
throw new UnsupportedOperationException("seatbelt unavailable: could not link methods. requires Leopard or above.");
}
// write rules to a temporary file, which will be passed to sandbox_init()
Path rules = Files.createTempFile(tmpFile, "es", "sb");
Files.write(rules, Collections.singleton(SANDBOX_RULES));
boolean success = false;
try {
PointerByReference errorRef = new PointerByReference();
int ret = libc_mac.sandbox_init(rules.toAbsolutePath().toString(), SANDBOX_NAMED, errorRef);
// if sandbox_init() fails, add the message from the OS (e.g. syntax error) and free the buffer
if (ret != 0) {
Pointer errorBuf = errorRef.getValue();
RuntimeException e = new UnsupportedOperationException("sandbox_init(): " + errorBuf.getString(0));
libc_mac.sandbox_free_error(errorBuf);
throw e;
}
logger.debug("OS X seatbelt initialization successful");
success = true;
} finally {
if (success) {
Files.delete(rules);
} else {
IOUtils.deleteFilesIgnoringExceptions(rules);
}
}
}
use of com.sun.jna.Pointer in project buck by facebook.
the class ProcessHelper method windowsProcessId.
@Nullable
private Long windowsProcessId(Object process) {
Class<?> clazz = process.getClass();
if (clazz.getName().equals("java.lang.Win32Process") || clazz.getName().equals("java.lang.ProcessImpl")) {
try {
Field f = clazz.getDeclaredField("handle");
f.setAccessible(true);
long peer = f.getLong(process);
Pointer pointer = Pointer.createConstant(peer);
WinNT.HANDLE handle = new WinNT.HANDLE(pointer);
return (long) Kernel32.INSTANCE.GetProcessId(handle);
} catch (Exception e) {
LOG.warn(e, "Cannot get process id!");
}
}
return null;
}
use of com.sun.jna.Pointer in project buck by facebook.
the class MacWifiSsidFinder method findCurrentSsid.
/**
* Finds the SSID of the default Wi-Fi interface using Mac OS X APIs.
*/
public static Optional<String> findCurrentSsid() {
LOG.debug("Getting current SSID..");
// Grab a handle to the Objective-C runtime.
Client objcClient = Client.getInstance();
// Try the OS X 10.10 and later supported API, then fall
// back to the OS X 10.6 API.
Pointer wifiClientClass = RuntimeUtils.cls("CWWiFiClient");
Optional<Proxy> defaultInterface;
if (wifiClientClass != null) {
LOG.verbose("Getting default interface using +[CWWiFiClient sharedWiFiClient]");
defaultInterface = getDefaultWifiInterface(objcClient, wifiClientClass);
} else {
LOG.verbose("Getting default interface using +[CWInterface defaultInterface]");
// CWInterface *defaultInterface = [CWInterface interface];
defaultInterface = Optional.ofNullable(objcClient.sendProxy("CWInterface", "interface"));
}
return getSsidFromInterface(defaultInterface);
}
Aggregations