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);
}
}
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));
}
}
}
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());
}
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);
}
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();
}
}
Aggregations