1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4// ============================================================
5//
6// BinderInterface.cpp
7//
8
9
10//
11// Implements the public AssemblyBinder interface
12//
13// ============================================================
14
15#include "assemblybinder.hpp"
16#include "assemblyname.hpp"
17#include "applicationcontext.hpp"
18#include "binderinterface.hpp"
19#include "bindresult.inl"
20#include "utils.hpp"
21
22#include "ex.h"
23
24using namespace BINDER_SPACE;
25
26namespace BinderInterface
27{
28
29 HRESULT Init()
30 {
31 HRESULT hr = S_OK;
32
33 EX_TRY
34 {
35 hr = AssemblyBinder::Startup();
36 }
37 EX_CATCH_HRESULT(hr);
38
39 return hr;
40 }
41
42 HRESULT SetupContext(LPCWSTR wszApplicationBase,
43 DWORD dwAppDomainId,
44 IUnknown **ppIApplicationContext)
45 {
46 HRESULT hr = S_OK;
47
48 EX_TRY
49 {
50 BINDER_LOG_LOCK();
51 BINDER_LOG_ENTER(L"BinderInterface::SetupContext");
52
53 // Verify input arguments
54 IF_FALSE_GO(ppIApplicationContext != NULL);
55
56 {
57 ReleaseHolder<ApplicationContext> pApplicationContext;
58
59 SAFE_NEW(pApplicationContext, ApplicationContext);
60 IF_FAIL_GO(pApplicationContext->Init());
61 pApplicationContext->SetAppDomainId(dwAppDomainId);
62 *ppIApplicationContext = static_cast<IUnknown *>(pApplicationContext.Extract());
63 }
64
65 Exit:
66 BINDER_LOG_LEAVE_HR(L"BinderInterface::SetupContext", hr);
67 }
68 EX_CATCH_HRESULT(hr);
69
70 return hr;
71 }
72
73 // See code:BINDER_SPACE::AssemblyBinder::GetAssembly for info on fNgenExplicitBind
74 // and fExplicitBindToNativeImage, and see code:CEECompileInfo::LoadAssemblyByPath
75 // for an example of how they're used.
76 HRESULT Bind(IUnknown *pIApplicationContext,
77 SString &assemblyDisplayName,
78 LPCWSTR wszCodeBase,
79 PEAssembly *pParentAssembly,
80 BOOL fNgenExplicitBind,
81 BOOL fExplicitBindToNativeImage,
82 BINDER_SPACE::Assembly **ppAssembly)
83 {
84 HRESULT hr = S_OK;
85
86 EX_TRY
87 {
88 BINDER_LOG_LOCK();
89 BINDER_LOG_ENTER(L"BinderInterface::Bind");
90
91 // Verify input arguments
92 IF_FALSE_GO(pIApplicationContext != NULL);
93 IF_FALSE_GO(ppAssembly != NULL);
94
95 {
96 ApplicationContext *pApplicationContext =
97 static_cast<ApplicationContext *>(pIApplicationContext);
98
99 ReleaseHolder<AssemblyName> pAssemblyName;
100 if (!assemblyDisplayName.IsEmpty())
101 {
102 SAFE_NEW(pAssemblyName, AssemblyName);
103 IF_FAIL_GO(pAssemblyName->Init(assemblyDisplayName));
104 }
105
106 IF_FAIL_GO(AssemblyBinder::BindAssembly(pApplicationContext,
107 pAssemblyName,
108 wszCodeBase,
109 pParentAssembly,
110 fNgenExplicitBind,
111 fExplicitBindToNativeImage,
112 false, // excludeAppPaths
113 ppAssembly));
114 }
115
116 Exit:
117 BINDER_LOG_LEAVE_HR(L"BinderInterface::Bind", hr);
118 }
119 EX_CATCH_HRESULT(hr);
120
121 return hr;
122 }
123
124 HRESULT BindToSystem(SString &sSystemDirectory,
125 BINDER_SPACE::Assembly **ppSystemAssembly,
126 bool fBindToNativeImage)
127 {
128 HRESULT hr = S_OK;
129
130 IF_FALSE_GO(ppSystemAssembly != NULL);
131
132 EX_TRY
133 {
134 BINDER_LOG_LOCK();
135
136 IF_FAIL_GO(AssemblyBinder::BindToSystem(sSystemDirectory, ppSystemAssembly, fBindToNativeImage));
137 }
138 EX_CATCH_HRESULT(hr);
139
140 Exit:
141 return hr;
142 }
143
144 HRESULT SetupBindingPaths(IUnknown *pIApplicationContext,
145 SString &sTrustedPlatformAssemblies,
146 SString &sPlatformResourceRoots,
147 SString &sAppPaths,
148 SString &sAppNiPaths)
149 {
150 HRESULT hr = S_OK;
151
152 EX_TRY
153 {
154 BINDER_LOG_LOCK();
155 BINDER_LOG_ENTER(L"BinderInterface::SetupBindingPaths");
156
157 // Verify input arguments
158 IF_FALSE_GO(pIApplicationContext != NULL);
159
160 {
161 ApplicationContext *pApplicationContext =
162 static_cast<ApplicationContext *>(pIApplicationContext);
163 _ASSERTE(pApplicationContext != NULL);
164
165 IF_FAIL_GO(pApplicationContext->SetupBindingPaths(sTrustedPlatformAssemblies, sPlatformResourceRoots, sAppPaths, sAppNiPaths, TRUE /* fAcquireLock */));
166 }
167
168 Exit:
169 BINDER_LOG_LEAVE_HR(L"BinderInterface::SetupBindingPaths", hr);
170 }
171 EX_CATCH_HRESULT(hr);
172
173 return hr;
174 }
175
176#ifdef BINDER_DEBUG_LOG
177 HRESULT Log(LPCWSTR wszMessage)
178 {
179 HRESULT hr = S_OK;
180
181 EX_TRY
182 {
183 BINDER_LOG_LOCK();
184 BINDER_LOG((WCHAR *) wszMessage);
185 }
186 EX_CATCH_HRESULT(hr);
187
188 return hr;
189 }
190#endif // BINDER_DEBUG_LOG
191};
192