use of com.sun.jna.platform.win32.Ddeml.HDDEDATA in project jna by java-native-access.
the class DdemlTest method testMemoryHandling.
@Test
public void testMemoryHandling() {
DdeCallback callback = new Ddeml.DdeCallback() {
public WinDef.PVOID ddeCallback(int wType, int wFmt, Ddeml.HCONV hConv, Ddeml.HSZ hsz1, Ddeml.HSZ hsz2, Ddeml.HDDEDATA hData, BaseTSD.ULONG_PTR lData1, BaseTSD.ULONG_PTR lData2) {
return new PVOID();
}
};
DWORDByReference pidInst = new DWORDByReference();
int initResult = Ddeml.INSTANCE.DdeInitialize(pidInst, callback, Ddeml.APPCMD_CLIENTONLY, 0);
assertEquals(Ddeml.DMLERR_NO_ERROR, initResult);
// Acquire dummy handle
HSZ hsz = Ddeml.INSTANCE.DdeCreateStringHandle(pidInst.getValue().intValue(), "Dummy", Ddeml.CP_WINUNICODE);
String testStringPart1 = "Hallo ";
String testStringPart2 = "Welt";
// Create Handle
// String in DDE can not exceed 255 Chars
Memory mem = new Memory(256 * 2);
mem.setWideString(0, testStringPart1);
HDDEDATA data = Ddeml.INSTANCE.DdeCreateDataHandle(pidInst.getValue().intValue(), mem, testStringPart1.length() * 2, 0, hsz, WinUser.CF_UNICODETEXT, Ddeml.HDATA_APPOWNED);
mem.setWideString(0, testStringPart2);
Ddeml.INSTANCE.DdeAddData(data, mem, (testStringPart2.length() + 1) * 2, testStringPart1.length() * 2);
DWORDByReference dataSize = new DWORDByReference();
Pointer resultPointer = Ddeml.INSTANCE.DdeAccessData(data, dataSize);
assertEquals((testStringPart1.length() + testStringPart2.length() + 1) * 2, dataSize.getValue().intValue());
assertEquals(testStringPart1 + testStringPart2, resultPointer.getWideString(0));
boolean result = Ddeml.INSTANCE.DdeUnaccessData(data);
int readSize = Ddeml.INSTANCE.DdeGetData(data, mem, (int) mem.size(), 0);
assertEquals((testStringPart1.length() + testStringPart2.length() + 1) * 2, readSize);
assertEquals(testStringPart1 + testStringPart2, mem.getWideString(0));
assertTrue(result);
result = Ddeml.INSTANCE.DdeFreeDataHandle(data);
assertTrue(result);
synchronized (mem) {
}
result = Ddeml.INSTANCE.DdeUninitialize(pidInst.getValue().intValue());
assertTrue(result);
}
use of com.sun.jna.platform.win32.Ddeml.HDDEDATA 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);
}
}
use of com.sun.jna.platform.win32.Ddeml.HDDEDATA 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);
}
}
use of com.sun.jna.platform.win32.Ddeml.HDDEDATA in project jna by java-native-access.
the class DdemlUtilTest method testEnableCallback.
@Test
public void testEnableCallback() throws InterruptedException {
final String serviceName = "TestService";
final String topicName = "TestTopic";
final String testExecute = "Execute�������";
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) {
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);
server.enableCallback(Ddeml.EC_DISABLE);
assertThat(server.enableCallback(Ddeml.EC_QUERYWAITING), is(false));
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);
con.execute(testExecute, Ddeml.TIMEOUT_ASYNC, result, null);
assertThat(server.enableCallback(Ddeml.EC_QUERYWAITING), is(true));
server.enableCallback(Ddeml.EC_ENABLEALL);
assertTrue(executesProcessed.await(3, TimeUnit.SECONDS));
} finally {
closeQuitely(client);
closeQuitely(server);
}
}
use of com.sun.jna.platform.win32.Ddeml.HDDEDATA in project jna by java-native-access.
the class DdemlUtilTest method testAdvise.
@Test
public void testAdvise() throws InterruptedException {
final String serviceName = "TestService";
final String topicName = "TestTopic";
final String itemName = "TestItem";
final String testValue = "Execute�������";
final CountDownLatch adviseStartReceived = new CountDownLatch(1);
final CountDownLatch adviseDataRequestReceived = new CountDownLatch(1);
final CountDownLatch adviseDataReceived = new CountDownLatch(1);
StandaloneDdeClient client = null;
StandaloneDdeClient server = null;
try {
client = new StandaloneDdeClient() {
private final AdvdataHandler advdataHandler = new DdemlUtil.AdvdataHandler() {
public int onAdvdata(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item, HDDEDATA hdata) {
if (dataFormat == WinUser.CF_UNICODETEXT && topicName.equals(queryString(topic)) && itemName.equals(queryString(item))) {
Pointer pointer = accessData(hdata, null);
try {
if (testValue.equals(pointer.getWideString(0))) {
adviseDataReceived.countDown();
}
} finally {
unaccessData(hdata);
}
}
return Ddeml.DDE_FACK;
}
};
{
registerAdvdataHandler(advdataHandler);
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 AdvreqHandler advreqHandler = new AdvreqHandler() {
public HDDEDATA onAdvreq(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item, int count) {
adviseDataRequestReceived.countDown();
Memory mem = new Memory((testValue.length() + 1) * 2);
mem.setWideString(0, testValue);
return createDataHandle(mem, (int) mem.size(), 0, item, dataFormat, 0);
}
};
private final AdvstartHandler advstartHandler = new AdvstartHandler() {
public boolean onAdvstart(int transactionType, int dataFormat, HCONV hconv, HSZ topic, HSZ item) {
adviseStartReceived.countDown();
return dataFormat == WinUser.CF_UNICODETEXT && topicName.equals(queryString(topic)) && itemName.equals(queryString(item));
}
};
{
registerConnectHandler(connectHandler);
registerAdvReqHandler(advreqHandler);
registerAdvstartHandler(advstartHandler);
this.initialize(Ddeml.APPCMD_FILTERINITS | Ddeml.CBF_SKIP_ALLNOTIFICATIONS);
}
};
server.nameService(serviceName, Ddeml.DNS_REGISTER);
IDdeConnection con = client.connect(serviceName, topicName, null);
con.advstart(itemName, WinUser.CF_UNICODETEXT, 5 * 1000, null, null);
assertTrue(adviseStartReceived.await(5, TimeUnit.SECONDS));
server.postAdvise(topicName, itemName);
assertTrue(adviseDataRequestReceived.await(5, TimeUnit.SECONDS));
assertTrue(adviseDataReceived.await(5, TimeUnit.SECONDS));
} finally {
closeQuitely(client);
closeQuitely(server);
}
}
Aggregations