summaryrefslogblamecommitdiffstats
path: root/private/ole32/dcomss/olescm/scmif.cxx
blob: 6036d3642e60b9ff6c0e3e58e80a7215f9e76b93 (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




















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                              
//+-------------------------------------------------------------------------
//
//  Microsoft Windows
//  Copyright (C) Microsoft Corporation, 1992 - 1993.
//
//  File:       scmif.cxx
//
//  Contents:   Entry points for scm interface.
//
//  Functions:  StartObjectService
//              SvcActivateObject
//              SvcCreateActivateObject
//              ObjectServerStarted
//              StopServer
//
//  History:    01-May-93 Ricksa    Created
//              31-Dec-93 ErikGav   Chicago port
//
//--------------------------------------------------------------------------

#include <headers.cxx>
#pragma hdrstop

#include    <scm.h>
#include    "rawdscm.h"
#include    "scm.hxx"
#include    "port.hxx"
#include    "cls.hxx"
#include    <caller.hxx>
#include    "dbgprt.hxx"

#include    "rotif.hxx"
#include    "clckpath.hxx"
#include    <valid.h>
#include    "or.hxx"

#ifndef _CHICAGO_
#include    <shrtbl.hxx>
extern CScmShrdTbl *g_pScmShrdTbl;
#endif

//+-------------------------------------------------------------------------
//
//  Function:   Dummy1
//
//  Synopsis:   Needed for IDL hack.  Never called.
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//
//  Returns:    HRESULT
//
//  History:    14 Apr 95   AlexMit    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT DummyQueryInterfaceIOSCM(
    handle_t  hRpc,
    ORPCTHIS  *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    DWORD     dummy )
{
    CairoleDebugOut((DEB_ERROR, "SCM Dummy function should never be called!\n" ));
    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;
    return E_FAIL;
}

//+-------------------------------------------------------------------------
//
//  Function:   Dummy2
//
//  Synopsis:   Needed for IDL hack.  Never called.
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//
//  Returns:    HRESULT
//
//  History:    14 Apr 95   AlexMit    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT DummyAddRefIOSCM(
    handle_t  hRpc,
    ORPCTHIS  *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    DWORD     dummy )
{
    CairoleDebugOut((DEB_ERROR, "SCM Dummy function should never be called!\n" ));
    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;
    return E_FAIL;
}

//+-------------------------------------------------------------------------
//
//  Function:   Dummy3
//
//  Synopsis:   Needed for IDL hack.  Never called.
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//
//  Returns:    HRESULT
//
//  History:    14 Apr 95   AlexMit    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT DummyReleaseIOSCM(
    handle_t  hRpc,
    ORPCTHIS  *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    DWORD     dummy )
{
    CairoleDebugOut((DEB_ERROR, "SCM Dummy function should never be called!\n" ));
    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;
    return E_FAIL;
}

//+-------------------------------------------------------------------------
//
//  Function:	ServerRegisterClsid
//
//  Synopsis:   Notifies SCM that server is started for a class
//
//  Arguments:	[hRpc] - RPC handle
//		[phProcess] - context handle
//		[lpDeskTop] - caller's desktop
//		[pregin] - array of registration entries
//		[ppregout] - array of registration cookies to return
//		[rpcstat] - status code
//
//  Returns:    HRESULT
//
//  History:    01-May-93 Ricksa    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT ServerRegisterClsid(
    handle_t hRpc,
    PHPROCESS phProcess,
    WCHAR *  pwszWinstaDesktop,
    RegInput *pregin,
    RegOutput **ppregout,
    error_status_t *rpcstat)
{
    CheckLocalCall( hRpc );

    *rpcstat = 0;

    HRESULT hr = S_OK;

#if DBG == 1

    CairoleDebugOut((DEB_SCM, "_IN ObjectServerStarted\n"));

#if defined(_NT1X_) && !defined(_CHICAGO_)

    CairoleDebugOut((DEB_SCM, "_IN Desk Top: %s\n", pwszWinstaDesktop));

#endif // _NT1X_ && !_CHICAGO_

    DbgPrintRegIn("_IN Classes To Register", pregin);

#endif // DBG == 1

    VDATEHEAP();

    // Initialize the output structure.
    int cOutBytes = sizeof(RegOutput)
        + (pregin->dwSize - 1) * sizeof(RegOutputEnt);

    RegOutput *pregout = (RegOutput *) PrivMemAlloc(cOutBytes);

    *ppregout = pregout;

    if (pregout == NULL)
        return E_OUTOFMEMORY;

    memset(pregout, 0, cOutBytes);
    pregout->dwSize = pregin->dwSize;

    hr = GetClassCache().SetEndPoints(
#ifndef _CHICAGO_
            phProcess,
            ((CProcess *)phProcess)->GetToken()->GetSid(),
            pwszWinstaDesktop,
#else
            NULL,
#endif
            pregin,
            pregout );

    VDATEHEAP();

#if DBG == 1
    CairoleDebugOut((DEB_SCM, "OUT ObjectServerStarted\n"));
    CairoleDebugOut((DEB_SCM, "OUT Hresult : %lx\n", hr));

    if (SUCCEEDED(hr))
    {
        DbgPrintRegOut("OUT Register Information", ppregout);
    }

#endif // DBG == 1

    return hr;
}

//+-------------------------------------------------------------------------
//
//  Function:   StopServer
//
//  Synopsis:   Get notification that class server is stopping
//
//  Arguments:  [hRpc] - RPC handle
//              [prevcls] - list of classes/registrations to stop
//
//  History:    01-May-93 Ricksa    Created
//
//--------------------------------------------------------------------------
extern "C" void ServerRevokeClsid(
    handle_t hRpc,
    PHPROCESS phProcess,
    RevokeClasses *prevcls,
    error_status_t *rpcstat)
{
#if DBG == 1

    CairoleDebugOut((DEB_SCM, "_IN StopServer\n"));
    DbgPrintRevokeClasses("_IN RevokeClasses:", prevcls);

#endif // DBG == 1

    CheckLocalCall( hRpc );

    VDATEHEAP();

    *rpcstat = 0;

    DWORD cLoops = prevcls->dwSize;
    RevokeEntry *prevent = prevcls->revent;

    for (DWORD i = 0; i < cLoops; i++, prevent++)
    {
        GetClassCache().StopServer(prevent->clsid,
                                   IFSECURITY( ((CProcess *)phProcess)->GetToken()->GetSid() )
                                   prevent->dwReg);

        ((CProcess *)phProcess)->RemoveClassReg( prevent->clsid, prevent->dwReg );
    }

    VDATEHEAP();
    CairoleDebugOut((DEB_SCM, "OUT StopServer\n"));
}

void GetThreadID(
    handle_t    hRpc,
    DWORD *     pThreadID,
    error_status_t *prpcstat)
{
    CheckLocalCall( hRpc );

    *prpcstat = 0;
    *pThreadID = InterlockedExchangeAdd((long *)&gNextThreadID,1);
}

#ifndef _CHICAGO_
//+-------------------------------------------------------------------------
//
//  Function:   UpdateShrdTbls
//
//  Synopsis:   Update the shared memory tables.
//
//  Arguments:  [hRpc] - RPC handle
//              [prpcstat] - communication status
//
//  History:    11-Jul-94 Rickhi    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT UpdateShrdTbls(
    handle_t hRpc,
    error_status_t *prpcstat)
{
    CheckLocalCall( hRpc );

    *prpcstat = 0;
    CairoleDebugOut((DEB_SCM, "_IN UpdateShrdTbls\n"));
    VDATEHEAP();

    // we dont take the lock because the caller is holding it for us.
    g_pScmShrdTbl->UpdateWithLock();

    VDATEHEAP();
    CairoleDebugOut((DEB_SCM, "OUT UpdateShrdTbls\n"));
    return S_OK;
}

//+-------------------------------------------------------------------------
//
//  Function:   UpdateActivationSettings
//
//  Synopsis:   Re-read default activation settings.
//
//  Arguments:  [hRpc] - RPC handle
//              [prpcstat] - communication status
//
//--------------------------------------------------------------------------
extern void ComputeSecurity();

extern "C" void UpdateActivationSettings(
    handle_t hRpc,
    error_status_t *prpcstat)
{
    CheckLocalCall( hRpc );

    *prpcstat = 0;
    gpClassCache->ReadRemoteActivationKeys();
    ComputeSecurity();
}
#endif // !_CHICAGO_


#ifdef _CHICAGO_
//+-------------------------------------------------------------------------
//
//  Function:   StartObjectService
//
//  Synopsis:   Get a class object for a client
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//              [rclsid] - class id for reequest
//              [dwCtrl] - type of server required
//              [ppIFDClassObj] - where to return the serialized class object
//              [ppwszDllToLoad] - where to return DLL path
//
//  Returns:    HRESULT
//
//  History:    01-May-93 Ricksa    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT StartObjectService(
    handle_t hRpc,
    ORPCTHIS *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    WCHAR *lpDesktop,
    const GUID *guidCLSID,
    DWORD dwCtrl,
    WCHAR *pwszServer,
    InterfaceData **ppIFDClassObj,
    DWORD *pdwDllType,
    WCHAR **ppwszDllToLoad )
{

    REFCLSID rclsid = *guidCLSID;

#if DBG ==1

    CairoleDebugOut((DEB_SCM, "_IN StartObjectServer\n"));
    CairoleDebugOut((DEB_SCM, "_IN Desk Top: %s\n", lpDesktop));
    DbgPrintGuid("_IN ThreadId: ", &orpcthis->cid);
    DbgPrintGuid("_IN CLSID: ", guidCLSID);
    CairoleDebugOut((DEB_SCM, "_IN Context: %lX\n", dwCtrl));
    CairoleDebugOut((DEB_SCM, "_IN DLL Type: %lX\n", *pdwDllType));

#endif // DBG == 1

    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;

    VDATEHEAP();

    CBaseData bd( GETCLASSOBJECT,
        lpDesktop, &orpcthis->cid, rclsid, dwCtrl,
        pdwDllType, ppwszDllToLoad, ppIFDClassObj,
        NULL, FALSE, NULL, NULL, NULL, NULL,FALSE);

    HRESULT hr = GetClassCache().ProcessScmMessage(&bd, NULL);

    VDATEHEAP();

#if DBG == 1

    CairoleDebugOut((DEB_SCM, "OUT StartObjectServer\n"));
    CairoleDebugOut((DEB_SCM, "OUT Hresult : %lx\n", hr));

    if (SUCCEEDED(hr))
    {
        if (*ppIFDClassObj)
        {
            DbgPrintIFD("OUT Class Obj:", *ppIFDClassObj);
        }

        if (*ppwszDllToLoad)
        {
            CairoleDebugOut((DEB_SCM, "OUT DLL Type: %lX\n", *pdwDllType));
            CairoleDebugOut((DEB_SCM, "OUT DLL: %ws\n", *ppwszDllToLoad));
        }
    }

#endif // DBG == 1

    return hr;
}

//+-------------------------------------------------------------------------
//
//  Function:   SvcActivateObject
//
//  Synopsis:   Instantiate an object with interface
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//              [rclsid] - class id for object
//              [dwOptions] - type of server needed
//              [grfMode] - how to open file if specified
//              [pwszPath] - path to object
//              [pIFDstg] - marshaled storage
//              [pwszDllPath] - path to DLL
//
//  Returns:    HRESULT
//
//  History:    01-May-93 Ricksa    Created
//              18-Aug-94 AlexT     Add caller, callee thread id
//
//--------------------------------------------------------------------------

// BUGBUG [mikese] The pwszServerAddress parameter is redundant, and should
//  be removed from the IDL

// BUGBUG: hash value is an obsolete field as well.

extern "C" HRESULT SvcActivateObject(
    handle_t hRpc,
    ORPCTHIS *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    WCHAR *lpDesktop,
    WCHAR *pwszProtseq,
    const GUID *pclsid,
    DWORD dwOptions,
    DWORD grfMode,
    DWORD dwHash,
    WCHAR *pwszPath,
    InterfaceData *pIFDstg,
    InterfaceData **ppIFDunk,
    DWORD *pdwDllType,
    WCHAR **ppwszDllPath,
    WCHAR *pwszServerAddress )
{
#if DBG ==1

    CairoleDebugOut((DEB_SCM, "_IN SvcActivateObject\n"));
    CairoleDebugOut((DEB_SCM, "_IN Desk Top: %s\n", lpDesktop));
    CairoleDebugOut((DEB_SCM, "_IN Protocol Seq: %ws\n",
       (pwszProtseq == NULL) ? L"None" : pwszProtseq));
    DbgPrintGuid("_IN ThreadId: ", &orpcthis->cid);
    DbgPrintGuid("_IN CLSID: ", pclsid);
    CairoleDebugOut((DEB_SCM, "_IN Context: %lX\n", dwOptions));
    CairoleDebugOut((DEB_SCM, "_IN Mode: %lX\n", grfMode));
    CairoleDebugOut((DEB_SCM, "_IN Path: %ws\n",
       (pwszPath == NULL) ? L"None" : pwszPath));
    CairoleDebugOut((DEB_SCM, "_IN Stg: %lX\n", pIFDstg));
    CairoleDebugOut((DEB_SCM, "_IN DLL Type: %lX\n", *pdwDllType));

#endif // DBG == 1

    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;

    VDATEHEAP();

    HRESULT hr = S_OK;

    InterfaceData *pifdFromROT = NULL;

    BEGIN_BLOCK

        // Lock the path so that only one path may be bound at a time
        CLockPath lckpath(pwszPath, hr);

        if (FAILED(hr))
        {
            // Couldn't lock the path -- memory problem maybe. Anyway, we
            // can't go anywhere from here.
            CairoleDebugOut((DEB_ERROR,
                "SvcActivateObject Lock of Path Failed %lx\n", hr));

            EXIT_BLOCK;
        }

        // If there is a path Look for path in the Directory ROT
        if (pwszPath != NULL)
        {
            // Remember that pdwDllType is overloaded here. We use it to
            // indicate that we got an object from the ROT. We need to do
            // this because there is no guarantee that the ROT entry will
            // not be bad by the time we get back to the caller and therefore,
            // the caller will want to retry in this case.
            if (GetObjectFromRot(pwszPath, &pifdFromROT) == S_OK)
            {
                // Is this a local operatoin?
                if (pwszProtseq == NULL)
                {
                    // Return the marshaled interface from the ROT to the
                    // caller.
                    *ppIFDunk = pifdFromROT;

                    // So we remember not to clean up the buffer
                    pifdFromROT = NULL;

                    // Let caller know that we got this from the ROT so
                    // if it doesn't work they should try again.
                    *pdwDllType = GOT_FROM_ROT;

                    // We got what we came for from the ROT so we can exit
                    CairoleDebugOut((DEB_TRACE, "Found object in the ROT\n"));
                    EXIT_BLOCK;
                }

                // Because a tabled marshaled interface is not enought
                // to get unmarshaled locally, we need to send it back
                // to the object server to get something we can pass back
                // to another machine.
            }
        }

        // BUGBUG - Until we can decide how to pass back the callee's thread
        // id, just hold it here.
        DWORD   dwTIDCallee;
        IID     IIDs[1];
        HRESULT Results[1];

        IIDs[0] = IID_IUnknown;

        CGetCreateData gcd(pwszProtseq, dwOptions, grfMode, pwszPath,
            pIFDstg, NULL, ppIFDunk, localthis->dwClientThread,
              &dwTIDCallee, pifdFromROT,
              1, IIDs, Results);

        CBaseData bd(GETPERSISTENTOBJ,
            lpDesktop, &orpcthis->cid, *pclsid, dwOptions, pdwDllType,
                ppwszDllPath, ppIFDunk,
                NULL, FALSE, NULL, NULL, NULL, NULL,FALSE);

        hr = GetClassCache().ProcessScmMessage(&bd, &gcd);

    END_BLOCK;

    VDATEHEAP();

#if DBG == 1

    CairoleDebugOut((DEB_SCM, "OUT SvcActivateObject\n"));
    CairoleDebugOut((DEB_SCM, "OUT Hresult : %lx\n", hr));

    if (SUCCEEDED(hr))
    {
        if (*ppIFDunk)
        {
            DbgPrintIFD("OUT Object:", *ppIFDunk);
        }

        if (*ppwszDllPath)
        {
            CairoleDebugOut((DEB_SCM, "OUT DLL Type: %lX\n", *pdwDllType));
            CairoleDebugOut((DEB_SCM, "OUT DLL: %ws\n", *ppwszDllPath));
        }
    }

#endif // DBG == 1

    if (pifdFromROT != NULL)
    {
        MIDL_user_free(pifdFromROT);
    }

    return hr;
}

//+-------------------------------------------------------------------------
//
//  Function:   SvcCreateActivateObject
//
//  Synopsis:   Create and activate a new object
//
//  Arguments:  [hRpc] - RPC handle
//              [orpcthis] - ORPC handle
//              [localthis] - ORPC call data
//              [orpcthat] - ORPC reply data
//              [rclsid] - class id for object to create
//              [dwOptions] - type of server required
//              [dwMode] - how to open storage
//              [pwszPath] - path to the storage
//              [pIFDstg] - marshaled storage
//              [pwszNewName] - new name of object
//              [ppIFDunk] - marshaled IUnknown to return
//              [ppwszDllPath] - path to DLL for handler or server
//
//  Returns:    HRESULT
//
//  History:    01-May-93 Ricksa    Created
//              18-Aug-94 AlexT     Add caller, callee thread id
//
//--------------------------------------------------------------------------

// BUGBUG [mikese] The pwszServerAddress parameter is redundant, and should
//  be removed from the IDL

extern "C" HRESULT SvcCreateActivateObject(
    handle_t hRpc,
    ORPCTHIS *orpcthis,
    LOCALTHIS *localthis,
    ORPCTHAT *orpcthat,
    WCHAR *  lpDesktop,
    WCHAR *pwszProtseq,
    const GUID *guidCLSID,
    DWORD dwOptions,
    DWORD dwMode,
    WCHAR *pwszPath,
    InterfaceData *pIFDstg,
    WCHAR *pwszNewName,
    InterfaceData **ppIFDunk,
    DWORD *pdwDllType,
    WCHAR **ppwszDllPath,
    WCHAR *pwszServerAddress )
{
    REFCLSID rclsid = *guidCLSID;

#if DBG ==1

    CairoleDebugOut((DEB_SCM, "_IN SvcCreateActivateObject\n"));
    CairoleDebugOut((DEB_SCM, "_IN Desk Top: %s\n", lpDesktop));
    CairoleDebugOut((DEB_SCM, "_IN Protocol Seq: %ws\n",
       (pwszProtseq == NULL) ? L"None" : pwszProtseq));
    DbgPrintGuid("_IN ThreadId: ", &orpcthis->cid);
    DbgPrintGuid("_IN CLSID: ", guidCLSID);
    CairoleDebugOut((DEB_SCM, "_IN Context: %lX\n", dwOptions));
    CairoleDebugOut((DEB_SCM, "_IN Mode: %lX\n", dwMode));
    CairoleDebugOut((DEB_SCM, "_IN Path: %ws\n",
       (pwszPath == NULL) ? L"None" : pwszPath));
    CairoleDebugOut((DEB_SCM, "_IN Stg: %lX\n", pIFDstg));
    CairoleDebugOut((DEB_SCM, "_IN New Path: %ws\n",
       (pwszNewName == NULL) ? L"None" : pwszNewName));
    CairoleDebugOut((DEB_SCM, "_IN DLL Type: %lX\n", *pdwDllType));

#endif // DBG == 1

    orpcthat->flags      = 0;
    orpcthat->extensions = NULL;

    VDATEHEAP();

    // Error return from function
    HRESULT hr=S_OK;

    // Lock the path so that only one path may be bound at a time. Note
    // that for create we don't look in the ROT since we don't want to
    // get an already running entry but create a new one.
    CLockPath lckpath(pwszPath, hr);

    if (FAILED(hr))
    {
        // Couldn't lock the path -- memory problem maybe. Anyway, we
        // can't go anywhere from here.
        CairoleDebugOut((DEB_ERROR, "GetClass Failed %lx\n", hr));

        return hr;
    }

    // BUGBUG - Until we can decide how to pass back the callee's thread
    // id, just hold it here.
    DWORD   dwTIDCallee;
    IID     IIDs[1];
    HRESULT Results[1];

    IIDs[0] = IID_IUnknown;

    CGetCreateData gcd(pwszProtseq, dwOptions, dwMode, pwszPath, pIFDstg, pwszNewName,
        ppIFDunk, localthis->dwClientThread, &dwTIDCallee, NULL,
        1, IIDs, Results );

    CBaseData bd(CREATEPERSISTENTOBJ, lpDesktop, &orpcthis->cid, rclsid,
        dwOptions, pdwDllType, ppwszDllPath, ppIFDunk,
        NULL, FALSE, NULL, NULL, NULL, NULL,FALSE);

    hr = GetClassCache().ProcessScmMessage(&bd, &gcd);

    CairoleDebugOut((DEB_TRACE,
        "Result %lx\nDll Path %ws\n", hr, *ppwszDllPath));

    VDATEHEAP();

#if DBG == 1

    CairoleDebugOut((DEB_SCM, "OUT SvcActivateObject\n"));
    CairoleDebugOut((DEB_SCM, "OUT Hresult : %lx\n", hr));

    if (SUCCEEDED(hr))
    {
        if (*ppIFDunk)
        {
            DbgPrintIFD("OUT Object:", *ppIFDunk);
        }

        if (*ppwszDllPath)
        {
            CairoleDebugOut((DEB_SCM, "OUT DLL Type: %lX\n", *pdwDllType));
            CairoleDebugOut((DEB_SCM, "OUT DLL: %ws\n", *ppwszDllPath));
        }
    }

#endif // DBG == 1

    return hr;
}
#endif // _CHICAGO_

#ifdef DCOM
//+-------------------------------------------------------------------------
//
//  Structure:  WIPEntry  (Window Interface Property)
//
//  Synopsis:   Stores marshaled interface info associated with a given window.
//              It is an entry in the CWIPTable (see below).
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
typedef struct tagWIPEntry
{
    DWORD       hWnd;           // window the interface property is stored in
    DWORD       dwFlags;        // flags (see WIPFLAGS)
    STDOBJREF   std;            // marshaled interface data
    OXID_INFO   oxidInfo;       // data needed to resolve the OXID
} WIPEntry;

typedef enum tagWIPFLAGS
{
    WIPF_VACANT     = 0x1,      // slot is vacant
    WIPF_OCCUPIED   = 0x2       // slot is occupied
} WIPFLAGS;

#define WIPTBL_GROW_SIZE    10  // grow table by 10 entries at a time


//+-------------------------------------------------------------------------
//
//  Class:      CWIPTable (Window Interface Property Table)
//
//  Synopsis:   Stores marshaled interface info associated with a given window.
//              This is used for registering drag/drop interfaces.
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
class CWIPTable
{
public:
    HRESULT AddEntry(DWORD hWnd, STDOBJREF *pStd, OXID_INFO *pOxidInfo, DWORD *pdwCookie);
    HRESULT GetEntry(DWORD hWnd, DWORD dwCookie, BOOL fRevoke,
                     STDOBJREF *pStd, OXID_INFO *pOxidInfo);

private:
    DWORD   Grow();

    static  DWORD       s_cEntries;     // count of entries in the table
    static  DWORD       s_iNextFree;    // index to first free entry in table
    static  WIPEntry   *s_pTbl;         // ptr to the first entry in table
    static  CMutexSem   s_mxs;          // critical section to protect data
};

// static data for the table (avoids running a ctor for the class)
DWORD     CWIPTable::s_cEntries  = 0;
DWORD     CWIPTable::s_iNextFree = 0xffffffff;
WIPEntry *CWIPTable::s_pTbl      = NULL;
CMutexSem CWIPTable::s_mxs;

CWIPTable gWIPTbl;  // global instance of the class


//+-------------------------------------------------------------------------
//
//  Function:   CopyDualStringArray
//
//  Synopsis:   makes a copy of the given string array
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
HRESULT CopyDualStringArray(DUALSTRINGARRAY *psa, DUALSTRINGARRAY **ppsaNew)
{
    ULONG ulSize = sizeof(DUALSTRINGARRAY) + (psa->wNumEntries * sizeof(WCHAR));

    *ppsaNew = (DUALSTRINGARRAY *) PrivMemAlloc(ulSize);

    if (*ppsaNew == NULL)
    {
        return E_OUTOFMEMORY;
    }

    memcpy(*ppsaNew, psa, ulSize);
    return S_OK;
}

//+-------------------------------------------------------------------------
//
//  Member:     CWIPTable::AddEntry, public
//
//  Synopsis:   Adds a WIPEntry to the table.
//
//  Arguments:  [hWnd] - window handle
//              [pStd] - standard marshaled interface STDOBJREF
//              [pOxidInfo] - info needed to resolve the OXID
//              [pdwCookie] - cookie to return (to be placed on the window)
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
HRESULT CWIPTable::AddEntry(DWORD hWnd, STDOBJREF *pStd, OXID_INFO *pOxidInfo,
                            DWORD *pdwCookie)
{
    // make a copy of the string array in the OxidInfo since MIDL will
    // delete it on the way back out of the call.

    DUALSTRINGARRAY *psaNew;
    HRESULT hr = CopyDualStringArray(pOxidInfo->psa, &psaNew);
    if (FAILED(hr))
    {
        return hr;
    }

    CLock lck(s_mxs);

    // find a free slot in the table
    DWORD dwIndex = s_iNextFree;

    if (dwIndex == 0xffffffff)
    {
        // grow the table
        dwIndex = Grow();
    }

    if (dwIndex != 0xffffffff)
    {
        // get the pointer to the entry,
        WIPEntry *pEntry = s_pTbl + dwIndex;

        // update the next free index.
        s_iNextFree = pEntry->hWnd;

        // copy in the data
        memcpy(&pEntry->std, pStd, sizeof(STDOBJREF));
        memcpy(&pEntry->oxidInfo, pOxidInfo, sizeof(OXID_INFO));

        pEntry->oxidInfo.psa = psaNew;
        pEntry->hWnd         = hWnd;
        pEntry->dwFlags      = WIPF_OCCUPIED;

        // return success
        hr = S_OK;
    }
    else
    {
        // free the allocated string array
        PrivMemFree(psaNew);
    }

    // set the cookie to return
    *pdwCookie = dwIndex+5000;
    return hr;
}

//+-------------------------------------------------------------------------
//
//  Member:     CWIPTable::GetEntry, public
//
//  Synopsis:   Retrieves and optionally delets a WIPEntry from the table.
//
//  Arguments:  [hWnd] - window handle
//              [dwCookie] - cookie from the window
//              [pStd] - place to return STDOBJREF data
//              [pOxidInfo] - place to return info needed to resolve the OXID
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
HRESULT CWIPTable::GetEntry(DWORD hWnd, DWORD dwCookie, BOOL fRevoke,
                            STDOBJREF *pStd, OXID_INFO *pOxidInfo)
{
    HRESULT hr = E_INVALIDARG;

    // validate the cookie
    DWORD dwIndex = dwCookie - 5000;
    if (dwIndex < 0 || dwIndex >= s_cEntries)
    {
        return hr;
    }

    CLock lck(s_mxs);

    // get the pointer to the entry,
    WIPEntry *pEntry = s_pTbl + dwIndex;

    // make sure the entry is occupied
    if (pEntry->dwFlags & WIPF_OCCUPIED)
    {
        DUALSTRINGARRAY *psaNew;
        hr = CopyDualStringArray(pEntry->oxidInfo.psa, &psaNew);

        if (SUCCEEDED(hr))
        {
            // copy out the data to return
            memcpy(pStd, &pEntry->std, sizeof(STDOBJREF));
            memcpy(pOxidInfo, &pEntry->oxidInfo, sizeof(OXID_INFO));
            pOxidInfo->psa = psaNew;

            if (fRevoke)
            {
                // free the entry by updating the flags and the next free index
                PrivMemFree(pEntry->oxidInfo.psa);

                pEntry->dwFlags = WIPF_VACANT;
                pEntry->hWnd    = s_iNextFree;
                s_iNextFree         = dwIndex;
            }
        }
    }

    return hr;
}

//+-------------------------------------------------------------------------
//
//  Member:     CWIPTable::Grow, private
//
//  Synopsis:   grows the WIPTable size.
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
DWORD CWIPTable::Grow()
{
    // compute the size and allocate a new table
    DWORD dwSize = (s_cEntries + WIPTBL_GROW_SIZE) * sizeof(WIPEntry);
    WIPEntry *pNewTbl = (WIPEntry *) PrivMemAlloc(dwSize);

    if (pNewTbl != NULL)
    {
        // copy the old table in
        memcpy(pNewTbl, s_pTbl, (s_cEntries * sizeof(WIPEntry)));

        // free the old table
        if (s_pTbl)
        {
            PrivMemFree(s_pTbl);
        }

        // replace the old table ptr
        s_pTbl = pNewTbl;

        // update the free list and mark the new entries as vacant
        s_iNextFree = s_cEntries;

        WIPEntry *pNext = s_pTbl + s_cEntries;

        for (ULONG i=0; i< WIPTBL_GROW_SIZE; i++)
        {
            pNext->hWnd    = ++s_cEntries;
            pNext->dwFlags = WIPF_VACANT;
            pNext++;
        }

        (pNext-1)->hWnd = 0xffffffff;   // last entry has END_OF_LIST marker
    }

    return s_iNextFree;
}

//+-------------------------------------------------------------------------
//
//  Function:   RegisterWindowPropInterface
//
//  Synopsis:   Associate a window property with a (standard) marshaled
//              interface.
//
//  Arguments:  [hRpc] - RPC handle
//              [hWnd] - window handle
//              [pStd] - standard marshaled interface STDOBJREF
//              [pOxidInfo] - info needed to resolve the OXID
//              [pdwCookie] - cookie to return (to be placed on the window)
//              [prpcstat] - communication status
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT RegisterWindowPropInterface(
    handle_t       hRpc,
    DWORD          hWnd,
    STDOBJREF      *pStd,
    OXID_INFO      *pOxidInfo,
    DWORD          *pdwCookie,
    error_status_t *prpcstat)
{
    CheckLocalCall( hRpc );

    *prpcstat = 0;
    CairoleDebugOut((DEB_SCM,
        "_IN RegisterWindowPropInterface hWnd:%x pStd:%x pOxidInfo:%x\n",
         hWnd, pStd, pOxidInfo));
    VDATEHEAP();

    HRESULT hr = gWIPTbl.AddEntry(hWnd, pStd, pOxidInfo, pdwCookie);

    CairoleDebugOut((DEB_SCM, "_OUT RegisterWindowPropInterface dwCookie:%x\n",
                    *pdwCookie));
    VDATEHEAP();
    return hr;
}

//+-------------------------------------------------------------------------
//
//  Function:   GetWindowPropInterface
//
//  Synopsis:   Get the marshaled interface associated with a window property.
//
//  Arguments:  [hRpc] - RPC handle
//              [hWnd] - window handle
//              [dwCookie] - cookie from the window
//              [fRevoke] - whether to revoke entry or not
//              [pStd] - standard marshaled interface STDOBJREF to return
//              [pOxidInfo] - info needed to resolve the OXID
//              [prpcstat] - communication status
//
//  History:    22-Jan-96 Rickhi    Created
//
//--------------------------------------------------------------------------
extern "C" HRESULT GetWindowPropInterface(
    handle_t       hRpc,
    DWORD          hWnd,
    DWORD          dwCookie,
    BOOL           fRevoke,
    STDOBJREF      *pStd,
    OXID_INFO      *pOxidInfo,
    error_status_t *prpcstat)
{
    CheckLocalCall( hRpc );

    *prpcstat = 0;
    CairoleDebugOut((DEB_SCM,
        "_IN GetWindowPropInterface hWnd:%x dwCookie:%x fRevoke:%x\n",
             hWnd, dwCookie, fRevoke));
    VDATEHEAP();

    HRESULT hr = gWIPTbl.GetEntry(hWnd, dwCookie, fRevoke, pStd, pOxidInfo);

    CairoleDebugOut((DEB_SCM,
        "_OUT GetWindowPropInterface pStd:%x pOxidInfo:%x\n", pStd, pOxidInfo));
    VDATEHEAP();
    return hr;
}

#endif // DCOM