Search in sources :

Example 6 with HMODULE

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

the class User32WindowMessagesTest method createWindowAndLoop.

public void createWindowAndLoop(String windowClass) {
    // define new window class
    HMODULE hInst = Kernel32.INSTANCE.GetModuleHandle("");
    WNDCLASSEX wClass = new WNDCLASSEX();
    wClass.hInstance = hInst;
    wClass.lpfnWndProc = new WindowProc() {

        @Override
        public LRESULT callback(HWND hwnd, int uMsg, WPARAM wParam, LPARAM lParam) {
            // log(hwnd + " - received a message : " + uMsg);
            switch(uMsg) {
                case WinUser.WM_CREATE:
                    {
                        log(hwnd + " - onCreate: WM_CREATE");
                        return new LRESULT(0);
                    }
                case WinUser.WM_CLOSE:
                    log(hwnd + " WM_CLOSE");
                    User32.INSTANCE.DestroyWindow(hwnd);
                    return new LRESULT(0);
                case WinUser.WM_DESTROY:
                    {
                        log(hwnd + " - on Destroy.");
                        User32.INSTANCE.PostQuitMessage(0);
                        return new LRESULT(0);
                    }
                case WinUser.WM_USER:
                    {
                        log(hwnd + " - received a WM_USER message with code : '" + wParam + "' and value : '" + lParam + "'");
                        if (wParam.intValue() == MSG_SIMPLE_CODE) {
                            assertEqualsForCallbackExecution(MSG_SIMPLE_VAL, lParam.intValue());
                        }
                        if (wParam.intValue() == MSG_HOOKED_CODE) {
                            assertEqualsForCallbackExecution(MSG_HOOKED_VAL, lParam.intValue());
                        }
                        return new LRESULT(0);
                    }
                case WinUser.WM_COPYDATA:
                    {
                        COPYDATASTRUCT copyDataStruct = new COPYDATASTRUCT(new Pointer(lParam.longValue()));
                        ULONG_PTR uMsg1 = copyDataStruct.dwData;
                        Pointer lParam1 = copyDataStruct.lpData;
                        int wParam1 = copyDataStruct.cbData;
                        log(hwnd + " - received a WM_COPYDATA message with code : '" + uMsg1 + "' of size : '" + wParam1 + "'");
                        switch(uMsg1.intValue()) {
                            case DATA_STRUCT_CODE:
                                {
                                    MsgStruct msg = new MsgStruct(lParam1);
                                    // log(hwnd + " - received structured content : " + msg.toString(true));
                                    log(hwnd + " - message is of type MsgStruct with number = " + msg.number + " and message = '" + msg.message + "'");
                                    assertEqualsForCallbackExecution(MSG_STRUCT_NUMBER, msg.number);
                                    assertEqualsForCallbackExecution(MSG_STRUCT_VAL, msg.message);
                                }
                        }
                        return new LRESULT(0);
                    }
                default:
                    return User32.INSTANCE.DefWindowProc(hwnd, uMsg, wParam, lParam);
            }
        }
    };
    wClass.lpszClassName = windowClass;
    // register window class
    User32.INSTANCE.RegisterClassEx(wClass);
    getLastError();
    // create new window
    HWND hWnd = User32.INSTANCE.CreateWindowEx(User32.WS_EX_TOPMOST, windowClass, "My hidden helper window, used only to catch the windows events", 0, 0, 0, 0, 0, null, null, hInst, null);
    getLastError();
    log("window sucessfully created! window hwnd: " + hWnd.getPointer().toString());
    MSG msg = new MSG();
    while (User32.INSTANCE.GetMessage(msg, hWnd, 0, 0) > 0) {
        User32.INSTANCE.TranslateMessage(msg);
        User32.INSTANCE.DispatchMessage(msg);
    }
    User32.INSTANCE.UnregisterClass(windowClass, hInst);
    User32.INSTANCE.DestroyWindow(hWnd);
    log("program exit!");
}
Also used : MSG(com.sun.jna.platform.win32.WinUser.MSG) HWND(com.sun.jna.platform.win32.WinDef.HWND) Pointer(com.sun.jna.Pointer) HMODULE(com.sun.jna.platform.win32.WinDef.HMODULE) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) WindowProc(com.sun.jna.platform.win32.WinUser.WindowProc) LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) WNDCLASSEX(com.sun.jna.platform.win32.WinUser.WNDCLASSEX) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)

Example 7 with HMODULE

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

the class PsapiTest method testGetModuleInformation.

@Test
public void testGetModuleInformation() {
    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);
        MODULEINFO lpmodinfo = new MODULEINFO();
        if (!Psapi.INSTANCE.GetModuleInformation(me, list.get(0), lpmodinfo, lpmodinfo.size())) {
            throw new Win32Exception(Native.getLastError());
        }
        assertTrue("MODULEINFO.EntryPoint should not be null.", lpmodinfo.EntryPoint != null);
    } 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) MODULEINFO(com.sun.jna.platform.win32.Psapi.MODULEINFO) Test(org.junit.Test)

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