-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
DdemlUtil.java
3896 lines (3563 loc) · 166 KB
/
DdemlUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* The contents of this file is dual-licensed under 2
* alternative Open Source/Free licenses: LGPL 2.1 or later and
* Apache License 2.0. (starting with JNA version 4.0.0).
*
* You can freely decide which license you want to apply to
* the project.
*
* You may obtain a copy of the LGPL License at:
*
* http://www.gnu.org/licenses/licenses.html
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "LGPL2.1".
*
* You may obtain a copy of the Apache License at:
*
* http://www.apache.org/licenses/
*
* A copy is also included in the downloadable source code package
* containing JNA, in file "AL2.0".
*/
package com.sun.jna.platform.win32;
import com.sun.jna.Memory;
import com.sun.jna.Pointer;
import com.sun.jna.platform.win32.BaseTSD.DWORD_PTR;
import com.sun.jna.platform.win32.BaseTSD.ULONG_PTR;
import com.sun.jna.platform.win32.Ddeml.CONVCONTEXT;
import com.sun.jna.platform.win32.Ddeml.CONVINFO;
import com.sun.jna.platform.win32.Ddeml.HCONV;
import com.sun.jna.platform.win32.Ddeml.HCONVLIST;
import com.sun.jna.platform.win32.Ddeml.HDDEDATA;
import com.sun.jna.platform.win32.Ddeml.HSZ;
import com.sun.jna.platform.win32.Ddeml.HSZPAIR;
import com.sun.jna.platform.win32.User32Util.MessageLoopThread;
import com.sun.jna.platform.win32.User32Util.MessageLoopThread.Handler;
import com.sun.jna.win32.W32APIOptions;
import java.io.Closeable;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* DdemlUtil defines helper classes, that help with manageing DDE communications.
*/
public abstract class DdemlUtil {
/**
* StandaloneDdeClient is a convenience class, that wraps a DdeClient and
* a {@link User32Util.MessageLoopThread}. The DdeClient needs a running
* windows message loop, which is supplied by the MessageLoopThread.
*/
public static class StandaloneDdeClient implements IDdeClient, Closeable {
private final MessageLoopThread messageLoop = new MessageLoopThread();
private final IDdeClient ddeClient;
private final IDdeClient clientDelegate;
public StandaloneDdeClient() {
ddeClient = new DdeClient();
IDdeClient messageLoopHandler = (IDdeClient) Proxy.newProxyInstance(StandaloneDdeClient.class.getClassLoader(),
new Class[]{IDdeClient.class},
messageLoop.new Handler(ddeClient));
clientDelegate = (IDdeClient) Proxy.newProxyInstance(StandaloneDdeClient.class.getClassLoader(),
new Class[]{IDdeClient.class},
new MessageLoopWrapper(messageLoop, messageLoopHandler));
messageLoop.setDaemon(true);
messageLoop.start();
}
public Integer getInstanceIdentitifier() {
return ddeClient.getInstanceIdentitifier();
}
public void initialize(int afCmd) throws DdemlException {
clientDelegate.initialize(afCmd);
}
public Ddeml.HSZ createStringHandle(String value) throws DdemlException {
return clientDelegate.createStringHandle(value);
}
public void nameService(Ddeml.HSZ name, int afCmd) throws DdemlException {
clientDelegate.nameService(name, afCmd);
}
public int getLastError() {
return clientDelegate.getLastError();
}
public IDdeConnection connect(Ddeml.HSZ service, Ddeml.HSZ topic, Ddeml.CONVCONTEXT convcontext) {
return clientDelegate.connect(service, topic, convcontext);
}
public String queryString(Ddeml.HSZ value) throws DdemlException {
return clientDelegate.queryString(value);
}
@Override
public Ddeml.HDDEDATA createDataHandle(Pointer pSrc, int cb, int cbOff, Ddeml.HSZ hszItem, int wFmt, int afCmd) {
return clientDelegate.createDataHandle(pSrc, cb, cbOff, hszItem, wFmt, afCmd);
}
@Override
public void freeDataHandle(Ddeml.HDDEDATA hData) {
clientDelegate.freeDataHandle(hData);
}
@Override
public Ddeml.HDDEDATA addData(Ddeml.HDDEDATA hData, Pointer pSrc, int cb, int cbOff) {
return clientDelegate.addData(hData, pSrc, cb, cbOff);
}
@Override
public int getData(Ddeml.HDDEDATA hData, Pointer pDst, int cbMax, int cbOff) {
return clientDelegate.getData(hData, pDst, cbMax, cbOff);
}
@Override
public Pointer accessData(Ddeml.HDDEDATA hData, WinDef.DWORDByReference pcbDataSize) {
return clientDelegate.accessData(hData, pcbDataSize);
}
@Override
public void unaccessData(Ddeml.HDDEDATA hData) {
clientDelegate.unaccessData(hData);
}
@Override
public void postAdvise(Ddeml.HSZ hszTopic, Ddeml.HSZ hszItem) {
clientDelegate.postAdvise(hszTopic, hszItem);
}
public void close() throws IOException {
clientDelegate.uninitialize();
messageLoop.exit();
}
@Override
public boolean freeStringHandle(Ddeml.HSZ value) {
return clientDelegate.freeStringHandle(value);
}
@Override
public boolean keepStringHandle(Ddeml.HSZ value) {
return clientDelegate.keepStringHandle(value);
}
@Override
public void abandonTransactions() {
clientDelegate.abandonTransactions();
}
@Override
public IDdeConnectionList connectList(Ddeml.HSZ service, Ddeml.HSZ topic, IDdeConnectionList existingList, Ddeml.CONVCONTEXT ctx) {
return clientDelegate.connectList(service, topic, existingList, ctx);
}
@Override
public boolean enableCallback(int wCmd) {
return clientDelegate.enableCallback(wCmd);
}
@Override
public IDdeConnection wrap(HCONV conv) {
return clientDelegate.wrap(conv);
}
@Override
public IDdeConnection connect(String service, String topic, Ddeml.CONVCONTEXT convcontext) {
return clientDelegate.connect(service, topic, convcontext);
}
@Override
public boolean uninitialize() {
return clientDelegate.uninitialize();
}
@Override
public void postAdvise(String hszTopic, String hszItem) {
clientDelegate.postAdvise(hszTopic, hszItem);
}
@Override
public IDdeConnectionList connectList(String service, String topic, IDdeConnectionList existingList, Ddeml.CONVCONTEXT ctx) {
return clientDelegate.connectList(service, topic, existingList, ctx);
}
@Override
public void nameService(String name, int afCmd) throws DdemlException {
clientDelegate.nameService(name, afCmd);
}
@Override
public void registerAdvstartHandler(AdvstartHandler handler) {
clientDelegate.registerAdvstartHandler(handler);
}
@Override
public void unregisterAdvstartHandler(AdvstartHandler handler) {
clientDelegate.unregisterAdvstartHandler(handler);
}
@Override
public void registerAdvstopHandler(AdvstopHandler handler) {
clientDelegate.registerAdvstopHandler(handler);
}
@Override
public void unregisterAdvstopHandler(AdvstopHandler handler) {
clientDelegate.unregisterAdvstopHandler(handler);
}
@Override
public void registerConnectHandler(ConnectHandler handler) {
clientDelegate.registerConnectHandler(handler);
}
@Override
public void unregisterConnectHandler(ConnectHandler handler) {
clientDelegate.unregisterConnectHandler(handler);
}
@Override
public void registerAdvReqHandler(AdvreqHandler handler) {
clientDelegate.registerAdvReqHandler(handler);
}
@Override
public void unregisterAdvReqHandler(AdvreqHandler handler) {
clientDelegate.unregisterAdvReqHandler(handler);
}
@Override
public void registerRequestHandler(RequestHandler handler) {
clientDelegate.registerRequestHandler(handler);
}
@Override
public void unregisterRequestHandler(RequestHandler handler) {
clientDelegate.unregisterRequestHandler(handler);
}
@Override
public void registerWildconnectHandler(WildconnectHandler handler) {
clientDelegate.registerWildconnectHandler(handler);
}
@Override
public void unregisterWildconnectHandler(WildconnectHandler handler) {
clientDelegate.unregisterWildconnectHandler(handler);
}
@Override
public void registerAdvdataHandler(AdvdataHandler handler) {
clientDelegate.registerAdvdataHandler(handler);
}
@Override
public void unregisterAdvdataHandler(AdvdataHandler handler) {
clientDelegate.unregisterAdvdataHandler(handler);
}
@Override
public void registerExecuteHandler(ExecuteHandler handler) {
clientDelegate.registerExecuteHandler(handler);
}
@Override
public void unregisterExecuteHandler(ExecuteHandler handler) {
clientDelegate.unregisterExecuteHandler(handler);
}
@Override
public void registerPokeHandler(PokeHandler handler) {
clientDelegate.registerPokeHandler(handler);
}
@Override
public void unregisterPokeHandler(PokeHandler handler) {
clientDelegate.unregisterPokeHandler(handler);
}
@Override
public void registerConnectConfirmHandler(ConnectConfirmHandler handler) {
clientDelegate.registerConnectConfirmHandler(handler);
}
@Override
public void unregisterConnectConfirmHandler(ConnectConfirmHandler handler) {
clientDelegate.unregisterConnectConfirmHandler(handler);
}
@Override
public void registerDisconnectHandler(DisconnectHandler handler) {
clientDelegate.registerDisconnectHandler(handler);
}
@Override
public void unregisterDisconnectHandler(DisconnectHandler handler) {
clientDelegate.unregisterDisconnectHandler(handler);
}
@Override
public void registerErrorHandler(ErrorHandler handler) {
clientDelegate.registerErrorHandler(handler);
}
@Override
public void unregisterErrorHandler(ErrorHandler handler) {
clientDelegate.unregisterErrorHandler(handler);
}
@Override
public void registerRegisterHandler(RegisterHandler handler) {
clientDelegate.registerRegisterHandler(handler);
}
@Override
public void unregisterRegisterHandler(RegisterHandler handler) {
clientDelegate.unregisterRegisterHandler(handler);
}
@Override
public void registerXactCompleteHandler(XactCompleteHandler handler) {
clientDelegate.registerXactCompleteHandler(handler);
}
@Override
public void unregisterXactCompleteHandler(XactCompleteHandler handler) {
clientDelegate.unregisterXactCompleteHandler(handler);
}
@Override
public void registerUnregisterHandler(UnregisterHandler handler) {
clientDelegate.registerUnregisterHandler(handler);
}
@Override
public void unregisterUnregisterHandler(UnregisterHandler handler) {
clientDelegate.unregisterUnregisterHandler(handler);
}
@Override
public void registerMonitorHandler(MonitorHandler handler) {
clientDelegate.registerMonitorHandler(handler);
}
@Override
public void unregisterMonitorHandler(MonitorHandler handler) {
clientDelegate.unregisterMonitorHandler(handler);
}
}
private static class MessageLoopWrapper implements InvocationHandler {
private final Object delegate;
private final MessageLoopThread loopThread;
public MessageLoopWrapper(MessageLoopThread thread, Object delegate) {
this.loopThread = thread;
this.delegate = delegate;
}
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable {
try {
Object result = method.invoke(delegate, args);
Class<?> wrapClass = null;
if ( result instanceof IDdeConnection ) {
wrapClass = IDdeConnection.class;
} else if (result instanceof IDdeConnectionList) {
wrapClass = IDdeConnectionList.class;
} else if (result instanceof IDdeClient) {
wrapClass = IDdeClient.class;
}
if(wrapClass != null && method.getReturnType().isAssignableFrom(wrapClass)) {
result = wrap(result, wrapClass);
}
return result;
} catch (InvocationTargetException ex) {
Throwable cause = ex.getCause();
if (cause instanceof Exception) {
throw (Exception) cause;
} else {
throw ex;
}
}
}
private <V> V wrap(V delegate, Class clazz) {
V messageLoopHandler = (V) Proxy.newProxyInstance(StandaloneDdeClient.class.getClassLoader(),
new Class[]{clazz},
loopThread.new Handler(delegate));
V clientDelegate = (V) Proxy.newProxyInstance(StandaloneDdeClient.class.getClassLoader(),
new Class[]{clazz},
new MessageLoopWrapper(loopThread, messageLoopHandler));
return clientDelegate;
}
}
public static class DdeConnection implements IDdeConnection {
private HCONV conv;
private final IDdeClient client;
public DdeConnection(IDdeClient client, HCONV conv) {
this.conv = conv;
this.client = client;
}
public Ddeml.HCONV getConv() {
return conv;
}
@Override
public void abandonTransaction(int transactionId) {
boolean result = Ddeml.INSTANCE.DdeAbandonTransaction(client.getInstanceIdentitifier(), conv, transactionId);
if(! result) {
throw DdemlException.create(client.getLastError());
}
}
public void abandonTransactions() {
boolean result = Ddeml.INSTANCE.DdeAbandonTransaction(client.getInstanceIdentitifier(), conv, 0);
if(! result) {
throw DdemlException.create(client.getLastError());
}
}
@Override
public Ddeml.HDDEDATA clientTransaction(Pointer data, int dataLength, Ddeml.HSZ item, int wFmt, int transaction, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
if(timeout == Ddeml.TIMEOUT_ASYNC && result == null) {
result = new WinDef.DWORDByReference();
}
Ddeml.HDDEDATA returnData = Ddeml.INSTANCE.DdeClientTransaction(data, dataLength, conv, item, wFmt, transaction, timeout, result);
if(returnData == null) {
throw DdemlException.create(client.getLastError());
}
if (userHandle != null) {
if (timeout != Ddeml.TIMEOUT_ASYNC) {
setUserHandle(Ddeml.QID_SYNC, userHandle);
} else {
setUserHandle(result.getValue().intValue(), userHandle);
}
}
return returnData;
}
public Ddeml.HDDEDATA clientTransaction(Pointer data, int dataLength, String item, int wFmt, int transaction, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
HSZ itemHSZ = null;
try {
itemHSZ = client.createStringHandle(item);
return clientTransaction(data, dataLength, itemHSZ, wFmt, transaction, timeout, result, userHandle);
} finally {
client.freeStringHandle(itemHSZ);
}
}
@Override
public void poke(Pointer data, int dataLength, Ddeml.HSZ item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
clientTransaction(data, dataLength, item, wFmt, Ddeml.XTYP_POKE, timeout, result, userHandle);
}
public void poke(Pointer data, int dataLength, String item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
HSZ itemHSZ = null;
try {
itemHSZ = client.createStringHandle(item);
poke(data, dataLength, itemHSZ, wFmt, timeout, result, userHandle);
} finally {
client.freeStringHandle(itemHSZ);
}
}
@Override
public Ddeml.HDDEDATA request(Ddeml.HSZ item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
return clientTransaction(Pointer.NULL, 0, item, wFmt, Ddeml.XTYP_REQUEST, timeout, result, userHandle);
}
public Ddeml.HDDEDATA request(String item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
HSZ itemHSZ = null;
try {
itemHSZ = client.createStringHandle(item);
return request(itemHSZ, wFmt, timeout, result, userHandle);
} finally {
client.freeStringHandle(itemHSZ);
}
}
@Override
public void execute(String executeString, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
Memory mem = new Memory(executeString.length() * 2 + 2);
mem.setWideString(0, executeString);
clientTransaction(mem, (int) mem.size(), (HSZ) null, 0, Ddeml.XTYP_EXECUTE, timeout, result, userHandle);
}
@Override
public void advstart(Ddeml.HSZ item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
clientTransaction(Pointer.NULL, 0, item, wFmt, Ddeml.XTYP_ADVSTART, timeout, result, userHandle);
}
public void advstart(String item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
HSZ itemHSZ = null;
try {
itemHSZ = client.createStringHandle(item);
advstart(itemHSZ, wFmt, timeout, result, userHandle);
} finally {
client.freeStringHandle(itemHSZ);
}
}
@Override
public void advstop(Ddeml.HSZ item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
clientTransaction(Pointer.NULL, 0, item, wFmt, Ddeml.XTYP_ADVSTOP, timeout, result, userHandle);
}
public void advstop(String item, int wFmt, int timeout, WinDef.DWORDByReference result, DWORD_PTR userHandle) {
HSZ itemHSZ = null;
try {
itemHSZ = client.createStringHandle(item);
advstop(itemHSZ, wFmt, timeout, result, userHandle);
} finally {
client.freeStringHandle(itemHSZ);
}
}
public void impersonateClient() {
boolean result = Ddeml.INSTANCE.DdeImpersonateClient(conv);
if (!result) {
throw DdemlException.create(client.getLastError());
}
}
public void close() {
boolean result = Ddeml.INSTANCE.DdeDisconnect(conv);
if(! result) {
throw DdemlException.create(client.getLastError());
}
}
public void reconnect() {
Ddeml.HCONV newConv = Ddeml.INSTANCE.DdeReconnect(conv);
if(newConv != null) {
conv = newConv;
} else {
throw DdemlException.create(client.getLastError());
}
}
public boolean enableCallback(int wCmd) {
boolean result = Ddeml.INSTANCE.DdeEnableCallback(client.getInstanceIdentitifier(), conv, wCmd);
if ((!result) && wCmd == Ddeml.EC_QUERYWAITING) {
throw DdemlException.create(client.getLastError());
}
return result;
}
public void setUserHandle(int id, DWORD_PTR hUser) throws DdemlException {
boolean result = Ddeml.INSTANCE.DdeSetUserHandle(conv, id, hUser);
if (!result) {
throw DdemlException.create(client.getLastError());
}
}
public CONVINFO queryConvInfo(int idTransaction) throws DdemlException {
CONVINFO convInfo = new Ddeml.CONVINFO();
convInfo.cb = convInfo.size();
convInfo.ConvCtxt.cb = convInfo.ConvCtxt.size();
convInfo.write();
int result = Ddeml.INSTANCE.DdeQueryConvInfo(conv, idTransaction, convInfo);
if (result == 0) {
throw DdemlException.create(client.getLastError());
}
return convInfo;
}
}
public static class DdeConnectionList implements IDdeConnectionList {
private final IDdeClient client;
private final HCONVLIST convList;
public DdeConnectionList(IDdeClient client, Ddeml.HCONVLIST convList) {
this.convList = convList;
this.client = client;
}
@Override
public Ddeml.HCONVLIST getHandle() {
return this.convList;
}
@Override
public IDdeConnection queryNextServer(IDdeConnection prevConnection) {
Ddeml.HCONV conv = Ddeml.INSTANCE.DdeQueryNextServer(
convList,
prevConnection != null ? prevConnection.getConv() : null);
if(conv != null) {
return new DdeConnection(client, conv);
} else {
return null;
}
}
@Override
public void close() {
boolean result = Ddeml.INSTANCE.DdeDisconnectList(convList);
if(! result){
throw DdemlException.create(client.getLastError());
}
}
}
public static class DdeClient implements IDdeClient {
private Integer idInst;
private final DdeAdapter ddeAdapter = new DdeAdapter();
public Integer getInstanceIdentitifier() {
return idInst;
}
public void initialize(int afCmd) throws DdemlException {
WinDef.DWORDByReference pidInst = new WinDef.DWORDByReference();
Integer result = Ddeml.INSTANCE.DdeInitialize(pidInst, ddeAdapter, afCmd, 0);
if(result != Ddeml.DMLERR_NO_ERROR) {
throw DdemlException.create(result);
}
idInst = pidInst.getValue().intValue();
if(ddeAdapter instanceof DdeAdapter) {
ddeAdapter.setInstanceIdentifier(idInst);
}
}
public Ddeml.HSZ createStringHandle(String value) throws DdemlException {
if(value == null) {
return null;
}
int codePage;
if(W32APIOptions.DEFAULT_OPTIONS == W32APIOptions.UNICODE_OPTIONS) {
codePage = Ddeml.CP_WINUNICODE;
} else {
codePage = Ddeml.CP_WINANSI;
}
Ddeml.HSZ handle = Ddeml.INSTANCE.DdeCreateStringHandle(idInst, value, codePage);
if(handle == null) {
throw DdemlException.create(getLastError());
}
return handle;
}
public void nameService(Ddeml.HSZ name, int afCmd) throws DdemlException {
Ddeml.HDDEDATA handle = Ddeml.INSTANCE.DdeNameService(idInst, name, new Ddeml.HSZ(), afCmd);
if (handle == null) {
throw DdemlException.create(getLastError());
}
}
public void nameService(String name, int afCmd) throws DdemlException {
HSZ nameHSZ = null;
try {
nameHSZ = createStringHandle(name);
nameService(nameHSZ, afCmd);
} finally {
freeStringHandle(nameHSZ);
}
}
public int getLastError() {
return Ddeml.INSTANCE.DdeGetLastError(idInst);
}
public IDdeConnection connect(Ddeml.HSZ service, Ddeml.HSZ topic, Ddeml.CONVCONTEXT convcontext) {
Ddeml.HCONV hconv = Ddeml.INSTANCE.DdeConnect(idInst, service, topic, convcontext);
if(hconv == null) {
throw DdemlException.create(getLastError());
}
return new DdeConnection(this, hconv);
}
public IDdeConnection connect(String service, String topic, Ddeml.CONVCONTEXT convcontext) {
HSZ serviceHSZ = null;
HSZ topicHSZ = null;
try {
serviceHSZ = createStringHandle(service);
topicHSZ = createStringHandle(topic);
return connect(serviceHSZ, topicHSZ, convcontext);
} finally {
freeStringHandle(topicHSZ);
freeStringHandle(serviceHSZ);
}
}
public String queryString(Ddeml.HSZ value) throws DdemlException {
int codePage;
int byteWidth;
if(W32APIOptions.DEFAULT_OPTIONS == W32APIOptions.UNICODE_OPTIONS) {
codePage = Ddeml.CP_WINUNICODE;
byteWidth = 2;
} else {
codePage = Ddeml.CP_WINANSI;
byteWidth = 1;
}
Memory buffer = new Memory((256 + 1) * byteWidth);
try {
int length = Ddeml.INSTANCE.DdeQueryString(idInst, value, buffer, 256, codePage);
if (W32APIOptions.DEFAULT_OPTIONS == W32APIOptions.UNICODE_OPTIONS) {
return buffer.getWideString(0);
} else {
return buffer.getString(0);
}
} finally {
buffer.valid();
}
}
public Ddeml.HDDEDATA createDataHandle(Pointer pSrc, int cb, int cbOff, Ddeml.HSZ hszItem, int wFmt, int afCmd) {
Ddeml.HDDEDATA returnData = Ddeml.INSTANCE.DdeCreateDataHandle(idInst, pSrc, cb, cbOff, hszItem, wFmt, afCmd);
if(returnData == null) {
throw DdemlException.create(getLastError());
}
return returnData;
}
public void freeDataHandle(Ddeml.HDDEDATA hData) {
boolean result = Ddeml.INSTANCE.DdeFreeDataHandle(hData);
if(! result) {
throw DdemlException.create(getLastError());
}
}
public Ddeml.HDDEDATA addData(Ddeml.HDDEDATA hData, Pointer pSrc, int cb, int cbOff) {
Ddeml.HDDEDATA newHandle = Ddeml.INSTANCE.DdeAddData(hData, pSrc, cb, cbOff);
if(newHandle == null) {
throw DdemlException.create(getLastError());
}
return newHandle;
}
public int getData(Ddeml.HDDEDATA hData, Pointer pDst, int cbMax, int cbOff) {
int result = Ddeml.INSTANCE.DdeGetData(hData, pDst, cbMax, cbOff);
int errorCode = getLastError();
if(errorCode != Ddeml.DMLERR_NO_ERROR) {
throw DdemlException.create(errorCode);
}
return result;
}
public Pointer accessData(Ddeml.HDDEDATA hData, WinDef.DWORDByReference pcbDataSize) {
Pointer result = Ddeml.INSTANCE.DdeAccessData(hData, pcbDataSize);
if(result == null) {
throw DdemlException.create(getLastError());
}
return result;
}
public void unaccessData(Ddeml.HDDEDATA hData) {
boolean result = Ddeml.INSTANCE.DdeUnaccessData(hData);
if (!result) {
throw DdemlException.create(getLastError());
}
}
public void postAdvise(Ddeml.HSZ hszTopic, Ddeml.HSZ hszItem) {
boolean result = Ddeml.INSTANCE.DdePostAdvise(idInst, hszTopic, hszItem);
if (!result) {
throw DdemlException.create(getLastError());
}
}
public void postAdvise(String topic, String item) {
HSZ itemHSZ = null;
HSZ topicHSZ = null;
try {
topicHSZ = createStringHandle(topic);
itemHSZ = createStringHandle(item);
postAdvise(topicHSZ, itemHSZ);
} finally {
freeStringHandle(topicHSZ);
freeStringHandle(itemHSZ);
}
}
public boolean freeStringHandle(Ddeml.HSZ value) {
if(value == null) {
return true;
}
return Ddeml.INSTANCE.DdeFreeStringHandle(idInst, value);
}
public boolean keepStringHandle(Ddeml.HSZ value) {
return Ddeml.INSTANCE.DdeKeepStringHandle(idInst, value);
}
public void abandonTransactions() {
boolean result = Ddeml.INSTANCE.DdeAbandonTransaction(idInst, null, 0);
if(! result) {
throw DdemlException.create(getLastError());
}
}
public IDdeConnectionList connectList(Ddeml.HSZ service, Ddeml.HSZ topic, IDdeConnectionList existingList, Ddeml.CONVCONTEXT ctx) {
Ddeml.HCONVLIST convlist = Ddeml.INSTANCE.DdeConnectList(idInst, service, topic, existingList != null ? existingList.getHandle() : null, ctx);
if(convlist == null) {
throw DdemlException.create(getLastError());
} else {
return new DdeConnectionList(this, convlist);
}
}
public IDdeConnectionList connectList(String service, String topic, IDdeConnectionList existingList, Ddeml.CONVCONTEXT ctx) {
HSZ serviceHSZ = null;
HSZ topicHSZ = null;
try {
serviceHSZ = createStringHandle(service);
topicHSZ = createStringHandle(topic);
return connectList(serviceHSZ, topicHSZ, existingList, ctx);
} finally {
freeStringHandle(topicHSZ);
freeStringHandle(serviceHSZ);
}
}
public boolean enableCallback(int wCmd) {
boolean result = Ddeml.INSTANCE.DdeEnableCallback(idInst, null, wCmd);
if ((!result) && wCmd != Ddeml.EC_QUERYWAITING) {
int errorCode = getLastError();
if(errorCode != Ddeml.DMLERR_NO_ERROR) {
throw DdemlException.create(getLastError());
}
}
return result;
}
public boolean uninitialize() {
return Ddeml.INSTANCE.DdeUninitialize(idInst);
}
public void close() {
uninitialize();
}
public IDdeConnection wrap(HCONV hconv) {
return new DdeConnection(this, hconv);
}
public void unregisterDisconnectHandler(DisconnectHandler handler) {
ddeAdapter.unregisterDisconnectHandler(handler);
}
public void registerAdvstartHandler(AdvstartHandler handler) {
ddeAdapter.registerAdvstartHandler(handler);
}
public void unregisterAdvstartHandler(AdvstartHandler handler) {
ddeAdapter.unregisterAdvstartHandler(handler);
}
public void registerAdvstopHandler(AdvstopHandler handler) {
ddeAdapter.registerAdvstopHandler(handler);
}
public void unregisterAdvstopHandler(AdvstopHandler handler) {
ddeAdapter.unregisterAdvstopHandler(handler);
}
public void registerConnectHandler(ConnectHandler handler) {
ddeAdapter.registerConnectHandler(handler);
}
public void unregisterConnectHandler(ConnectHandler handler) {
ddeAdapter.unregisterConnectHandler(handler);
}
public void registerAdvReqHandler(AdvreqHandler handler) {
ddeAdapter.registerAdvReqHandler(handler);
}
public void unregisterAdvReqHandler(AdvreqHandler handler) {
ddeAdapter.unregisterAdvReqHandler(handler);
}
public void registerRequestHandler(RequestHandler handler) {
ddeAdapter.registerRequestHandler(handler);
}
public void unregisterRequestHandler(RequestHandler handler) {
ddeAdapter.unregisterRequestHandler(handler);
}
public void registerWildconnectHandler(WildconnectHandler handler) {
ddeAdapter.registerWildconnectHandler(handler);
}
public void unregisterWildconnectHandler(WildconnectHandler handler) {
ddeAdapter.unregisterWildconnectHandler(handler);
}
public void registerAdvdataHandler(AdvdataHandler handler) {
ddeAdapter.registerAdvdataHandler(handler);
}
public void unregisterAdvdataHandler(AdvdataHandler handler) {
ddeAdapter.unregisterAdvdataHandler(handler);
}
public void registerExecuteHandler(ExecuteHandler handler) {
ddeAdapter.registerExecuteHandler(handler);
}
public void unregisterExecuteHandler(ExecuteHandler handler) {
ddeAdapter.unregisterExecuteHandler(handler);
}
public void registerPokeHandler(PokeHandler handler) {
ddeAdapter.registerPokeHandler(handler);
}
public void unregisterPokeHandler(PokeHandler handler) {
ddeAdapter.unregisterPokeHandler(handler);
}
public void registerConnectConfirmHandler(ConnectConfirmHandler handler) {
ddeAdapter.registerConnectConfirmHandler(handler);
}
public void unregisterConnectConfirmHandler(ConnectConfirmHandler handler) {
ddeAdapter.unregisterConnectConfirmHandler(handler);
}
public void registerDisconnectHandler(DisconnectHandler handler) {
ddeAdapter.registerDisconnectHandler(handler);
}
public void registerErrorHandler(ErrorHandler handler) {
ddeAdapter.registerErrorHandler(handler);
}
public void unregisterErrorHandler(ErrorHandler handler) {
ddeAdapter.unregisterErrorHandler(handler);
}
public void registerRegisterHandler(RegisterHandler handler) {
ddeAdapter.registerRegisterHandler(handler);
}
public void unregisterRegisterHandler(RegisterHandler handler) {
ddeAdapter.unregisterRegisterHandler(handler);
}
public void registerXactCompleteHandler(XactCompleteHandler handler) {
ddeAdapter.registerXactCompleteHandler(handler);
}
public void unregisterXactCompleteHandler(XactCompleteHandler handler) {
ddeAdapter.xactCompleteXactCompleteHandler(handler);
}
public void registerUnregisterHandler(UnregisterHandler handler) {
ddeAdapter.registerUnregisterHandler(handler);
}
public void unregisterUnregisterHandler(UnregisterHandler handler) {
ddeAdapter.unregisterUnregisterHandler(handler);
}
public void registerMonitorHandler(MonitorHandler handler) {
ddeAdapter.registerMonitorHandler(handler);
}
public void unregisterMonitorHandler(MonitorHandler handler) {
ddeAdapter.unregisterMonitorHandler(handler);
}
}
public interface AdvstartHandler {
/**
* A server callback function should return TRUE to allow an advise loop
* on the specified topic name and item name pair, or FALSE to deny the
* advise loop. If the callback function returns TRUE, any subsequent
* calls to the DdePostAdvise function by the server on the same topic
* name and item name pair causes the system to send XTYP_ADVREQ
* transactions to the server.
*
* <p>
* <strong>Remarks</strong></p>
* <p>If a client requests an advise loop on a topic name, item name,
* and data format for an advise loop that is already established, the
* Dynamic Data Exchange Management Library (DDEML) does not create a
* duplicate advise loop but instead alters the advise loop flags
* (XTYPF_ACKREQ and XTYPF_NODATA) to match the latest request.</p>