summaryrefslogtreecommitdiff
path: root/src/binder/binderinterface.cpp
blob: 4e5f9b42b39b1597eebb00ac84ae98b4204e5a65 (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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
// 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.
// ============================================================
//
// BinderInterface.cpp
//


//
// Implements the public AssemblyBinder interface
//
// ============================================================

#include "assemblybinder.hpp"
#include "assemblyname.hpp"
#include "applicationcontext.hpp"
#include "binderinterface.hpp"
#include "bindresult.inl"
#include "utils.hpp"

#include "ex.h"

using namespace BINDER_SPACE;

namespace BinderInterface
{

    HRESULT Init()
    {
        HRESULT hr = S_OK;
        
        EX_TRY
        {
            hr = AssemblyBinder::Startup();
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    HRESULT SetupContext(LPCWSTR    wszApplicationBase,
                         DWORD      dwAppDomainId,
                         IUnknown **ppIApplicationContext)
    {
        HRESULT hr = S_OK;
        
        EX_TRY
        {
            BINDER_LOG_LOCK();
            BINDER_LOG_ENTER(L"BinderInterface::SetupContext");

            // Verify input arguments
            IF_FALSE_GO(ppIApplicationContext != NULL);

            {
                ReleaseHolder<ApplicationContext> pApplicationContext;

                SAFE_NEW(pApplicationContext, ApplicationContext);
                IF_FAIL_GO(pApplicationContext->Init());
                pApplicationContext->SetAppDomainId(dwAppDomainId);
                *ppIApplicationContext = static_cast<IUnknown *>(pApplicationContext.Extract());
            }

        Exit:
            BINDER_LOG_LEAVE_HR(L"BinderInterface::SetupContext", hr);
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    // See code:BINDER_SPACE::AssemblyBinder::GetAssembly for info on fNgenExplicitBind
    // and fExplicitBindToNativeImage, and see code:CEECompileInfo::LoadAssemblyByPath
    // for an example of how they're used.
    HRESULT Bind(IUnknown    *pIApplicationContext,
                 SString     &assemblyDisplayName,
                 LPCWSTR      wszCodeBase,
                 PEAssembly  *pParentAssembly,
                 BOOL         fNgenExplicitBind,
                 BOOL         fExplicitBindToNativeImage,
                 BINDER_SPACE::Assembly   **ppAssembly)
    {
        HRESULT hr = S_OK;

        EX_TRY
        {
            BINDER_LOG_LOCK();
            BINDER_LOG_ENTER(L"BinderInterface::Bind");
            
            // Verify input arguments
            IF_FALSE_GO(pIApplicationContext != NULL);
            IF_FALSE_GO(ppAssembly != NULL);

            {
                ApplicationContext *pApplicationContext =
                    static_cast<ApplicationContext *>(pIApplicationContext);

                ReleaseHolder<AssemblyName> pAssemblyName;
                if (!assemblyDisplayName.IsEmpty())
                {
                    SAFE_NEW(pAssemblyName, AssemblyName);
                    IF_FAIL_GO(pAssemblyName->Init(assemblyDisplayName));
                }
                
                IF_FAIL_GO(AssemblyBinder::BindAssembly(pApplicationContext,
                                                        pAssemblyName,
                                                        wszCodeBase,
                                                        pParentAssembly,
                                                        fNgenExplicitBind,
                                                        fExplicitBindToNativeImage,
                                                        false, // excludeAppPaths
                                                        ppAssembly));
            }

        Exit:
            BINDER_LOG_LEAVE_HR(L"BinderInterface::Bind", hr);
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

    HRESULT BindToSystem(SString   &sSystemDirectory,
                         BINDER_SPACE::Assembly **ppSystemAssembly,
                         bool fBindToNativeImage)
    {
        HRESULT hr = S_OK;

        IF_FALSE_GO(ppSystemAssembly != NULL);

        EX_TRY
        {
            BINDER_LOG_LOCK();

            IF_FAIL_GO(AssemblyBinder::BindToSystem(sSystemDirectory, ppSystemAssembly, fBindToNativeImage));
        }
        EX_CATCH_HRESULT(hr);

    Exit:
        return hr;
    }

    HRESULT SetupBindingPaths(IUnknown *pIApplicationContext,
                            SString &sTrustedPlatformAssemblies,
                            SString &sPlatformResourceRoots,
                            SString &sAppPaths,
                            SString &sAppNiPaths)
    {
        HRESULT hr = S_OK;

        EX_TRY
        {
            BINDER_LOG_LOCK();
            BINDER_LOG_ENTER(L"BinderInterface::SetupBindingPaths");

            // Verify input arguments
            IF_FALSE_GO(pIApplicationContext != NULL);

            {
                ApplicationContext *pApplicationContext =
                    static_cast<ApplicationContext *>(pIApplicationContext);
                _ASSERTE(pApplicationContext != NULL);

                IF_FAIL_GO(pApplicationContext->SetupBindingPaths(sTrustedPlatformAssemblies, sPlatformResourceRoots, sAppPaths, sAppNiPaths, TRUE /* fAcquireLock */));
            }

        Exit:
            BINDER_LOG_LEAVE_HR(L"BinderInterface::SetupBindingPaths", hr);
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }

#ifdef BINDER_DEBUG_LOG
    HRESULT Log(LPCWSTR wszMessage)
    {
        HRESULT hr = S_OK;
        
        EX_TRY
        {
            BINDER_LOG_LOCK();
            BINDER_LOG((WCHAR *) wszMessage);
        }
        EX_CATCH_HRESULT(hr);

        return hr;
    }
#endif // BINDER_DEBUG_LOG
};