Search in sources :

Example 66 with Pointer

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();
    }
}
Also used : JFrame(javax.swing.JFrame) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Example 67 with Pointer

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);
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) KBDLLHOOKSTRUCT(com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) LowLevelKeyboardProc(com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) User32(com.sun.jna.platform.win32.User32)

Example 68 with Pointer

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);
        }
    }
}
Also used : Path(java.nio.file.Path) PointerByReference(com.sun.jna.ptr.PointerByReference) Pointer(com.sun.jna.Pointer)

Example 69 with Pointer

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;
}
Also used : Field(java.lang.reflect.Field) WinNT(com.sun.jna.platform.win32.WinNT) Pointer(com.sun.jna.Pointer) Nullable(javax.annotation.Nullable)

Example 70 with Pointer

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);
}
Also used : Proxy(ca.weblite.objc.Proxy) Pointer(com.sun.jna.Pointer) Client(ca.weblite.objc.Client)

Aggregations

Pointer (com.sun.jna.Pointer)93 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Memory (com.sun.jna.Memory)15 IntByReference (com.sun.jna.ptr.IntByReference)15 Test (org.junit.Test)12 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)7 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)7 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)7 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)7 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)7 File (java.io.File)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HWND (com.sun.jna.platform.win32.WinDef.HWND)6 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)6 IOException (java.io.IOException)6 BufferedImage (java.awt.image.BufferedImage)5 Function (com.sun.jna.Function)4 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)4