Search in sources :

Example 1 with HMODULE

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

the class PsapiTest method testEnumProcessModules.

@Test
public void testEnumProcessModules() {
    HANDLE me = null;
    Win32Exception we = null;
    try {
        me = Kernel32.INSTANCE.OpenProcess(WinNT.PROCESS_ALL_ACCESS, false, Kernel32.INSTANCE.GetCurrentProcessId());
        assertTrue("Handle to my process should not be null", me != null);
        List<HMODULE> list = new LinkedList<HMODULE>();
        HMODULE[] lphModule = new HMODULE[100 * 4];
        IntByReference lpcbNeeded = new IntByReference();
        if (!Psapi.INSTANCE.EnumProcessModules(me, lphModule, lphModule.length, lpcbNeeded)) {
            throw new Win32Exception(Native.getLastError());
        }
        for (int i = 0; i < lpcbNeeded.getValue() / 4; i++) {
            list.add(lphModule[i]);
        }
        assertTrue("List should have at least 1 item in it.", list.size() > 0);
    } catch (Win32Exception e) {
        we = e;
        // re-throw to invoke finally block
        throw we;
    } finally {
        try {
            Kernel32Util.closeHandle(me);
        } catch (Win32Exception e) {
            if (we == null) {
                we = e;
            } else {
                we.addSuppressed(e);
            }
        }
        if (we != null) {
            throw we;
        }
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Example 2 with HMODULE

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

the class Kernel32Util method getResource.

/**
     * Gets the specified resource out of the specified executable file
     *
     * @param path
     *            The path to the executable file
     * @param type
     *            The type of the resource (either a type name or type ID is
     *            allowed)
     * @param name
     *            The name or ID of the resource
     * @return The resource bytes, or null if no such resource exists.
     * @throws IllegalStateException if the call to LockResource fails
     */
public static byte[] getResource(String path, String type, String name) {
    HMODULE target = Kernel32.INSTANCE.LoadLibraryEx(path, null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
    if (target == null) {
        throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
    }
    Win32Exception err = null;
    Pointer start = null;
    int length = 0;
    byte[] results = null;
    try {
        Pointer t = null;
        try {
            t = new Pointer(Long.parseLong(type));
        } catch (NumberFormatException e) {
            t = new Memory(Native.WCHAR_SIZE * (type.length() + 1));
            t.setWideString(0, type);
        }
        Pointer n = null;
        try {
            n = new Pointer(Long.parseLong(name));
        } catch (NumberFormatException e) {
            n = new Memory(Native.WCHAR_SIZE * (name.length() + 1));
            n.setWideString(0, name);
        }
        HRSRC hrsrc = Kernel32.INSTANCE.FindResource(target, n, t);
        if (hrsrc == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // according to MSDN, on 32 bit Windows or newer, calling FreeResource() is not necessary - and in fact does nothing but return false.
        HANDLE loaded = Kernel32.INSTANCE.LoadResource(target, hrsrc);
        if (loaded == null) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        length = Kernel32.INSTANCE.SizeofResource(target, hrsrc);
        if (length == 0) {
            throw new Win32Exception(Kernel32.INSTANCE.GetLastError());
        }
        // MSDN: It is not necessary to unlock resources because the system automatically deletes them when the process that created them terminates.
        // MSDN does not say that LockResource sets GetLastError
        start = Kernel32.INSTANCE.LockResource(loaded);
        if (start == null) {
            throw new IllegalStateException("LockResource returned null.");
        }
        // have to capture it into a byte array before you free the library, otherwise bad things happen.
        results = start.getByteArray(0, length);
    } catch (Win32Exception we) {
        err = we;
    } finally {
        // from what I can tell on MSDN, the only thing that needs cleanup on this is the HMODULE from LoadLibrary
        if (target != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(target)) {
                Win32Exception we = new Win32Exception(Kernel32.INSTANCE.GetLastError());
                if (err != null) {
                    we.addSuppressed(err);
                }
                throw we;
            }
        }
    }
    if (err != null) {
        throw err;
    }
    return results;
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 3 with HMODULE

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

the class Kernel32Test method testEnumResourceNames.

public void testEnumResourceNames() {
    // "14" is the type name of the My Computer icon in explorer.exe
    Pointer pointer = new Memory(Native.WCHAR_SIZE * 3);
    pointer.setWideString(0, "14");
    WinBase.EnumResNameProc ernp = new WinBase.EnumResNameProc() {

        @Override
        public boolean invoke(HMODULE module, Pointer type, Pointer name, Pointer lParam) {
            return true;
        }
    };
    // null HMODULE means use this process / its EXE
    // there are no type "14" resources in it.
    boolean result = Kernel32.INSTANCE.EnumResourceNames(null, pointer, ernp, null);
    assertFalse("EnumResourceNames should have failed.", result);
    assertEquals("GetLastError should be set to 1813", WinError.ERROR_RESOURCE_TYPE_NOT_FOUND, Kernel32.INSTANCE.GetLastError());
}
Also used : Memory(com.sun.jna.Memory) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE)

Example 4 with HMODULE

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

the class Kernel32Test method testLoadLibraryEx.

public void testLoadLibraryEx() {
    String winDir = Kernel32Util.getEnvironmentVariable("WINDIR");
    assertNotNull("No WINDIR value returned", winDir);
    assertTrue("Specified WINDIR does not exist: " + winDir, new File(winDir).exists());
    HMODULE hModule = null;
    try {
        hModule = Kernel32.INSTANCE.LoadLibraryEx(new File(winDir, "explorer.exe").getAbsolutePath(), null, Kernel32.LOAD_LIBRARY_AS_DATAFILE);
        if (hModule == null) {
            throw new Win32Exception(Native.getLastError());
        }
        assertNotNull("hModule should not be null.", hModule);
    } finally {
        if (hModule != null) {
            if (!Kernel32.INSTANCE.FreeLibrary(hModule)) {
                throw new Win32Exception(Native.getLastError());
            }
        }
    }
}
Also used : HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) File(java.io.File)

Example 5 with HMODULE

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

Aggregations

HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)6 Pointer (com.sun.jna.Pointer)4 HANDLE (com.sun.jna.platform.win32.WinNT.HANDLE)3 Memory (com.sun.jna.Memory)2 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)2 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)2 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)2 MSG (com.sun.jna.platform.win32.WinUser.MSG)2 IntByReference (com.sun.jna.ptr.IntByReference)2 LinkedList (java.util.LinkedList)2 Test (org.junit.Test)2 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)1 MODULEINFO (com.sun.jna.platform.win32.Psapi.MODULEINFO)1 User32 (com.sun.jna.platform.win32.User32)1 HWND (com.sun.jna.platform.win32.WinDef.HWND)1 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)1 KBDLLHOOKSTRUCT (com.sun.jna.platform.win32.WinUser.KBDLLHOOKSTRUCT)1 LowLevelKeyboardProc (com.sun.jna.platform.win32.WinUser.LowLevelKeyboardProc)1 WNDCLASSEX (com.sun.jna.platform.win32.WinUser.WNDCLASSEX)1 WindowProc (com.sun.jna.platform.win32.WinUser.WindowProc)1