Search in sources :

Example 1 with PokeHandler

use of com.sun.jna.platform.win32.DdemlUtil.PokeHandler in project jna by java-native-access.

the class DdemlUtilTest method testPoke.

@Test
public void testPoke() throws InterruptedException {
    final String serviceName = "TestService";
    final String topicName = "TestTopic";
    final String itemName = "TestItem";
    final String testValue = "Execute�������";
    final CountDownLatch pokeReceived = new CountDownLatch(1);
    StandaloneDdeClient client = null;
    StandaloneDdeClient server = null;
    try {
        client = new StandaloneDdeClient() {

            {
                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 PokeHandler pokeHandler = new PokeHandler() {

                @Override
                public int onPoke(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item, Ddeml.HDDEDATA hdata) {
                    Pointer[] pointer = new Pointer[] { accessData(hdata, null) };
                    try {
                        String commandString = pointer[0].getWideString(0);
                        if (testValue.equals(commandString) && queryString(topic).equals(topicName) && queryString(item).equals(itemName)) {
                            pokeReceived.countDown();
                            return Ddeml.DDE_FACK;
                        }
                    } finally {
                        synchronized (pointer) {
                        }
                    }
                    return Ddeml.DDE_FNOTPROCESSED;
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerPokeHandler(pokeHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        Memory mem = new Memory((testValue.length() + 1) * 2);
        mem.setWideString(0, testValue);
        con.poke(mem, (int) mem.size(), itemName, WinUser.CF_UNICODETEXT, 5 * 1000, null, null);
        assertTrue(pokeReceived.await(5, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : Memory(com.sun.jna.Memory) HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) CountDownLatch(java.util.concurrent.CountDownLatch) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) PokeHandler(com.sun.jna.platform.win32.DdemlUtil.PokeHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) Test(org.junit.Test)

Aggregations

Memory (com.sun.jna.Memory)1 Pointer (com.sun.jna.Pointer)1 HCONV (com.sun.jna.platform.win32.Ddeml.HCONV)1 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)1 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)1 ConnectHandler (com.sun.jna.platform.win32.DdemlUtil.ConnectHandler)1 IDdeConnection (com.sun.jna.platform.win32.DdemlUtil.IDdeConnection)1 PokeHandler (com.sun.jna.platform.win32.DdemlUtil.PokeHandler)1 StandaloneDdeClient (com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1