summaryrefslogtreecommitdiff
path: root/src/vm/olecontexthelpers.cpp
blob: 43dc318d78891d892fb0b1495fedb384985c5242 (plain)
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.


#include "common.h"

#ifdef FEATURE_COMINTEROP_APARTMENT_SUPPORT

#include "mtx.h"
#include "oletls.h"
#include "contxt.h"
#include "ctxtcall.h"

HRESULT GetCurrentObjCtx(IUnknown **ppObjCtx)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        PRECONDITION(CheckPointer(ppObjCtx));
#ifdef FEATURE_COMINTEROP
        PRECONDITION(TRUE == g_fComStarted);
#endif // FEATURE_COMINTEROP
    }
    CONTRACTL_END;

    return CoGetObjectContext(IID_IUnknown, (void **)ppObjCtx);
}

//=====================================================================
// LPVOID SetupOleContext()
LPVOID SetupOleContext()
{
    CONTRACT (LPVOID)
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        ENTRY_POINT;
        POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
    }
    CONTRACT_END;
    
    IUnknown* pObjCtx = NULL;

    BEGIN_ENTRYPOINT_VOIDRET;
    
#ifdef FEATURE_COMINTEROP    
    if (g_fComStarted)
    {               
        HRESULT hr = GetCurrentObjCtx(&pObjCtx);
        if (hr == S_OK)
        {
            SOleTlsData* _pData = (SOleTlsData *) ClrTeb::GetOleReservedPtr();
            if (_pData && _pData->pCurrentCtx == NULL)
            {
                _pData->pCurrentCtx = (CObjectContext*)pObjCtx;   // no release !!!!
            }
            else
            {
                // We can't call SafeRelease here since that would transition 
                // to preemptive GC mode which is bad since SetupOleContext is called 
                // from places where we can't take a GC.
                ULONG cbRef = pObjCtx->Release();
            }
        }
    }
#endif // FEATURE_COMINTEROP

    END_ENTRYPOINT_VOIDRET;

    RETURN pObjCtx;
}

//================================================================
// LPVOID GetCurrentCtxCookie()
LPVOID GetCurrentCtxCookie()
{
    CONTRACT (LPVOID)
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        SO_TOLERANT;
        POSTCONDITION(CheckPointer(RETVAL, NULL_OK));
    }
    CONTRACT_END;
    
#ifdef FEATURE_COMINTEROP
    // check if com is started
    if (!g_fComStarted)
        RETURN NULL;
#endif // FEATURE_COMINTEROP

    ULONG_PTR ctxptr = 0;

    if (CoGetContextToken(&ctxptr) != S_OK)
        ctxptr = 0;

    RETURN (LPVOID)ctxptr;
}

//+-------------------------------------------------------------------------
//
//  HRESULT GetCurrentThreadTypeNT5(THDTYPE* pType)
// 
HRESULT GetCurrentThreadTypeNT5(THDTYPE* pType)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        SO_TOLERANT;
        PRECONDITION(CheckPointer(pType));
    }
    CONTRACTL_END;

    HRESULT hr = E_FAIL;
    
    IObjectContext *pObjCurrCtx = (IObjectContext *)GetCurrentCtxCookie();
    if(pObjCurrCtx)
    {
        GCX_PREEMP();
        
        SafeComHolderPreemp<IComThreadingInfo> pThreadInfo;
        hr = SafeQueryInterface(pObjCurrCtx, IID_IComThreadingInfo, (IUnknown **)&pThreadInfo);
        if(hr == S_OK)
        {
            _ASSERTE(pThreadInfo);
            hr = pThreadInfo->GetCurrentThreadType(pType);
        }
    }  
    return hr;
}

//+-------------------------------------------------------------------------
//
//  HRESULT GetCurrentApartmentTypeNT5(APTTYPE* pType)
// 
HRESULT GetCurrentApartmentTypeNT5(IObjectContext *pObjCurrCtx, APTTYPE* pType)
{
    CONTRACTL
    {
        NOTHROW;
        GC_TRIGGERS;
        MODE_ANY;
        PRECONDITION(CheckPointer(pType));
    }
    CONTRACTL_END;

    HRESULT hr = E_FAIL;
    if(pObjCurrCtx)
    {
        GCX_PREEMP();
        
        SafeComHolderPreemp<IComThreadingInfo> pThreadInfo;
        hr = SafeQueryInterface(pObjCurrCtx, IID_IComThreadingInfo, (IUnknown **)&pThreadInfo);
        if(hr == S_OK)
        {
            _ASSERTE(pThreadInfo);
            hr = pThreadInfo->GetCurrentApartmentType(pType);
        }
    }  
    return hr;
}

#endif // FEATURE_COMINTEROP_APARTMENT_SUPPORT