Search in sources :

Example 1 with ULONG_PTR

use of com.sun.jna.platform.win32.BaseTSD.ULONG_PTR in project jna by java-native-access.

the class User32WindowMessagesTest method createStructuredMessage.

private COPYDATASTRUCT createStructuredMessage() {
    MsgStruct myData = new MsgStruct();
    myData.number = MSG_STRUCT_NUMBER;
    myData.message = MSG_STRUCT_VAL;
    // writes to native memory the data structure otherwise nothing is sent...
    myData.write();
    // log("Prepared structured content to send : " + myData.toString(true));
    COPYDATASTRUCT copyDataStruct = new COPYDATASTRUCT();
    copyDataStruct.dwData = new ULONG_PTR(DATA_STRUCT_CODE);
    copyDataStruct.cbData = myData.size();
    copyDataStruct.lpData = myData.getPointer();
    // writes to native memory the data structure otherwise nothing is sent...
    copyDataStruct.write();
    return copyDataStruct;
}
Also used : COPYDATASTRUCT(com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)

Example 2 with ULONG_PTR

use of com.sun.jna.platform.win32.BaseTSD.ULONG_PTR in project jna by java-native-access.

the class DdemlUtilTest method testAbandonTransaction.

@Test
public void testAbandonTransaction() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String testExecute = "Execute�������";
    final CountDownLatch allTransactionsInvoked = new CountDownLatch(1);
    final CountDownLatch executesProcessed = new CountDownLatch(3);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            private final XactCompleteHandler xactCompleteHandler = new XactCompleteHandler() {

                public void onXactComplete(int transactionType, int dataFormat, HCONV hConv, HSZ topic, HSZ item, HDDEDATA hdata, ULONG_PTR transactionIdentifier, ULONG_PTR statusFlag) {
                    executesProcessed.countDown();
                }
            };

            {
                registerXactCompleteHandler(xactCompleteHandler);
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final ExecuteHandler executeHandler = new ExecuteHandler() {

                public int onExecute(int transactionType, HCONV hconv, HSZ topic, Ddeml.HDDEDATA commandStringData) {
                    try {
                        if (!allTransactionsInvoked.await(5, TimeUnit.SECONDS)) {
                            return Ddeml.DDE_FNOTPROCESSED;
                        }
                        Pointer[] pointer = new Pointer[] { accessData(commandStringData, null) };
                        try {
                            String commandString = pointer[0].getWideString(0);
                            if (testExecute.equals(commandString) && queryString(topic).equals(topicName)) {
                                return Ddeml.DDE_FACK;
                            }
                        } finally {
                            synchronized (pointer) {
                                unaccessData(commandStringData);
                            }
                        }
                        return Ddeml.DDE_FNOTPROCESSED;
                    } catch (InterruptedException ex) {
                        Logger.getLogger(DdemlUtilTest.class.getName()).log(Level.SEVERE, null, ex);
                        return Ddeml.DDE_FNOTPROCESSED;
                    }
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerExecuteHandler(executeHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        WinDef.DWORDByReference result = new WinDef.DWORDByReference();
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        int transactionId2 = result.getValue().intValue();
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
        con.abandonTransaction(transactionId2);
        allTransactionsInvoked.countDown();
        assertFalse(executesProcessed.await(2, TimeUnit.SECONDS));
        assertThat(executesProcessed.getCount(), is(1L));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) ExecuteHandler(com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) XactCompleteHandler(com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) Test(org.junit.Test)

Example 3 with ULONG_PTR

use of com.sun.jna.platform.win32.BaseTSD.ULONG_PTR 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 4 with ULONG_PTR

use of com.sun.jna.platform.win32.BaseTSD.ULONG_PTR in project jna by java-native-access.

the class User32Test method testGetClassLongPtr.

@Test
public void testGetClassLongPtr() {
    if (System.getProperty("os.arch", "unknown").equalsIgnoreCase("amd64")) {
        DesktopWindow explorerProc = getWindowByProcessPath("explorer.exe");
        assertNotNull("Could not find explorer.exe process", explorerProc);
        ULONG_PTR result = User32.INSTANCE.GetClassLongPtr(explorerProc.getHWND(), WinUser.GCLP_HMODULE);
        assertNotEquals(0, result.intValue());
    } else {
        System.err.println("GetClassLongPtr only supported on x64");
    }
}
Also used : ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) DesktopWindow(com.sun.jna.platform.DesktopWindow) Test(org.junit.Test)

Example 5 with ULONG_PTR

use of com.sun.jna.platform.win32.BaseTSD.ULONG_PTR in project jna by java-native-access.

the class DdemlUtilTest method testQueryConvInfoSetUserHandle.

@Test
public void testQueryConvInfoSetUserHandle() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String testExecute = "Execute�������";
    final CountDownLatch executesProcessed = new CountDownLatch(1);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            private final XactCompleteHandler xactCompleteHandler = new XactCompleteHandler() {

                public void onXactComplete(int transactionType, int dataFormat, HCONV hConv, HSZ topic, HSZ item, HDDEDATA hdata, ULONG_PTR transactionIdentifier, ULONG_PTR statusFlag) {
                    CONVINFO convInfo = wrap(hConv).queryConvInfo(transactionIdentifier.intValue());
                    if (convInfo.hUser.intValue() == 42) {
                        executesProcessed.countDown();
                    }
                }
            };

            {
                registerXactCompleteHandler(xactCompleteHandler);
                this.initialize(Ddeml.APPCMD_CLIENTONLY | Ddeml.CBF_SKIP_REGISTRATIONS | Ddeml.CBF_SKIP_UNREGISTRATIONS);
            }
        };
        server = new StandaloneDdeClient() {

            private final ConnectHandler connectHandler = new ConnectHandler() {

                public boolean onConnect(int transactionType, HSZ topic, HSZ service, Ddeml.CONVCONTEXT convcontext, boolean sameInstance) {
                    return topicName.equals(queryString(topic));
                }
            };

            private final ExecuteHandler executeHandler = new ExecuteHandler() {

                public int onExecute(int transactionType, HCONV hconv, HSZ topic, Ddeml.HDDEDATA commandStringData) {
                    Pointer[] pointer = new Pointer[] { accessData(commandStringData, null) };
                    try {
                        String commandString = pointer[0].getWideString(0);
                        if (testExecute.equals(commandString) && queryString(topic).equals(topicName)) {
                            return Ddeml.DDE_FACK;
                        }
                    } finally {
                        synchronized (pointer) {
                            unaccessData(commandStringData);
                        }
                    }
                    return Ddeml.DDE_FNOTPROCESSED;
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerExecuteHandler(executeHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, null, new BaseTSD.DWORD_PTR(42L));
        assertTrue(executesProcessed.await(3, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) ULONG_PTR(com.sun.jna.platform.win32.BaseTSD.ULONG_PTR) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) CONVINFO(com.sun.jna.platform.win32.Ddeml.CONVINFO) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) ExecuteHandler(com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) XactCompleteHandler(com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) Test(org.junit.Test)

Aggregations

ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)6 Pointer (com.sun.jna.Pointer)4 Test (org.junit.Test)4 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)3 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)3 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)3 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)3 ExecuteHandler (com.sun.jna.platform.win32.DdemlUtil.ExecuteHandler)3 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)3 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)3 XactCompleteHandler (com.sun.jna.platform.win32.DdemlUtil.XactCompleteHandler)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 COPYDATASTRUCT (com.sun.jna.platform.win32.WinUser.COPYDATASTRUCT)2 DesktopWindow (com.sun.jna.platform.DesktopWindow)1 CONVINFO (com.sun.jna.platform.win32.Ddeml.CONVINFO)1 HMODULE (com.sun.jna.platform.win32.WinDef.HMODULE)1 HWND (com.sun.jna.platform.win32.WinDef.HWND)1 LPARAM (com.sun.jna.platform.win32.WinDef.LPARAM)1 LRESULT (com.sun.jna.platform.win32.WinDef.LRESULT)1 WPARAM (com.sun.jna.platform.win32.WinDef.WPARAM)1