summaryrefslogblamecommitdiffstats
path: root/private/ole32/com/dde/server/srvr.cxx
blob: 2f2d8eb40f02683a339a171761218bae00e5d3f2 (plain) (tree)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                       

/****************************** Module Header ******************************\
* Module Name: Srvr.c Server Main module
*
* Purpose: Includes All the server communication related routines.
*
* Created: Oct 1990.
*
* Copyright (c) 1985, 1986, 1987, 1988, 1989  Microsoft Corporation
*
* History:
*    Raor:   Wrote the original version.
*
*
\***************************************************************************/

#include "ole2int.h"
//#include <shellapi.h>
// #include "cmacs.h"
#include <dde.h>

// for RemDdeRevokeClassFactory and HDDESRVR
#include <olerem.h>

#include "srvr.h"
#include "ddedebug.h"
#include "ddesrvr.h"
ASSERTDATA

#define WM_DONOTDESTROY WM_USER+1

#ifdef FIREWALLS
BOOL    bShowed = FALSE;
void    ShowVersion (void);
#endif

#ifdef _CHICAGO_
#define DdeCHAR CHAR
#define Ddelstrcmp lstrcmpA
#define DdeGetClassName GetClassNameA
#define szCDDEServer "CDDEServer"
#else
#define DdeCHAR WCHAR
#define Ddelstrcmp lstrcmpW
#define DdeGetClassName GetClassName
#define szCDDEServer OLESTR("CDDEServer")
#endif

//+---------------------------------------------------------------------------
//
//  Method:     CDDEServer::Create
//
//  Synopsis:   Create a server window to service a particular class
//
//  Effects:    Using lpclass, and the information in lpDdeInfo, create
//              a server window that is ready to respond to initiate
//              messages from this class.
//
//  Arguments:  [lpclass] --    Class name
//              [rclsid] -- Class ID
//              [lpDdeInfo] --  Class Object information
//              [phwnd] --      Out pointer for new window
//              [aOriginalClass] -- For TreatAs/Convert to case
//              [cnvtyp] --         Conversion type
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    5-28-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL CDDEServer::Create
    (LPOLESTR          lpclass,
     REFCLSID          rclsid,
     LPDDECLASSINFO    lpDdeInfo,
     HWND FAR *        phwnd,
     ATOM              aOriginalClass,
     CNVTYP            cnvtyp)
{
    // REVIEW   what happens if we have two MDI servers register the
    //          same class factory?.

    LPSRVR  lpDDEsrvr   = NULL;
    ATOM    aExe        = NULL;

    intrDebugOut((DEB_DDE_INIT,
                  "0 _IN CDDEServer::Create(lpclass=%ws)\n",
                  lpclass));

    // add the app atom to global list
    if (!ValidateSrvrClass (lpclass, &aExe))
    {
        intrDebugOut((DEB_IWARN,
                      "CDDEServer::Create(%ws) Invalid Class\n",
                      lpclass));

        return OLE_E_CLSID;
    }

    lpDDEsrvr =  new CDDEServer;
    RetZS (lpDDEsrvr, E_OUTOFMEMORY);

    // set the signature handle and the app atom.
    lpDDEsrvr->m_chk         = chkDdeSrvr;
    lpDDEsrvr->m_aClass      = wGlobalAddAtom (lpclass);
    lpDDEsrvr->m_clsid	     = rclsid; // Class ID (already TreatAs'd)
    lpDDEsrvr->m_aOriginalClass = wDupAtom (aOriginalClass);
    lpDDEsrvr->m_pClassFactory	= NULL;
    lpDDEsrvr->m_dwClassFactoryKey = lpDdeInfo->dwRegistrationKey;
    lpDDEsrvr->m_aExe        = aExe;
    lpDDEsrvr->m_cnvtyp      = cnvtyp;
    lpDDEsrvr->m_fcfFlags    = lpDdeInfo->dwFlags;

    lpDDEsrvr->m_bTerminate  = FALSE;        // Set if we are terminating.
    lpDDEsrvr->m_hcli        = NULL;         // handle to the first block of clients list
    lpDDEsrvr->m_termNo      = 0;            // termination count
    lpDDEsrvr->m_cSrvrClients= 0;            // no of clients;
    lpDDEsrvr->m_fDoNotDestroyWindow= 0;





#ifdef   FIREWALLS
    AssertSz(lpDdeInfo.dwFlags <= REGCLS_MULTI_SEPARATE, "invalid server options");
#endif

    // Create the server window and do not show it.
    //
    // We are explicitly calling CreateWindowA here.
    // The DDE tracking layer will attempt to convert hCommands to UNICODE
    // if the two windows in the conversation are both UNICODE.
    // This window is created as a child of the common server window for this
    // thread. When this thread dies, the common server window is destroyed if
    // it exists, which will cause all of the child windows to be destroyed also.
    //
    //
    if (!(lpDDEsrvr->m_hwnd = DdeCreateWindowEx (0, gOleWindowClass,
                                            szCDDEServer,
                                            WS_OVERLAPPED | WS_CHILD,
                                            0,0,0,0,
					    (HWND)TLSGetDdeServer(),
					    NULL,
					    g_hinst, NULL)))
    {
        goto errReturn;
    }

    // fix up the WindowProc entry point.
    SetWindowLong(lpDDEsrvr->m_hwnd, GWL_WNDPROC, (LONG)SrvrWndProc);

    //
    // The following will inform the class object in the class registration table
    // that this window should be notified when the class object is revoked. This
    // enables the window to shutdown properly.
    //
    // If there isn't a class factory, which happens for single instance servers
    // which were launched with a filename, then m_dwClassFactory will be 0,
    // in which case we don't make the set call.
    //
    if(lpDDEsrvr->m_dwClassFactoryKey != 0)
    {
        if(!SetDdeServerWindow(lpDDEsrvr->m_dwClassFactoryKey,lpDDEsrvr->m_hwnd))
        {
            intrDebugOut((DEB_IERROR,
                          "0 CDDEServer::Create unable to SetDdeServerWindow\n"));
            goto errReturn;
        }
    }

    intrDebugOut((DEB_DDE_INIT,
                  "DDE Server window for %ws created in task %x\n",
                  lpclass,GetCurrentThreadId()));

    // save the ptr to the server struct in the window.
    SetWindowLong (lpDDEsrvr->m_hwnd, 0, (LONG)lpDDEsrvr);

    // Set the signature.
    SetWindowWord (lpDDEsrvr->m_hwnd, WW_LE, WC_LE);

    *phwnd = lpDDEsrvr->m_hwnd;


    intrDebugOut((DEB_DDE_INIT,
                  "0 _OUT CDDEServer::Create returns %x\n",
                  NOERROR));
    return NOERROR;

errReturn:
    AssertSz (0, "CDDEServer::Create errReturn");
    if (lpDDEsrvr)
    {
        if (lpDDEsrvr->m_hwnd)
            SSDestroyWindow (lpDDEsrvr->m_hwnd);

        if (lpDDEsrvr->m_aClass)
            GlobalDeleteAtom (lpDDEsrvr->m_aClass);

        if (lpDDEsrvr->m_aExe)
            GlobalDeleteAtom (lpDDEsrvr->m_aExe);
        delete lpDDEsrvr;
    }

    intrDebugOut((DEB_IERROR,
                  "0 _OUT CDDEServer::Create returns %x\n",
                  E_OUTOFMEMORY));

    return E_OUTOFMEMORY;
}



// ValidateSrvrClass checks whether the given server class is valid by
// looking in the registration database.

INTERNAL_(BOOL)    ValidateSrvrClass (
LPOLESTR       lpclass,
ATOM FAR *  lpAtom
)
{
    WCHAR    buf[MAX_STR];
    LONG    cb = MAX_STR;
    WCHAR    key[MAX_STR];
    LPOLESTR   lptmp;
    LPOLESTR   lpbuf;
    WCHAR    ch;
    CLSID    clsid;

    if (CLSIDFromProgID (lpclass, &clsid) != NOERROR)
    {
        // ProgId is not correctly registered in reg db
        return FALSE;
    }

    lstrcpyW (key, lpclass);
    lstrcatW (key, OLESTR("\\protocol\\StdFileEditing\\server"));

    if (RegQueryValue (HKEY_CLASSES_ROOT, key, buf, &cb))
        return TRUE;

    if (!buf[0])
    {
        AssertSz (0, "ValidateSrvrClass failed.");
        return FALSE;
    }

    // Get exe name without path and then get an atom for that
    lptmp = lpbuf = buf;
    while (ch = *lptmp)
    {
        lptmp++;
        if (ch == '\\' || ch == ':')
            lpbuf = lptmp;
    }
    *lpAtom =  wGlobalAddAtom (lpbuf);

    return TRUE;
}



INTERNAL RemDdeRevokeClassFactory
    (LPSRVR lpsrvr)
{
    HRESULT hr;
    intrDebugOut((DEB_ITRACE,
                  "0 _IN RemDdeRevokeClassFactory(%x)\n",
                  lpsrvr));

    ChkS(lpsrvr);
    hr = lpsrvr->Revoke();
    intrDebugOut((DEB_ITRACE,
                  "0 OUT RemDdeRevokeClassFactory(%x) %x\n",
                  lpsrvr,hr));
    return(hr);
}



INTERNAL CDDEServer::Revoke ()
{
    intrDebugOut((DEB_ITRACE,
                  "%x _IN CDDEServer::Revoke() m_cSrvrClients=%x\n",
                  this,
                  m_cSrvrClients));
    HRESULT hr;

    ChkS(this);

    //
    // Can't revoke if there are still clients. QueryRevokeCLassFactory
    // determines if there are still clients attached.
    //
    if (!QueryRevokeClassFactory ())
    {
        intrDebugOut((DEB_IERROR,
                      "QueryRevokeClassFactory failed!"));
        hr = RPC_E_DDE_REVOKE;
        goto exitRtn;
    }

    if (m_cSrvrClients)
    {
        m_bTerminate = TRUE;
        // if there are any clients connected to this classfactory,
        // send terminates.
        SendServerTerminateMsg ();
        m_bTerminate = FALSE;
    }

    hr = FreeSrvrMem ();

exitRtn:
    intrDebugOut((DEB_ITRACE,
                  "%x OUT CDDEServer::Revoke(%x) hr = %x\n",
                  this, hr));
    return hr;
}

INTERNAL_(void)  CDDEServer::SendServerTerminateMsg ()
{

    HANDLE          hcliPrev = NULL;
    PCLILIST        pcli;
    HANDLE          *phandle;
    HANDLE          hcli;

    intrDebugOut((DEB_ITRACE,
                  "%x _IN CDDEServer::SendServerTerminateMsg\n",
                  this));

    hcli = m_hcli;
    while (hcli) {
        if ((pcli = (PCLILIST) LocalLock (hcli)) == NULL)
        {
            Assert(0);
            goto exitRtn;
        }

        phandle = (HANDLE *) (pcli->info);
        while (phandle < (HANDLE *)(pcli + 1)) {
            if (*phandle)
            {
                PostMessageToClientWithReply ((HWND)(*phandle), WM_DDE_TERMINATE,
                    (WPARAM) m_hwnd, NULL, WM_DDE_TERMINATE);
                Assert (m_cSrvrClients);
                m_cSrvrClients--;
            }
            phandle++;
            phandle++;
        }

        hcliPrev = hcli;
        hcli = pcli->hcliNext;
        LocalUnlock (hcliPrev);
    }

exitRtn:
    intrDebugOut((DEB_ITRACE,
                  "%x OUT CDDEServer::SendServerTerminateMsg\n",
                  this));

}

//+---------------------------------------------------------------------------
//
//  Method:     CDDEServer::FreeSrvrMem
//
//  Synopsis:   Free's up a CDDEServer.
//
//  Effects:
//
//  Arguments:  [void] --
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    6-26-94   kevinro   Commented
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL CDDEServer::FreeSrvrMem
    (void)
{
    HRESULT hr;
    // REVIEW: Not clear how this works in the synchronous mode
    // Release for class factory is called only when everything is
    // cleaned and srvr app can post WM_QUIT at this stage

    intrDebugOut((DEB_ITRACE,
                  "%x _IN CDDEServer::FreeSrvrMem\n",
                  this));

    PCLILIST pcliPrev;
    HANDLE hcli, hcliPrev;

    if (m_bTerminate)
    {
        AssertSz (0, "terminate flag is not FALSE");
    }


    if (m_aExe)
    {
        GlobalDeleteAtom (m_aExe);
    }


    // We deliberately do not call this->Lock (FALSE)
    // If the server has revoked his class object without
    // waiting for his Lock count to go to zero, then
    // presumably he doesn't need us to unlock him.  In fact,
    // doing such an unlock might confuse a server who then
    // tries to call CoRevokeClassObject recursively.
    if (m_pClassFactory)
    {
        m_pClassFactory->Release();
        m_pClassFactory = NULL;
    }

    hcli = m_hcli;
    while (hcli)
    {
        hcliPrev = hcli;
        if (pcliPrev = (PCLILIST) LocalLock (hcliPrev))
        {
            hcli = pcliPrev->hcliNext;
        }
        else
        {
            AssertSz (0, "Corrupt internal data structure or out-of-memory");
            hcli = NULL;
        }
        Verify (0==LocalUnlock (hcliPrev));
        Verify (NULL==LocalFree (hcliPrev));
    }

    hr = DestroyDdeSrvrWindow(m_hwnd,m_aClass);
    if (hr != NOERROR)
    {
        //
        // Well now, if DestroyWindow fails, there isn't a whole heck of
        // alot we can do about it. It could mean that the window was
        // destroyed previously, or the parent window was destroyed during
	// thread shutdown. We should still continue to cleanup
        //
        intrDebugOut((DEB_IERROR,
                      "%x CDDEServer::FreeSrvrMem DestroyDdeSrvrWindow failed %x\n",
                      this,
                      hr));
    }

    if (m_aClass)
    {
        GlobalDeleteAtom (m_aClass);
    }

    delete this;

    intrDebugOut((DEB_ITRACE,
                  "%x _OUT CDDEServer::FreeSrvrMem\n",
                  this));
    return NOERROR;
}

//+---------------------------------------------------------------------------
//
//  Function:   SrvrHandleIncomingCall
//
//  Synopsis:   Setup and call the CallControl to dispatch a call to the server
//
//  Effects:    A call has been made from the client that requires us to call
//              into our server. This must be routed through the call control.
//              This routine sets up the appropriate data structures, and
//              calls into the CallControl. The CallControl will in turn
//              call SrvrDispatchIncomingCall to actuall process the call.
//
//              This routine should only be called by the SrvrWndProc
//
//
//  Arguments:  [lpsrvr] -- Points to the server
//              [hwnd] -- hwnd of server
//              [hdata] -- Handle to data
//              [wParam] -- hwnd of client
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-05-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL SrvrHandleIncomingCall(LPSRVR lpsrvr,
                                HWND hwnd,
                                HANDLE hdata,
                                HWND wParam)
{
    VDATEHEAP();
    HRESULT hresult = NOERROR;
    SRVRDISPATCHDATA srvrdispdata;
    DISPATCHDATA     dispatchdata;

    intrDebugOut((DEB_ITRACE,
                  "0 _IN SrvrHandleIncomingCall lpsrvr=%x hwnd=%x hdata=%x wParam=%x\n",
                  lpsrvr,
                  hwnd,
                  hdata,
                  wParam));

    srvrdispdata.wDispFunc = DDE_DISP_SRVRWNDPROC;
    srvrdispdata.hwnd = hwnd;
    srvrdispdata.hData = hdata;
    srvrdispdata.wParam = wParam;
    srvrdispdata.lpsrvr = lpsrvr;

    dispatchdata.pData = &srvrdispdata;

    RPCOLEMESSAGE	 rpcMsg;
    RPC_SERVER_INTERFACE RpcInterfaceInfo;
    DWORD		 dwFault;

    rpcMsg.iMethod = 0;
    rpcMsg.Buffer  = &dispatchdata;
    rpcMsg.cbBuffer = sizeof(dispatchdata);
    rpcMsg.reserved2[1] = &RpcInterfaceInfo;
    *MSG_TO_IIDPTR(&rpcMsg) = GUID_NULL;


    IRpcStubBuffer * pStub = &(lpsrvr->m_pCallMgr);
    IRpcChannelBuffer * pChannel = &(lpsrvr->m_pCallMgr);
    hresult = STAInvoke(&rpcMsg, CALLCAT_SYNCHRONOUS, pStub, pChannel, NULL, &dwFault);

    intrDebugOut((DEB_ITRACE,
                  "0 _OUT SrvrHandleIncomingCall hresult=%x\n",
                  hresult));

    return(hresult);
}

//+---------------------------------------------------------------------------
//
//  Function:   SrvrDispatchIncomingCall
//
//  Synopsis:   Dispatch a call into the server.
//
//  Effects:    At the moment, the only incoming call that requires handling
//              by the server window is Execute. This routine dispatchs to it,
//              and returns.
//
//  Arguments:  [psdd] --
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-05-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL SrvrDispatchIncomingCall(PSRVRDISPATCHDATA psdd)
{
    VDATEHEAP();
    HRESULT hr;
    intrDebugOut((DEB_ITRACE,
                  "0 _IN SrvrDispatchIncomingCall psdd(%x)\n",psdd));

    hr = psdd->lpsrvr->SrvrExecute (psdd->hwnd,
                                    psdd->hData,
                             (HWND)(psdd->wParam));

    intrDebugOut((DEB_ITRACE,
                  "0 _OUT SrvrDispatchIncomingCall psdd(%x) hr =%x\n",
                  psdd,
                  hr));

    return(hr);
}


// REVIEW: Revoking Class Factory will not be successful if
//         any clients are either connected to the classfactory
//         or to the object instances.



//+---------------------------------------------------------------------------
//
//  Function:   SrvrWndProc
//
//  Synopsis:   This is the server window procedure.
//
//  Effects:
//
//  Arguments:  [hwndIn] -- Window handle (may not be full. See note)
//              [msg] --
//              [wParam] --
//              [lParam] --
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    8-03-94   kevinro   Created
//
//  Notes:
//
//  When running in a VDM, it is possible that this window was dispatched
//  without having a full window handle. This happens when the getmessage
//  was dispatched from 16-bit. Therefore, we need to convert the hwnd to
//  a full hwnd before doing any comparision functions.
//
//----------------------------------------------------------------------------
STDAPI_(LRESULT) SrvrWndProc (
HWND            hwndIn,
UINT		msg,
WPARAM          wParam,
LPARAM          lParam
)
{
    BOOL	fRevoke=FALSE;
    LPSRVR      lpsrvr;
    WORD        status = NULL;
    HANDLE      hdata;
    ATOM        aItem;
    HRESULT   retval;

    //
    // The following hwnd variable is used to determine the full HWND, in the
    // event we were dispatched in a 16 bit process.
    //
    HWND        hwnd;

#ifdef  FIREWALLS
    HWND        hwndClient;
#endif


    switch (msg){

       case WM_DDE_INITIATE:
            VDATEHEAP();
#ifdef  FIREWALLS
    AssertSz (lpsrvr, "No server window handle in server window");
#endif
            hwnd = ConvertToFullHWND(hwndIn);

            lpsrvr = (LPSRVR)GetWindowLong (hwnd, 0);
            if (lpsrvr->m_bTerminate){
                // we are terminating, no more connections
                break;
            }

            // class is not matching, so it is not definitely for us.
            // for apps sending the EXE for initiate, do not allow if the app
            // is mutiple instance (Bug fix for winworks).

            if (!(lpsrvr->m_aClass == (ATOM)(LOWORD(lParam)) ||
                 (NOERROR==wCompatibleClasses (LOWORD(lParam), lpsrvr->m_aClass)) ||
                 (lpsrvr->m_aExe == (ATOM)(LOWORD(lParam)) && IsSingleServerInstance() )))
            {
                break;
            }

            intrDebugOut((DEB_DDE_INIT,"::SrvrWndProc INITIATE\n"));


            if (!lpsrvr->HandleInitMsg (lParam))
            {
                if (!(aSysTopic == (ATOM)(HIWORD(lParam))))
                {
                    //
                    // If this isn't a sys topic, then it must be a request for
                    // a specific document. Send a message to the
                    // children windows, asking for the document. If one of them
                    // may send an ACK to the client.
                    //

                    // if the server window is not the right window for
                    // DDE conversation, then try with the doc windows.
                    BOOL fAckSent = SendInitMsgToChildren (hwnd, msg, wParam, lParam);

#ifdef KEVINRO_OLDCODE
 The following code was removed, because I don't belive it is required
 any longer. I am not 100% sure yet, so I have left it in. If you find it,
 you can probably remove it.
 It appears to be trying to claim the SINGLE_USE class factory from the class
 factory table. It does this when a child document window sends an ACK to the
 client, claiming to support the document being asked for. It really doesn't
 make too much sense here, since a single use server would have already removed
 its class factory if there was an open document.

 Anyway, the 16-bit version had a direct hack into the class factory table. We
 don't have that anymore, so this code wouldn't work anyway.

                    if (lpsrvr->m_fcfFlags==REGCLS_SINGLEUSE)
                    {
                        if (lpsrvr->m_pfAvail)
                        {
                            // Hide the entry in the class factory table so that no 2.0
                            // client can connect to the same server.
                            Assert (!IsBadWritePtr (lpsrvr->m_pfAvail, sizeof(BOOL)));
                            *(lpsrvr->m_pfAvail) = FALSE;
                        }
                    }
#endif // KEVINRO_OLDCODE
                    intrDebugOut((DEB_DDE_INIT,"SrvrWndProc Child Init\n"));
                    return fAckSent;
                }
                break;
            }

            // We can enterain this client. Put him in our client list
            // and acknowledge the initiate.

            if (!AddClient ((LPHANDLE)&lpsrvr->m_hcli, (HWND)wParam,(HWND)/*fLocked*/FALSE))
            {
                break;
            }

            //
            // Now its time to grab up the class factory from the class
            // factory table. When this window was created, the class factory
            // was available. However, it is possible that it has already
            // been claimed by someone else. So, we try grabbing it (which
            // normally should succeed). If it fails, then delete the client
            // and don't acknowledge.
            //

            if (lpsrvr->m_pClassFactory == NULL)
            {
                DdeClassInfo ddeInfo;
                ddeInfo.dwContextMask = CLSCTX_LOCAL_SERVER |
                                        CLSCTX_INPROC_SERVER;
                intrDebugOut((DEB_DDE_INIT,"SrvrWndProc getting class factory\n"));
                //
                // The following asks for control of the class
                // factory in the case of a single use class
                //
                ddeInfo.fClaimFactory = TRUE;
                ddeInfo.dwRegistrationKey = lpsrvr->m_dwClassFactoryKey;

                if (GetClassInformationFromKey(&ddeInfo) == FALSE)
                {
                    intrDebugOut((DEB_IERROR,"SrvrWndProc failed to get class factory\n"));
                    //
                    // Whoops, we were not able to grab the class factory
                    // Cleanup and hop out
                    if (!FindClient ((LPHANDLE)lpsrvr->m_hcli,(HWND)wParam, TRUE))
                    {
                        intrAssert(!"FindClient failed\n");
                    }
                    return(0);
                }
                lpsrvr->m_pClassFactory = (IClassFactory *)ddeInfo.punk;
                lpsrvr->m_fcfFlags = ddeInfo.dwFlags;
            }

            intrAssert(lpsrvr->m_pClassFactory != NULL);

            lpsrvr->m_cSrvrClients++;

            lpsrvr->Lock (TRUE, (HWND)wParam);

            // Post acknowledge
            DuplicateAtom (LOWORD(lParam));
            DuplicateAtom (HIWORD(lParam));
            SSSendMessage ((HWND)wParam, WM_DDE_ACK, (WPARAM)hwnd, lParam);


            return 1L; // fAckSent==TRUE
            VDATEHEAP();
            break;


    case WM_DDE_EXECUTE:
            VDATEHEAP();
            hwnd = ConvertToFullHWND(hwndIn);

            lpsrvr = (LPSRVR)GetWindowLong (hwnd, 0);

            hdata = GET_WM_DDE_EXECUTE_HDATA(wParam,lParam);

#ifdef  FIREWALLS
            AssertSz (lpsrvr, "No server  handle in server window");
#endif

            intrDebugOut((DEB_ITRACE,"SrvrWndProc WM_DDE_EXECUTE\n"));

#ifdef  FIREWALLS
            // find the client in the client list.
            hwndClient = FindClient (lpsrvr->m_hcli, (HWND)wParam, FALSE);
            AssertSz (hwndClient, "Client is missing from the server")
#endif
            // Are we terminating
            if (lpsrvr->m_bTerminate) {
                intrDebugOut((DEB_ITRACE,
                              "SrvrWndProc WM_DDE_EXECUTE ignored for TERMINATE\n"));
                // !!! are we supposed to free the data
                GlobalFree (hdata);
                break;
            }

            retval = SrvrHandleIncomingCall(lpsrvr,hwnd,hdata,(HWND)wParam);

            if (NOERROR!=retval)
            {
                intrDebugOut((DEB_IERROR,
                              "SrvrWndProc SrvrHandleIncomingCall fail %x\n",
                              retval));
            }
            SET_MSG_STATUS (retval, status)

            if (!lpsrvr->m_bTerminate)
            {
                // REVIEW: We are making an assumption that, we will not be posting
                // any DDE messages because of calling the SrvrExecute.
                // If we post any messages, before we post the acknowledge
                // we will be in trouble.

                lParam = MAKE_DDE_LPARAM(WM_DDE_ACK,status,(UINT) hdata);

                intrDebugOut((DEB_ITRACE,
                              "SrvrWndProc WM_DDE_EXECUTE sending %x for ack\n",status));

                // Post the acknowledge to the client
                if (!PostMessageToClient ((HWND) wParam,
                                          WM_DDE_ACK, (UINT) hwnd, lParam)) {
                    // if the window died or post failed, delete the atom.
                    GlobalFree (hdata);
                    DDEFREE(WM_DDE_ACK,lParam);
                }
            }
            VDATEHEAP();
            break;



    case WM_DDE_TERMINATE:
            intrDebugOut((DEB_ITRACE,
                          "SrvrWndProc WM_DDE_TERMINATE\n"));

            hwnd = ConvertToFullHWND(hwndIn);

            lpsrvr = (LPSRVR)GetWindowLong (hwnd, 0);

#ifdef  FIREWALLS
            // find the client in the client list.
            hwndClient = FindClient (lpsrvr->m_hcli, (HWND)wParam, FALSE);
            AssertSz (hwndClient, "Client is missing from the server")
#endif
            Putsi (lpsrvr->m_bTerminate);
            if (lpsrvr->m_bTerminate)
            {
                AssertSz (0, "Unexpected code path");
            }
            else
            {
                // If client initiated the terminate. post matching terminate
                PostMessageToClient ((HWND)wParam,
                                      WM_DDE_TERMINATE,
                                      (UINT) hwnd,
                                      NULL);
                --lpsrvr->m_cSrvrClients;
                if (0==lpsrvr->m_cSrvrClients
                    && lpsrvr->QueryRevokeClassFactory())
                {
#ifdef KEVINRO_OLD_CODE
                    if (lpsrvr->m_phwndDde)
                    {
                        // Remove from class factory table
                        *(lpsrvr->m_phwndDde) = (HWND)0;
                    }
#endif // KEVINRO_OLD_CODE
                    fRevoke = TRUE;
                }

                lpsrvr->Lock (FALSE, (HWND)wParam); // Unlock server
                FindClient (lpsrvr->m_hcli, (HWND)wParam, /*fDelete*/TRUE);

                if (fRevoke)
                {
                    lpsrvr->Revoke();
                }
            }
            break;


       case WM_DDE_REQUEST:
            aItem = GET_WM_DDE_REQUEST_ITEM(wParam,lParam);

            hwnd = ConvertToFullHWND(hwndIn);

            lpsrvr = (LPSRVR)GetWindowLong (hwnd, 0);

            intrDebugOut((DEB_ITRACE,
                          "SrvrWndProc WM_DDE_REQUEST(aItem=%x)\n",aItem));

            if (lpsrvr->m_bTerminate || !IsWindowValid ((HWND) wParam))
            {
                goto RequestErr;
            }

            if(RequestDataStd (aItem, (HANDLE FAR *)&hdata) != NOERROR)
            {

                lParam = MAKE_DDE_LPARAM(WM_DDE_ACK,0x8000,aItem);

                // if request failed, then acknowledge with error.
                if (!PostMessageToClient ((HWND)wParam, WM_DDE_ACK,
                                                        (UINT) hwnd, lParam))
                {
                    DDEFREE(WM_DDE_ACK,lParam);
RequestErr:
                    if (aItem)
                    {
                        GlobalDeleteAtom (aItem);
                    }

                }
            }
            else
            {
                lParam = MAKE_DDE_LPARAM(WM_DDE_REQUEST,
                                                    (UINT) hdata,(UINT) aItem);

                // post the data message and we are not asking for any
                // acknowledge.

                if (!PostMessageToClient ((HWND)wParam, WM_DDE_DATA,
                                                        (UINT) hwnd, lParam))
                {
                    GlobalFree (hdata);
                    DDEFREE(WM_DDE_REQUEST,lParam);
                    goto RequestErr;
                }
            }
            break;

    case WM_DONOTDESTROY:
            intrDebugOut((DEB_ITRACE,
                          "SrvrWndProc WM_DONOTDESTROY %x\n",
                          wParam));

            //
            // This message is only sent by 32-bit code that has been
            // given our full handle
            //

            lpsrvr = (LPSRVR)GetWindowLong (hwndIn, 0);

            //
            // The WM_DONOTDESTROY message tells the server how to
            // handle the following WM_USER message. If wParam is set,
            // then the m_fDoNotDestroyWindow flag will be set, which
            // keeps us from destroying the server window. If cleared,
            // it will enable the destruction. This message is sent
            // from the MaybeCreateDocWindow routine
            //

            lpsrvr->m_fDoNotDestroyWindow = wParam;
            return 0;
            break;

    case WM_USER:
            intrDebugOut((DEB_ITRACE,
                          "SrvrWndProc WM_USER\n"));
            //
            // This message is only sent by 32-bit code that has been
            // given our full handle
            //

            lpsrvr = (LPSRVR)GetWindowLong (hwndIn, 0);

            // cftable.cpp sends a WM_USER message to destory the DDE
            // server window when a 2.0 client has connected to a
            // SDI 2.0 server (and no 1.0 client should be allowed to also
            // connect.
            // cftable.cpp cannot call RemDdeRevokeClassFactory directly
            // becuase they may be in different processes.
            //
            // The m_fDoNotDestroyWindow flag is used by
            // MaybeCreateDocWindow in the case that the server is a
            // single use server, and revokes its class factory when
            // an object is created. MaybeCreateDocWindow will set this
            // flag, telling us to ignore the message.
            //
            // returning 0 means we did destroy, 1 means we did not.

            if (!lpsrvr->m_fDoNotDestroyWindow)
            {
                RemDdeRevokeClassFactory(lpsrvr);
                return(0);
            }
            return 1;
            break;

    default:
            return SSDefWindowProc (hwndIn, msg, wParam, lParam);
    }

    return 0L;
}

//+---------------------------------------------------------------------------
//
//  Method:     CDDEServer::HandleInitMsg
//
//  Synopsis:   Determine if we are going to handle the INITIATE message.
//
//  Effects:
//
//  Arguments:  [lParam] --
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    5-28-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL_(BOOL) CDDEServer::HandleInitMsg(LPARAM    lParam)
{

    // If it is not system or Ole, this is not the server.
    if (!((aSysTopic == (ATOM)(HIWORD(lParam))) || (aOLE == (ATOM)(HIWORD(lParam)))))
    {
        return FALSE;
    }
    Assert (m_fcfFlags<=REGCLS_MULTI_SEPARATE);

    // single instance MDI accept
    if (m_fcfFlags != REGCLS_SINGLEUSE)
    {
        return TRUE;
    }

    // this server is multiple instance. So, check for any clients or docs.
    if (!GetWindow (m_hwnd, GW_CHILD) && 0==m_cSrvrClients)
        return TRUE;

    return FALSE;
}



// AddClient: Adds  a  client entry to the list.
// Each client entry is a pair of handles; key handle
// and data handle.  Ecah list entry contains space for
// MAX_LIST of pairs of handles.

INTERNAL_(BOOL)   AddClient
(
LPHANDLE    lphead,         // ptr to loc which contains the head handle
HANDLE      hkey,           // key
HANDLE      hdata           // hdata
)
{

    HANDLE          hcli = NULL;
    HANDLE          hcliPrev = NULL;
    PCLILIST        pcli;
    HANDLE          *phandle;


    hcli = *lphead;

    // if the entry is already present, return error.
    if (hcli && FindClient (hcli, hkey, FALSE))
        return FALSE;

    while (hcli) {
        if (hcliPrev)
            LocalUnlock (hcliPrev);

        if ((pcli = (PCLILIST) LocalLock (hcli)) == NULL)
            return FALSE;

        phandle = (HANDLE *) pcli->info;
        while (phandle < (HANDLE *)(pcli + 1)) {
            if (*phandle == NULL) {
                *phandle++ = hkey;
                *phandle++ = hdata;
                LocalUnlock (hcli);
                return TRUE;
            }
            phandle++;
            phandle++;
        }
        hcliPrev = hcli;
        hcli = pcli->hcliNext;
        lphead = (LPHANDLE)&pcli->hcliNext;
    }

    // not in the list.
    hcli = LocalAlloc (LMEM_MOVEABLE | LMEM_ZEROINIT, sizeof (CLILIST));
    if (hcli == NULL)
        goto  errRtn;

    if ((pcli = (PCLILIST) LocalLock (hcli)) == NULL)
        goto errRtn;

    // set the link to this handle in the previous entry
    *lphead = hcli;
    if (hcliPrev)
        LocalUnlock (hcliPrev);

    phandle = (HANDLE *) pcli->info;
    *phandle++ = hkey;
    *phandle++ = hdata;
    LocalUnlock (hcli);
    return TRUE;

errRtn:

    if (hcliPrev)
        LocalUnlock (hcliPrev);

    if (hcli)
        LocalFree (hcli);

    return FALSE;

}


// FindClient: finds a client and deletes the client if necessary.
INTERNAL_(HANDLE) FindClient
(
HANDLE      hcli,
HANDLE      hkey,
BOOL        bDelete
)
{
    HANDLE        hcliPrev = NULL;
    PCLILIST      pcli;
    HANDLE        *phandle;
    HANDLE        hdata;

    while (hcli) {
        if ((pcli = (PCLILIST) LocalLock (hcli)) == NULL)
            return FALSE;

        phandle = (HANDLE *) pcli->info;
        while (phandle < (HANDLE *)(pcli + 1)) {
            if (*phandle == hkey) {
                if (bDelete)
                    *phandle = NULL;

                hdata = *++phandle;
                LocalUnlock (hcli);
                return hdata;
            }
            phandle++;
            phandle++;
        }
        hcliPrev = hcli;
        hcli = pcli->hcliNext;
        LocalUnlock (hcliPrev);

    }
    return NULL;
}


//+---------------------------------------------------------------------------
//
//  Method:     CDDEServer::SrvrExecute
//
//  Synopsis:   takes care of the WM_DDE_EXECUTE for the server.
//
//  Effects:    Parses the EXECUTE string, and determines what it should be
//              done.
//
//  Arguments:  [hwnd] --   Server window
//              [hdata] --  Handle to EXECUTE string
//              [hwndClient] -- Client window
//
//  Requires:
//      hdata is an ANSI string. It was passed to us by a DDE client.
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    6-05-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL CDDEServer::SrvrExecute
(
HWND        hwnd,
HANDLE      hdata,
HWND        hwndClient
)
{
    intrDebugOut((DEB_ITRACE,
                  "%x _IN CDDESrvr::SrvrExecute(hwnd=%x,hdata=%x,hwndClient=%x)\n",
                  this,
                  hwnd,
                  hdata,
                  hwndClient));

    ATOM            aCmd;
    BOOL            fActivate;

    LPSTR           lpdata = NULL;
    HANDLE          hdup   = NULL;
    HRESULT         hresult = E_UNEXPECTED;

    LPSTR           lpdocname;
    LPSTR           lptemplate;
    LPCLIENT        lpdocClient = NULL;
    LPSTR           lpnextarg;
    LPSTR           lpclassname;
    LPSTR           lpitemname;
    LPSTR           lpopt;
    CLSID           clsid;
    WORD            wCmdType;
    BOOL            bCreateInst = FALSE;
        LPUNKNOWN               pUnk = NULL;

    LPPERSISTSTORAGE pPersistStg=NULL;

    // REVIEW: if any methods called on the objects genarate DDE messages
    // before we return from Execute, we will be in trouble.


    // REVIEW: this code can be lot simplified if we do the argument scanning
    // seperately and return the ptrs to the args. Rewrite later on.

    ErrZS (hdup = UtDupGlobal (hdata,GMEM_MOVEABLE), E_OUTOFMEMORY);

    ErrZS (lpdata  = (LPSTR)GlobalLock (hdup), E_OUTOFMEMORY);

    intrDebugOut((DEB_ITRACE,
                  "CDDESrvr::SrvrExecute(lpdata = %s)\n",lpdata));

    if (*lpdata++ != '[') // commands start with the left sqaure bracket
    {
        hresult = ResultFromScode (RPC_E_DDE_SYNTAX_EXECUTE);
        goto  errRtn;
    }

    hresult = ReportResult(0, RPC_E_DDE_SYNTAX_EXECUTE, 0, 0);
    // scan upto the first arg
    if (!(wCmdType = ScanCommand (lpdata, WT_SRVR, &lpdocname, &aCmd)))
        goto  errRtn;

    if (wCmdType == NON_OLE_COMMAND)
    {
        if (!UtilQueryProtocol (m_aClass, PROTOCOL_EXECUTE))
            hresult = ReportResult(0, RPC_E_DDE_PROTOCOL, 0, 0);
        else {
            // REVIEW: StdExecute has to be mapped on to the StdCommandProtocol
            // What command do we map on to?

            AssertSz (0, "StdExecute is being called for server");
        }

        goto errRtn1;
    }

    if (aCmd == aStdExit)
    {
        if (*lpdocname)
            goto errRtn1;

        hresult = NOERROR;
        // REVIEW: Do we have to initiate any terminations from the
        // the servr side? Check how this works with excel.
        goto end2;
    }

    // scan the next argument.
    if (!(lpnextarg = ScanArg(lpdocname)))
        goto errRtn;

    //////////////////////////////////////////////////////////////////////////
    //
    // [StdShowItem("docname", "itemname"[, "true"])]
    //
    //////////////////////////////////////////////////////////////////////////

    if (aCmd == aStdShowItem) {

        // first find the documnet. If the doc does not exist, then
        // blow it off.

        if (!(lpdocClient = FindDocObj (lpdocname)))
            goto errRtn1;

        lpitemname = lpnextarg;

        if( !(lpopt = ScanArg(lpitemname)))
            goto errRtn1;

        // scan for the optional parameter
        // Optional can be only TRUE or FALSE.

        fActivate = FALSE;
        if (*lpopt) {

            if( !(lpnextarg = ScanBoolArg (lpopt, (BOOL FAR *)&fActivate)))
                goto errRtn1;

            if (*lpnextarg)
                goto errRtn1;

        }


        // scan it. But, igonre the arg.
        hresult = lpdocClient->DocShowItem (lpitemname, !fActivate);
        goto end2;



    }

    //////////////////////////////////////////////////////////////////////////
    //
    // [StdCloseDocument ("docname")]
    //
    //////////////////////////////////////////////////////////////////////////

    if (aCmd == aStdClose) {
        if (!(lpdocClient = FindDocObj (lpdocname)))
            goto errRtn1;

        if (*lpnextarg)
            goto errRtn1;

        // REVIEW: Do we have to do anything for shutting down the
        // the app? Is the client going to initiate the terminate?.
        // if we need to initiate the terminates, make sure we post
        // the ACK  first.

        lpdocClient->Revoke();
        goto end2;
    }


    if (aCmd == aStdOpen)
    {
        // find if any doc level object is already registerd.
        // if the object is registerd, then no need to call srvr app.
        if (FindDocObj (lpdocname))
        {
            // A client has already opened the document or user opened the
            // doc. We should do an addref to the docobj

#ifdef TRY
            if (m_cSrvrClients == 0)
                // Why are we doing this?
                hresult = lpdocClient->m_lpoleObj->AddRef();
            else
#endif
                hresult = NOERROR;
            goto end1;
        }
    }

    if (aCmd == aStdCreate || aCmd == aStdCreateFromTemplate) {
        lpclassname = lpdocname;
        lpdocname   = lpnextarg;
        if( !(lpnextarg = ScanArg(lpdocname)))
            goto errRtn1;

    }

    // check whether we can create/open more than one doc.

    if ((m_fcfFlags == REGCLS_SINGLEUSE) &&
            GetWindow (m_hwnd, GW_CHILD))
            goto errRtn;


    ErrZ (CLSIDFromAtom(m_aClass, &clsid));


    //
    // Generate a wide version of the name
    //

    WCHAR       awcWideDocName[MAX_STR];

    if (MultiByteToWideChar(CP_ACP,0,lpdocname,-1,awcWideDocName,MAX_STR) == FALSE)
    {
        Assert(!"Unable to convert characters");
        hresult = E_UNEXPECTED;
        goto errRtn;
    }

    //////////////////////////////////////////////////////////////////////////
    //
    // [StdOpenDocument ("docname")]
    //
    //////////////////////////////////////////////////////////////////////////

    // Document does not exist.
    if (aCmd == aStdOpen)
    {
        ErrRtnH (wClassesMatch (clsid, awcWideDocName));
        ErrRtnH (wFileBind (awcWideDocName, &pUnk));
    }


    ErrRtnH (CreateInstance (clsid, awcWideDocName, lpdocname, pUnk, &lpdocClient, hwndClient));
    bCreateInst = TRUE;

    if (aCmd == aStdOpen)
    {
        // Temporary flag to indicate someone will INITIATE on this doc.
        // The flag is reset after the INITITATE.
        // This is Yet-Another-Excel-Hack.  See ::QueryRevokeClassFactory
        lpdocClient->m_fCreatedNotConnected = TRUE;
    }
    else
    {
        lpdocClient->m_fEmbed = TRUE;
    }

    //////////////////////////////////////////////////////////////////////////
    //
    // [StdNewDocument ("classname", "docname")]
    //
    //////////////////////////////////////////////////////////////////////////

    if (aCmd == aStdCreate)
    {
        hresult = lpdocClient->DoInitNew();
        lpdocClient->m_fCreatedNotConnected = TRUE;
        goto end;
    }


    //////////////////////////////////////////////////////////////////////////
    //
    // [StdNewFormTemplate ("classname", "docname". "templatename)]
    //
    //////////////////////////////////////////////////////////////////////////
    if (aCmd == aStdCreateFromTemplate)
    {
        ErrRtnH (lpdocClient->DoInitNew());
        lpdocClient->m_fCreatedNotConnected = TRUE;
        IPersistFile FAR * lpPF;
        lptemplate = lpnextarg;

        if(!(lpnextarg = ScanArg(lpnextarg)))
        {
            goto errRtn;
        }


        hresult = lpdocClient->m_lpoleObj->QueryInterface(IID_IPersistFile,(LPLPVOID)&lpPF);
        if (hresult == NOERROR)
        {
            WCHAR awcWideTemplate[MAX_STR];

            if (MultiByteToWideChar(CP_ACP,0,lpdocname,-1,awcWideTemplate,MAX_STR) != FALSE)
            {
                hresult = lpPF->Load(awcWideTemplate, 0);
            }
            else
            {
                Assert(!"Unable to convert characters");
                lpPF->Release();
                hresult = E_UNEXPECTED;
                goto end;
            }

            lpPF->Release();
            lpdocClient->m_fEmbed = TRUE;
        }
        else
        {
            goto end;
        }
    }
    //////////////////////////////////////////////////////////////////////////
    //
    // [StdEditDocument ("docname")]
    //
    //////////////////////////////////////////////////////////////////////////
    // REVIEW: Do we have to call InitNew for editing an embedded object

    if (aCmd == aStdEdit)
    {
        lpdocClient->m_fEmbed = TRUE;
        lpdocClient->m_fGotEditNoPokeNativeYet = TRUE;
        lpdocClient->m_fCreatedNotConnected = TRUE;
        goto end;
    }

    intrDebugOut((DEB_IERROR,
                  "%x CDDESrvr::SrvrExecute Unknown command\n",
                  this));

end:

    if (hresult != NOERROR)
        goto errRtn;
end1:
    // make sure that the srg string is indeed terminated by
    // NULL.
    if (*lpnextarg)
    {
        hresult = RPC_E_DDE_SYNTAX_EXECUTE;
    }
errRtn:

   if ( hresult != NOERROR)
   {
        if (bCreateInst && lpdocClient)
        {
            lpdocClient->DestroyInstance ();
            lpdocClient = NULL;  //DestroyInstance invalidates the pointer
        }
   }

end2:
errRtn1:

   if (lpdata)
        GlobalUnlock (hdup);

   if (hdup)
        GlobalFree (hdup);
   if (pUnk)
                pUnk->Release();

   if (pPersistStg)
        pPersistStg->Release();

   Assert (GetScode(hresult) != E_UNEXPECTED);

   intrDebugOut((DEB_ITRACE,
                  "%x _OUT CDDESrvr::SrvrExecute hresult=%x\n",
                  this,
                  hresult));

   return hresult;
}




// Maybe CreateDocWindow
//
// Return NOERROR only if a doc window was created and it sent an ACK.
//


//+---------------------------------------------------------------------------
//
//  Function:   MaybeCreateDocWindow
//
//  Synopsis:   Determine if a DocWindow should be created
//
//  Effects:    Given a class, and a filename atom, determine if this thread
//              should be the server for this request.
//
//  Arguments:  [aClass] -- Class of object (PROGID)
//              [aFile] -- Filename (ATOM)
//              [hwndDdeServer] -- HWND of CDDEServer
//              [hwndSender] -- HWND of new requesting client
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-29-94   kevinro   Created
//
//  Notes:
//
//----------------------------------------------------------------------------
 INTERNAL MaybeCreateDocWindow
    (ATOM aClass,
    ATOM aFile,
    HWND hwndDdeServer,
    HWND hwndSender)
{
    CLSID       clsid       = CLSID_NULL;
    LPUNKNOWN   pUnk        = NULL;
    HWND        hwndClient  = NULL;
    ULONG       fAckSent    = FALSE;
    LPSRVR      pDdeSrvr    = NULL;
    WCHAR       szFile [MAX_STR];
    BOOL        fTrue       = TRUE;
    BOOL        fRunningInSDI   = FALSE;
    HRESULT     hresult = NOERROR;
    IClassFactory *pcf = NULL;
    IPersistFile *ppf = NULL;
    DdeClassInfo        ddeClassInfo;

    intrDebugOut((DEB_DDE_INIT,
                  "MaybeCreateDocWindow(aClass=%x(%ws),aFile=%x,"
                  "hwndDdeServer=%x,hwndSender=%x\n",
                  aClass,wAtomName(aClass),aFile,hwndDdeServer,hwndSender));

    //
    // If the window isn't valid, it would be very bad.
    //
    if (!IsWindowValid(hwndDdeServer))
    {
        intrDebugOut((DEB_DDE_INIT,
                      "MaybeCreateDocWindow: hwndDdeServer is invalid\n"));
        hresult = E_UNEXPECTED;
        goto exitRtn;
    }

    //
    // We need the filename, which is passed in an Atom
    //
    Assert (IsFile (aFile));
    if (GlobalGetAtomName(aFile,szFile,MAX_STR) == 0)
    {
        //
        // The filename was not valid
        //
        hresult = S_FALSE;
        intrDebugOut((DEB_IERROR,
                      "MaybeCreateDocWindow Invalid file atom\n"));
        goto exitRtn;
    }

    intrDebugOut((DEB_DDE_INIT,
                  "MaybeCreateDocWindow File=(%ws)\n",
                  WIDECHECK(szFile)));

    //
    // Get the class of the object. The class was passed as an atom
    // in the INITIATE message.
    //
    if (CLSIDFromAtomWithTreatAs (&aClass, &clsid, NULL))
    {
        intrDebugOut((DEB_IERROR,
                      "MaybeCreateDocWindow CLSIDFromAtom failed\n"));

        hresult = S_FALSE;
        goto exitRtn;
    }

    if (CoIsOle1Class(clsid))
    {
        // we shouldn't even be looking at this INIT message
        hresult = S_FALSE;
        intrDebugOut((DEB_DDE_INIT,
                      "MaybeCreateDocWindow Its an OLE 1.0 class\n"));
        goto exitRtn;
    }

    //
    // First of three cases is to see if the object is running in our
    // local apartment. If it is, then this is the object we need to create
    // a DDEServer for.
    //
    // Otherwise, We are going to try and load this file.
    // Therefore, we need the class factory from the CFT.
    //
    // GetClassInformationForDde won't find a match if the class factory was
    // single use, and is now hidden or invalid.
    //
    // If there was no class information available, then we are going to
    // check to see if the object is in the local ROT. If it is in the
    // local ROT, then we will use it, since it is registered and
    // available for use by others
    //

    ddeClassInfo.dwContextMask = CLSCTX_LOCAL_SERVER | CLSCTX_INPROC_SERVER;
    ddeClassInfo.fClaimFactory = TRUE;

    if ( GetLocalRunningObjectForDde(szFile, &pUnk) == NOERROR)
    {
        intrDebugOut((DEB_DDE_INIT,
                      "Found %ws in ROT\n",WIDECHECK(szFile)));
        //
        // Elsewhere in the code, we need to know if this is an SDI server.
        // The old code determined this by detecting that there is a running
        // object, and there was no class factory registered.
        // This is sick, and obscene. Compatibilities says we need to get the
        // class info anyway. However, we don't want to claim it.
        //

        ddeClassInfo.fClaimFactory = FALSE;
        fRunningInSDI = !GetClassInformationForDde(clsid,&ddeClassInfo);
    }
    else if (!GetClassInformationForDde(clsid,&ddeClassInfo))
    {
        intrDebugOut((DEB_IERROR,
                      "No class registered for %ws\n",WIDECHECK(szFile)));

        hresult = S_FALSE;
        goto exitRtn;
    }
    else
    {
        //
        // Otherwise, we are registered as the server for this class. This
        // means we can create this object.
        //
        // A 1.0 client will have launched the server with a command line
        // like server.exe -Embedding filename The server ignored the filename
        // so now we must make it load the file by binding the moniker.
        //
        // KevinRo: The old code did a bind moniker here, on the filename,
        // which went through the ROT, didn't find the object, so went for
        // a server. This isn't terribly safe, since we could end up binding
        // out of process when we really didn't mean to. So, I have made this
        // routine just use the ClassFactory we retrieve from the
        // local class factory table.
        //

        intrDebugOut((DEB_DDE_INIT,
                      "Found classinfo: Loading %ws\n",WIDECHECK(szFile)));


        //
        // Need to insure that the server doesn't go away on us. The following
        // tells the server not to destroy itself.
        //
        SSSendMessage(hwndDdeServer,WM_DONOTDESTROY,TRUE,0);

        intrAssert(ddeClassInfo.punk != NULL);
        pcf = (IClassFactory *) ddeClassInfo.punk;

        hresult = pcf->CreateInstance(NULL,IID_IUnknown,(void **)&pUnk);

        if (hresult != NOERROR)
        {
            intrDebugOut((DEB_IERROR,
                      "MaybeCreateDocWindow CreateInstancefailed File=(%ws)\n",
                          WIDECHECK(szFile)));
            goto sndMsg;
        }

        //
        // Get the IPersistFile interface, and ask the object to load
        // itself.
        //
        hresult = pUnk->QueryInterface(IID_IPersistFile,(void **)&ppf);
        if (hresult != NOERROR)
        {
            intrDebugOut((DEB_IERROR,
                          "MaybeCreateDocWindow QI IPF failed File=(%ws)\n",
                          WIDECHECK(szFile)));
            goto sndMsg;
        }
        //
        // Attempt to load the object. The flags STGM_READWRITE are the
        // same default values used by a standard bind context.
        //
        hresult = ppf->Load(szFile,STGM_READWRITE);
        if (hresult != NOERROR)
        {
            intrDebugOut((DEB_IERROR,
                          "MaybeCreateDocWindow ppf->Load(%ws) failed %x\n",
                          WIDECHECK(szFile),
			  hresult));
            goto sndMsg;
        }
sndMsg:
        SSSendMessage(hwndDdeServer,WM_DONOTDESTROY,FALSE,0);
        if (hresult != NOERROR)
        {
            goto exitRtn;

        }
        intrDebugOut((DEB_DDE_INIT,
                      "Loading %ws complete\n",WIDECHECK(szFile)));

    }


    intrAssert(IsWindowValid(hwndDdeServer));
    intrAssert (pUnk);

    pDdeSrvr = (LPSRVR) GetWindowLong (hwndDdeServer, 0);
    if (pDdeSrvr == NULL)
    {
        intrAssert(pDdeSrvr != NULL);
        hresult = E_UNEXPECTED;
        goto exitRtn;
    }

    // This actually creates the doc window as a child of the server window
    // Do not set the client site becuase this is a link.
    hresult = CDefClient::Create (pDdeSrvr,
                                  pUnk,
                                  szFile,
                                  /*fSetClientSite*/FALSE,
                                  /*fDoAdvise*/TRUE,
                                  fRunningInSDI,
                                  &hwndClient);

    if (hresult != NOERROR)
    {
        intrDebugOut((DEB_IERROR,
                      "MaybeCreateDocWindow CDefClient::Create failed %x\n",
                      hresult));
        goto exitRtn;
    }

    Assert (IsWindowValid (hwndClient));

    //
    // Pass along the original DDE_INIT to the newly created window.
    // That window should respond by sending an ACK to the 1.0 client.
    //
    fAckSent = SSSendMessage (hwndClient,
                            WM_DDE_INITIATE,
                     (UINT) hwndSender,
                            MAKELONG(aClass, aFile));
    if (!fAckSent)
    {
        intrDebugOut((DEB_IERROR,
                      "MaybeCreateDocWindow !fAckSent\n"));
        hresult = CO_E_APPDIDNTREG;
    }

exitRtn:

    if (ppf)
    {
        ppf->Release();
    }
    if (pUnk)
    {
        pUnk->Release();
    }
    if (pcf)
    {
        pcf->Release();
    }

    intrDebugOut((DEB_DDE_INIT,
                  "MaybeCreateDocWindow returns %x\n",
                  hresult));
    return hresult;
}


//+---------------------------------------------------------------------------
//
//  Function:   SendMsgToChildren
//
//  Synopsis:   This routine sends the msg to all child windows.
//
//  Arguments:  [hwnd] -- Hwnd of parent window
//		[msg] --  Message and parameters to send
//		[wParam] --
//		[lParam] --
//
//  Notes: This routine will stop on the first non-zero return code.
//
//----------------------------------------------------------------------------
BOOL SendMsgToChildren (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    intrDebugOut((DEB_ITRACE,
                  "0 _IN SendMsgToChildren(hwnd=%x,msg=%x,wParam=%x,lParam=%x)\n",
                  hwnd,msg,wParam,lParam));

    BOOL fAckSent = FALSE;

    hwnd = GetWindow(hwnd, GW_CHILD);

    //
    // This routine is to be called only from one place, which is
    // in the handling of WM_DDE_INITIATE. Because of that, we will terminate
    // the loop on the first non-zero return code.
    //
    Assert (msg == WM_DDE_INITIATE);

    while (hwnd)
    {
        intrDebugOut((DEB_ITRACE,"   SendMsgToChildren send to hwnd=%x\n",hwnd));

        if (fAckSent = (1L==SSSendMessage (hwnd, msg, wParam, lParam)))
	{
            break;
	}

        hwnd = GetWindow (hwnd, GW_HWNDNEXT);
    }

    intrDebugOut((DEB_ITRACE,"0 OUT SendMsgToChildren returns %x\n",fAckSent));
    return(fAckSent);
}


//+---------------------------------------------------------------------------
//
//  Function:   SendInitMsgToChildren
//
//  Synopsis:   Sends an init message to all child windows of the hwnd
//
//  Effects:    This routine will send an init message to all children
//              of the given window. It is assuming that the lParam is
//              the atom that contains the topic (ie filename) of the
//              object being looked for.
//
//  Arguments:  [hwnd] -- hwnd of server window
//              [msg] -- MSG to send
//              [wParam] -- hwnd of client window
//              [lParam] -- HIWORD(lParam) is atom of filename
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Algorithm:
//
//  History:    6-28-94   kevinro   Commented
//
//  Notes:
//
//----------------------------------------------------------------------------
BOOL SendInitMsgToChildren (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
    intrDebugOut((DEB_DDE_INIT,
                  "0 _IN SendInitMsgToChildren(hwnd=%x,msg=%x,wParam=%x,lParam=%x)\n",
                  hwnd,msg,wParam,lParam));

    BOOL fAckSent = FALSE;

    fAckSent = SendMsgToChildren(hwnd,msg,wParam,lParam);

    //
    // If no windows acknowledged, then we might need to create a doc window
    //
    if (!fAckSent)
    {
        ATOM aTopic = HIWORD(lParam);
        Assert (IsAtom(aTopic));

        // if someone's trying to initiate on a filename, i.e., for a link
        // then create the doc window on demand because 2.0 servers do not
        // register doc windows.  They don't even accept "-Embedding filename"
        // on the command line
        if (aTopic != aOLE && aTopic != aSysTopic && IsFile (aTopic))
        {
            intrDebugOut((DEB_DDE_INIT,"   Initiate for link %ws\n",wAtomName(aTopic)));
            HRESULT hresult = MaybeCreateDocWindow (LOWORD(lParam), aTopic,
                                                    hwnd, (HWND)wParam);

            fAckSent = (NOERROR==hresult);
        }
    }
    intrDebugOut((DEB_DDE_INIT,
                  "0 _OUT SendInitMsgToChildren fAckSent=%x\n",fAckSent));
    return fAckSent;
}



INTERNAL_(HRESULT)   RequestDataStd
(
ATOM        aItem,
LPHANDLE    lphdde
)
{


    HANDLE  hnew = NULL;

    if (!aItem)
        goto errRtn;

    if (aItem == aEditItems){
        hnew = MakeGlobal ("StdHostNames\tStdDocDimensions\tStdTargetDevice");
        goto   PostData;

    }

    if (aItem == aProtocols) {
        hnew = MakeGlobal ("Embedding\tStdFileEditing");
        goto   PostData;
    }

    if (aItem == aTopics) {
        hnew = MakeGlobal ("Doc");
        goto   PostData;
    }

    if (aItem == aFormats) {
        hnew = MakeGlobal ("Picture\tBitmap");
        goto   PostData;
    }

    if (aItem == aStatus) {
        hnew = MakeGlobal ("Ready");
        goto   PostData;
    }

    // format we do not understand.
    goto errRtn;

PostData:

    // Duplicate the DDE data
    if (MakeDDEData (hnew, CF_TEXT, lphdde, TRUE)){
        // !!! why are we duplicating the atom.
        DuplicateAtom (aItem);
        return NOERROR;
    }
errRtn:
    return ReportResult(0, S_FALSE, 0, 0);
}


//IsSingleServerInstance: returns true if the app is single server app else
//false.

INTERNAL_(BOOL)  IsSingleServerInstance ()
{
    HWND    hwnd;
    WORD    cnt = 0;
    HTASK   hTask;
    DdeCHAR    buf[MAX_STR];

    hwnd  = GetWindow (GetDesktopWindow(), GW_CHILD);
    hTask = GetCurrentThreadId();

    while (hwnd) {
        if (hTask == ((HTASK) GetWindowThreadProcessId (hwnd,NULL))) {
            DdeGetClassName (hwnd, buf, MAX_STR);
            if (Ddelstrcmp (buf, SRVR_CLASS) == 0)
                cnt++;
        }
        hwnd = GetWindow (hwnd, GW_HWNDNEXT);
    }
#ifdef  FIREWALLS
     AssertSz (cnt > 0, "srvr window instance count is zero");
#endif
    if (cnt == 1)
        return TRUE;
    else
        return FALSE;

}


// QueryRevokeClassFactory: returns FALSE if there are clients
// connected tothis class factory;
INTERNAL_(BOOL)        CDDEServer::QueryRevokeClassFactory ()
{

    HWND        hwnd;
    LPCLIENT    lpclient;

    Assert (IsWindow (m_hwnd));
    hwnd = GetWindow (m_hwnd, GW_CHILD);
    while (hwnd)
    {
        Assert (IsWindow (hwnd));
        lpclient = (LPCLIENT)GetWindowLong (hwnd, 0);
        if (lpclient->m_cClients != 0 || lpclient->m_fCreatedNotConnected)
            return FALSE;
        hwnd = GetWindow (hwnd, GW_HWNDNEXT);
    }
    return TRUE;
}



//+---------------------------------------------------------------------------
//
//  Method:     CDDEServer::CreateInstance
//
//  Synopsis:   Create an instance of a document
//
//  Effects:
//
//  Arguments:  [lpclassName] --
//              [lpWidedocName] --
//              [lpdocName] --
//              [pUnk] --
//              [lplpdocClient] --
//              [hwndClient] --
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    5-30-94   kevinro Commented/cleaned
//
//  Notes:
//
//----------------------------------------------------------------------------
INTERNAL CDDEServer::CreateInstance
(
REFCLSID        lpclassName,
LPOLESTR        lpWidedocName,
LPSTR           lpdocName,
LPUNKNOWN       pUnk,
LPCLIENT FAR*   lplpdocClient,
HWND            hwndClient)
{
    intrDebugOut((DEB_ITRACE,
                  "%p _IN CDDEServer::CreateInstance(lpWidedocName=%ws,hwndClient=%x)\n",
                  this,
                  WIDECHECK(lpWidedocName),
                  hwndClient));


    LPUNKNOWN           pUnk2=NULL;
    LPOLEOBJECT     lpoleObj= NULL;       // unknown object
    HRESULT         hresult;

    ChkS(this);

    if (NULL==pUnk)
    {
        Assert (m_pClassFactory);
        hresult = m_pClassFactory->CreateInstance (NULL, IID_IUnknown, (LPLPVOID)&pUnk2);

        if (hresult != NOERROR)
        {
            return hresult;
        }

        // Now that we have *used* the DDE server window, we can unlock
        // the server.
        // The OLE1 OleLockServer API opens a dummy DDE system channel
        // and just leaves it open until OleunlockServer is called.
        // Since we have now used this channel, we know it was not created
        // for the purpose of locking the server.
        this->Lock (FALSE, hwndClient);

        // if it is an SDI app, we must revoke the ClassFactory after using it
        // it is only good for "one-shot" createinstance call.
        if (m_fcfFlags == REGCLS_SINGLEUSE)
        {
            m_pClassFactory->Release();         // done with the ClassFactory
            Puts ("NULLing m_pCF\r\n");
            m_pClassFactory = NULL;
        }
    }
    else
    {
        pUnk2 = pUnk;
        pUnk->AddRef();
    }

    hresult = CDefClient::Create ((LPSRVR)this,
                                  pUnk2,
                                  lpWidedocName,
                                  /*fSetClientSite*/FALSE,
                                  /*fDoAdvise*/pUnk!=NULL);

    intrAssert (pUnk2 != NULL);
    if (pUnk2 != NULL)
    {
        pUnk2->Release();
    }

    pUnk2 = NULL;

    // REVIEW: error recovery
    if (!(*lplpdocClient = FindDocObj (lpdocName)))
    {
        intrAssert(!"Document created but not found");
    }
    else
    {
        // set the server instance flag so that WM_DDE_INITIATE will not icrement
        // the ref count. (EXCEL BUG)
        (*lplpdocClient)->m_bCreateInst = TRUE;
    }
    intrDebugOut((DEB_ITRACE,
                  "%p _OUT CDDEServer::CreateInstance hresult=%x\n",
                  this,hresult));
    return hresult;
}


INTERNAL_(void) CDDEServer::Lock
        (BOOL fLock,      // lock or unlock?
        HWND hwndClient)  // on behalf of which window?
{
    intrDebugOut((DEB_ITRACE,
                  "%p _IN CDDEServer::Lock(fLock=%x,hwndCient=%x)\n",
                  this,
                  fLock,
                  hwndClient));

    VDATEHEAP();
    BOOL fIsLocked = (BOOL) FindClient (m_hcli, hwndClient, /*fDelete*/FALSE);

    if (fLock && !fIsLocked)
    {
        if (m_pClassFactory)
        {
            intrDebugOut((DEB_ITRACE,
                          "%p ::Locking %x\n",
                          this,
                          m_pClassFactory));

            m_pClassFactory->LockServer (TRUE);
            // Only way to change the data associated with a client window
            // is to delete it and re-add it with the new data.
            FindClient (m_hcli, hwndClient, /*fDelete*/ TRUE);
            AddClient (&m_hcli, hwndClient, (HANDLE) TRUE); // mark as locked
        }
    }
    else if (!fLock && fIsLocked)
    {
        if (m_pClassFactory)
        {
            intrDebugOut((DEB_ITRACE,
                          "%p ::UnLocking %x\n",
                          this,
                          m_pClassFactory));
            m_pClassFactory->LockServer (FALSE);
            FindClient (m_hcli, hwndClient, /*fDelete*/ TRUE);
            AddClient (&m_hcli, hwndClient, (HANDLE) FALSE); //mark as unlocked
        }
    }
    VDATEHEAP();
    intrDebugOut((DEB_ITRACE,
                  "%p _OUT CDDEServer::Lock(fLock=%x,hwndCient=%x)\n",
                  this,
                  fLock,
                  hwndClient));
}





INTERNAL CDefClient::DestroyInstance
    (void)
{
    Puts ("DestroyInstance\r\n");
    // We just created the instance. we ran into error.
    // just call Release.
    m_pUnkOuter->AddRef();
    ReleaseObjPtrs();
    Verify (0==m_pUnkOuter->Release());
    //  "this" should be deleted now
    return NOERROR;
}



INTERNAL CDefClient::SetClientSite
 (void)
{
    HRESULT hresult = m_lpoleObj->SetClientSite (&m_OleClientSite);
    if (hresult==NOERROR)
    {
        m_fDidSetClientSite = TRUE;
    }
    else
    {
        Warn ("SetClientSite failed");
    }
    return hresult;
}


// implementations of IRpcStubBuffer methods
STDMETHODIMP CDdeServerCallMgr::QueryInterface
    ( REFIID iid, LPVOID * ppvObj )
{
    return S_OK;
}

STDMETHODIMP_(ULONG)CDdeServerCallMgr::AddRef ()
{
    return 1;
}

STDMETHODIMP_(ULONG)CDdeServerCallMgr::Release ()
{
    return 1;
}


STDMETHODIMP CDdeServerCallMgr::Connect
    (IUnknown * pUnkServer )
{
    // do nothing
    return S_OK;
}

STDMETHODIMP_(void) CDdeServerCallMgr::Disconnect
    ()
{
    // do nothing
}

STDMETHODIMP_(IRpcStubBuffer*) CDdeServerCallMgr::IsIIDSupported
    (REFIID riid)
{
    // do nothing
    return NULL;
}


STDMETHODIMP_(ULONG) CDdeServerCallMgr::CountRefs
    ()
{
    // do nothing
    return 1;
}

STDMETHODIMP CDdeServerCallMgr::DebugServerQueryInterface
    (void ** ppv )
{
    // do nothing
    *ppv = NULL;
    return S_OK;
}


STDMETHODIMP_(void) CDdeServerCallMgr::DebugServerRelease
    (void * pv)
{
    // do nothing
}

STDMETHODIMP CDdeServerCallMgr::Invoke
    (RPCOLEMESSAGE *_prpcmsg, IRpcChannelBuffer *_pRpcChannelBuffer)
{
    DISPATCHDATA *pdispdata = (PDISPATCHDATA) _prpcmsg->Buffer;
    return DispatchCall( pdispdata );
}


// Provided IRpcChannelBuffer methods (for callback methods side)
STDMETHODIMP CDdeServerCallMgr::GetBuffer(
/* [in] */ RPCOLEMESSAGE __RPC_FAR *pMessage,
/* [in] */ REFIID riid)
{
    return S_OK;
}

STDMETHODIMP CDdeServerCallMgr::SendReceive(
/* [out][in] */ RPCOLEMESSAGE __RPC_FAR *pMessage,
/* [out] */ ULONG __RPC_FAR *pStatus)
{
    return S_OK;
}

STDMETHODIMP CDdeServerCallMgr::FreeBuffer(
/* [in] */ RPCOLEMESSAGE __RPC_FAR *pMessage)
{
    return S_OK;
}

STDMETHODIMP CDdeServerCallMgr::GetDestCtx(
/* [out] */ DWORD __RPC_FAR *pdwDestContext,
/* [out] */ void __RPC_FAR *__RPC_FAR *ppvDestContext)
{
    return S_OK;
}

STDMETHODIMP CDdeServerCallMgr::IsConnected( void)
{
    return S_OK;
}

STDMETHODIMP CDdeServerCallMgr::SendReceive2(
/* [out][in] */ RPCOLEMESSAGE __RPC_FAR *pMessage,
/* [out] */ ULONG __RPC_FAR *pStatus)
{
    intrDebugOut((DEB_ITRACE,
                  "%p _IN CDdeServerCallMgr::SendReceive2(pMessage=%x,pStatus=%x)\n",
                  this,
                  pMessage,
                  pStatus));

    DDECALLDATA *pCD = ((DDECALLDATA *) pMessage->Buffer);

    if(!PostMessageToClient(pCD->hwndSvr,
                            pCD->wMsg,
                            pCD->wParam,
                            pCD->lParam))
    {
	intrDebugOut((DEB_ITRACE, "SendRecieve2(%x)PostMessageToClient failed", this));
        return RPC_E_SERVER_DIED;
    }


    CAptCallCtrl *pCallCtrl = GetAptCallCtrl();

    CCliModalLoop *pCML = pCallCtrl->GetTopCML();

    HRESULT hres = S_OK;
    BOOL fWait = !(m_pDefClient->m_CallState == SERVERCALLEX_ISHANDLED);

    while (fWait)
    {
	HRESULT hr = OleModalLoopBlockFn(NULL, pCML, NULL);

	if (m_pDefClient->m_CallState == SERVERCALLEX_ISHANDLED)
	{
	    fWait = FALSE;
	}
	else if (hr != RPC_S_CALLPENDING)
	{
	    fWait = FALSE;
	    hres = hr;		// return result from OleModalLoopBlockFn()
	}
    }

    if (FAILED(hres))
    {
	intrDebugOut((DEB_ITRACE, "**** CDdeServerCallMgr::SendReceive2 OleModalLoopBlockFn returned %x ***\n", hres));
    }

    intrDebugOut((DEB_ITRACE,
                  "%p _OUT CDdeServerCallMgr::SendReceive2(pMessage=%x,pStatus=%x)\n",
                  this,
                  pMessage,
                  pStatus));

    return hres;
}