Search in sources :

Example 21 with Pointer

use of com.sun.jna.Pointer 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)

Example 22 with Pointer

use of com.sun.jna.Pointer 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 23 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Kernel32ConsoleTest method testGetConsoleMode.

@Test
@Ignore("For some reason we get hr=6 - ERROR_INVALID_HANDLE - even though the documentation stipulates that GetStdHandle can be used")
public void testGetConsoleMode() {
    for (int nHandle : new int[] { Wincon.STD_INPUT_HANDLE, Wincon.STD_OUTPUT_HANDLE, Wincon.STD_ERROR_HANDLE }) {
        HANDLE hndl = INSTANCE.GetStdHandle(nHandle);
        Pointer ptr = hndl.getPointer();
        if (ptr == Pointer.NULL) {
            // can happen for interactive desktop application
            continue;
        }
        IntByReference lpMode = new IntByReference(0);
        assertCallSucceeded("GetConsoleMode(" + nHandle + ")", INSTANCE.GetConsoleMode(hndl, lpMode));
        int dwMode = lpMode.getValue();
        // don't really care what the mode is just want to make sure API can be called
        assertCallSucceeded("SetConsoleMode(" + nHandle + "," + dwMode + ")", INSTANCE.SetConsoleMode(hndl, dwMode));
    }
}
Also used : IntByReference(com.sun.jna.ptr.IntByReference) Pointer(com.sun.jna.Pointer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Kernel32Test method testWriteProcessMemory.

public void testWriteProcessMemory() {
    Kernel32 kernel = Kernel32.INSTANCE;
    boolean successWrite = kernel.WriteProcessMemory(null, Pointer.NULL, Pointer.NULL, 1, null);
    assertFalse(successWrite);
    assertEquals(kernel.GetLastError(), WinError.ERROR_INVALID_HANDLE);
    ByteBuffer bufDest = ByteBuffer.allocateDirect(4);
    bufDest.put(new byte[] { 0, 1, 2, 3 });
    ByteBuffer bufSrc = ByteBuffer.allocateDirect(4);
    bufSrc.put(new byte[] { 5, 10, 15, 20 });
    Pointer ptrSrc = Native.getDirectBufferPointer(bufSrc);
    Pointer ptrDest = Native.getDirectBufferPointer(bufDest);
    HANDLE selfHandle = kernel.GetCurrentProcess();
    //Write only the first three
    kernel.WriteProcessMemory(selfHandle, ptrDest, ptrSrc, 3, null);
    assertEquals(bufDest.get(0), 5);
    assertEquals(bufDest.get(1), 10);
    assertEquals(bufDest.get(2), 15);
    assertEquals(bufDest.get(3), 3);
}
Also used : Pointer(com.sun.jna.Pointer) ByteBuffer(java.nio.ByteBuffer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE)

Example 25 with Pointer

use of com.sun.jna.Pointer in project jna by java-native-access.

the class Kernel32UtilTest method testFreeGlobalMemory.

public void testFreeGlobalMemory() {
    try {
        Pointer ptr = new Pointer(0xFFFFFFFFFFFFFFFFL);
        Kernel32Util.freeGlobalMemory(ptr);
        fail("Unexpected success to free bad global memory");
    } catch (Win32Exception e) {
        HRESULT hr = e.getHR();
        int code = W32Errors.HRESULT_CODE(hr.intValue());
        assertEquals("Mismatched failure reason code", WinError.ERROR_INVALID_HANDLE, code);
    }
}
Also used : HRESULT(com.sun.jna.platform.win32.WinNT.HRESULT) Pointer(com.sun.jna.Pointer)

Aggregations

Pointer (com.sun.jna.Pointer)152 PointerByReference (com.sun.jna.ptr.PointerByReference)31 Test (org.junit.Test)31 Memory (com.sun.jna.Memory)23 File (java.io.File)21 ByteBuffer (java.nio.ByteBuffer)18 NativeLong (com.sun.jna.NativeLong)17 IntByReference (com.sun.jna.ptr.IntByReference)17 BufferedImage (java.awt.image.BufferedImage)15 FileInputStream (java.io.FileInputStream)11 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)8 DirectBuffer (sun.nio.ch.DirectBuffer)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 IOException (java.io.IOException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7