Search in sources :

Example 61 with Pointer

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

the class DdemlUtilTest method testRequest.

@Test
public void testRequest() 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 RequestHandler requestHandler = new RequestHandler() {

                public HDDEDATA onRequest(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item) {
                    if (dataFormat == WinUser.CF_UNICODETEXT && queryString(topic).equals(topicName) && queryString(item).equals(itemName)) {
                        Memory mem = new Memory((testValue.length() + 1) * 2);
                        mem.setWideString(0, testValue);
                        HDDEDATA result = createDataHandle(mem, (int) mem.size(), 0, item, dataFormat, 0);
                        pokeReceived.countDown();
                        return result;
                    } else {
                        return null;
                    }
                }
            };

            {
                registerConnectHandler(connectHandler);
                registerRequestHandler(requestHandler);
                this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
            }
        };
        server.nameService(serviceName, Ddeml.DNS_REGISTER);
        IDdeConnection con = client.connect(serviceName, topicName, null);
        HDDEDATA data = con.request(itemName, WinUser.CF_UNICODETEXT, 5 * 1000, null, null);
        try {
            try {
                Pointer pointer = server.accessData(data, null);
                assertThat(pointer.getWideString(0), is(testValue));
            } finally {
                server.unaccessData(data);
            }
        } finally {
            server.freeDataHandle(data);
        }
        assertTrue(pokeReceived.await(5, TimeUnit.SECONDS));
    } finally {
        closeQuitely(client);
        closeQuitely(server);
    }
}
Also used : ConnectHandler(com.sun.jna.platform.win32.DdemlUtil.ConnectHandler) RequestHandler(com.sun.jna.platform.win32.DdemlUtil.RequestHandler) HDDEDATA(com.sun.jna.platform.win32.Ddeml.HDDEDATA) StandaloneDdeClient(com.sun.jna.platform.win32.DdemlUtil.StandaloneDdeClient) Memory(com.sun.jna.Memory) HCONV(com.sun.jna.platform.win32.Ddeml.HCONV) Pointer(com.sun.jna.Pointer) IDdeConnection(com.sun.jna.platform.win32.DdemlUtil.IDdeConnection) CountDownLatch(java.util.concurrent.CountDownLatch) HSZ(com.sun.jna.platform.win32.Ddeml.HSZ) Test(org.junit.Test)

Example 62 with Pointer

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

the class Kernel32ConsoleTest method testGetStdHandle.

@Test
public void testGetStdHandle() {
    for (int nHandle : new int[] { Wincon.STD_INPUT_HANDLE, Wincon.STD_OUTPUT_HANDLE, Wincon.STD_ERROR_HANDLE }) {
        HANDLE hndl = INSTANCE.GetStdHandle(nHandle);
        assertNotEquals("Bad handle value for std handle=" + nHandle, WinBase.INVALID_HANDLE_VALUE, hndl);
        // don't really care what the handle value is - just ensure that API can be called
        /*
			 * According to the API documentation:
			 * 
			 * 		If an application does not have associated standard handles,
			 * 		such as a service running on an interactive desktop, and has
			 *  	not redirected them, the return value is NULL.
			 */
        Pointer ptr = hndl.getPointer();
        if (ptr == Pointer.NULL) {
            continue;
        } else {
            assertCallSucceeded("SetStdHandle(" + nHandle + ")", INSTANCE.SetStdHandle(nHandle, hndl));
        }
    }
}
Also used : Pointer(com.sun.jna.Pointer) HANDLE(com.sun.jna.platform.win32.WinNT.HANDLE) Test(org.junit.Test)

Example 63 with Pointer

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

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

the class Kernel32Test method testReadProcessMemory.

public void testReadProcessMemory() {
    Kernel32 kernel = Kernel32.INSTANCE;
    boolean successRead = kernel.ReadProcessMemory(null, Pointer.NULL, Pointer.NULL, 1, null);
    assertFalse(successRead);
    assertEquals(kernel.GetLastError(), WinError.ERROR_INVALID_HANDLE);
    ByteBuffer bufSrc = ByteBuffer.allocateDirect(4);
    bufSrc.put(new byte[] { 5, 10, 15, 20 });
    ByteBuffer bufDest = ByteBuffer.allocateDirect(4);
    bufDest.put(new byte[] { 0, 1, 2, 3 });
    Pointer ptrSrc = Native.getDirectBufferPointer(bufSrc);
    Pointer ptrDest = Native.getDirectBufferPointer(bufDest);
    HANDLE selfHandle = kernel.GetCurrentProcess();
    //Read only the first three
    kernel.ReadProcessMemory(selfHandle, ptrSrc, ptrDest, 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 65 with Pointer

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

the class WindowUtilsTest method testGetIconSize.

public void testGetIconSize() throws Exception {
    final JFrame w = new JFrame();
    try {
        final BufferedImage expectedIcon = ImageIO.read(new FileInputStream(new File(getClass().getResource("/res/test_icon.png").getPath())));
        w.setIconImage(expectedIcon);
        w.setVisible(true);
        Pointer p = Native.getComponentPointer(w);
        assertNotNull("Could not obtain native HANDLE for JFrame", p);
        HWND hwnd = new HWND(p);
        final DWORDByReference hIconNumber = new DWORDByReference();
        LRESULT result = User32.INSTANCE.SendMessageTimeout(hwnd, WinUser.WM_GETICON, new WPARAM(WinUser.ICON_BIG), new LPARAM(0), WinUser.SMTO_ABORTIFHUNG, 500, hIconNumber);
        assertNotEquals(0, result.intValue());
        final HICON hIcon = new HICON(new Pointer(hIconNumber.getValue().longValue()));
        assertTrue(WindowUtils.getIconSize(hIcon).width >= 32);
        assertTrue(WindowUtils.getIconSize(hIcon).height >= 32);
        assertEquals(WindowUtils.getIconSize(hIcon).width, WindowUtils.getIconSize(hIcon).height);
    } finally {
        w.dispose();
    }
}
Also used : LRESULT(com.sun.jna.platform.win32.WinDef.LRESULT) JFrame(javax.swing.JFrame) DWORDByReference(com.sun.jna.platform.win32.WinDef.DWORDByReference) HWND(com.sun.jna.platform.win32.WinDef.HWND) LPARAM(com.sun.jna.platform.win32.WinDef.LPARAM) HICON(com.sun.jna.platform.win32.WinDef.HICON) Pointer(com.sun.jna.Pointer) File(java.io.File) WPARAM(com.sun.jna.platform.win32.WinDef.WPARAM) BufferedImage(java.awt.image.BufferedImage) FileInputStream(java.io.FileInputStream)

Aggregations

Pointer (com.sun.jna.Pointer)93 PointerByReference (com.sun.jna.ptr.PointerByReference)20 Memory (com.sun.jna.Memory)15 IntByReference (com.sun.jna.ptr.IntByReference)15 Test (org.junit.Test)12 HDDEDATA (com.sun.jna.platform.win32.Ddeml.HDDEDATA)8 HSZ (com.sun.jna.platform.win32.Ddeml.HSZ)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 File (java.io.File)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 HWND (com.sun.jna.platform.win32.WinDef.HWND)6 ULONG (com.sun.jna.platform.win32.WinDef.ULONG)6 IOException (java.io.IOException)6 BufferedImage (java.awt.image.BufferedImage)5 Function (com.sun.jna.Function)4 ULONG_PTR (com.sun.jna.platform.win32.BaseTSD.ULONG_PTR)4