@@ -42,21 +42,16 @@ CClrHost::~CClrHost()
42
42
#pragma warning( disable : 4996 )
43
43
HRESULT CClrHost::FinalConstruct ()
44
44
{
45
- // load the CLR into the process
46
- /* return CorBindToRuntimeEx(NULL,
47
- NULL,
48
- 0,
49
- CLSID_CLRRuntimeHost,
50
- IID_ICLRRuntimeHost,
51
- reinterpret_cast<LPVOID *>(&m_pClr));*/
52
45
53
46
// load the CLR into the process
47
+ // First we get an instance of the MetaHost
54
48
ICLRMetaHost *pMetaHost = NULL ;
55
49
ICLRMetaHostPolicy *pMetaHostPolicy = NULL ;
56
50
ICLRDebugging *pCLRDebugging = NULL ;
57
51
HRESULT hr;
58
52
hr = CLRCreateInstance (CLSID_CLRMetaHost, IID_ICLRMetaHost,
59
53
(LPVOID*)&pMetaHost);
54
+
60
55
/* hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy,
61
56
(LPVOID*)&pMetaHostPolicy);*/
62
57
/* hr = CLRCreateInstance(CLSID_CLRDebugging, IID_ICLRDebugging,
@@ -72,6 +67,10 @@ HRESULT CClrHost::FinalConstruct()
72
67
WCHAR strName[128 ];
73
68
DWORD len = 128 ;
74
69
70
+ // If for whatever reason we end up here, we need to check to see what Runtimes are loaded
71
+ // This will set the default runtime as the last CLR to be loaded.
72
+ // At the time of writing this application it is 4.5 (4.0)
73
+
75
74
while ((hr = pRtEnum->Next (1 , (IUnknown **)&info, &fetched)) == S_OK && fetched > 0 )
76
75
{
77
76
ZeroMemory (strName, sizeof (strName));
@@ -81,12 +80,41 @@ HRESULT CClrHost::FinalConstruct()
81
80
reinterpret_cast <LPVOID *>(&m_pClr));
82
81
if (!SUCCEEDED (hr))
83
82
printf (" hr failed...." );
84
- runtimesLoaded = true ;
83
+
84
+ HRESULT hrClrControl = m_pClr->GetCLRControl (&m_pClrControl);
85
+ if (FAILED (hrClrControl))
86
+ return hrClrControl;
87
+
88
+ // set ourselves up as the host control
89
+ HRESULT hrHostControl = m_pClr->SetHostControl (static_cast <IHostControl *>(this ));
90
+ if (FAILED (hrHostControl))
91
+ return hrHostControl;
92
+
93
+ // get the host protection manager
94
+ ICLRHostProtectionManager *pHostProtectionManager = NULL ;
95
+ HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager (
96
+ IID_ICLRHostProtectionManager,
97
+ reinterpret_cast <void **>(&pHostProtectionManager));
98
+ if (FAILED (hrGetProtectionManager))
99
+ return hrGetProtectionManager;
100
+
101
+ // setup host proctection
102
+ HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories (
103
+ (EApiCategories)(eSynchronization | eSelfAffectingThreading));
104
+ pHostProtectionManager->Release ();
105
+
106
+ if (FAILED (hrHostProtection))
107
+ return hrHostProtection;
108
+
85
109
this ->m_lastCLR .assign (strName);
110
+ runtimesLoaded = true ;
86
111
}
87
112
pRtEnum->Release ();
88
113
pRtEnum = NULL ;
89
114
115
+ // If no runtimes are loaded we will make sure to load them all.
116
+ // This will set the default runtime as the last CLR to be loaded.
117
+ // At the time of writing this application it is 4.5 (4.0)
90
118
if (!runtimesLoaded)
91
119
{
92
120
pMetaHost->EnumerateInstalledRuntimes (&pRtEnum);
@@ -101,6 +129,36 @@ HRESULT CClrHost::FinalConstruct()
101
129
if (!SUCCEEDED (hr))
102
130
printf (" hr failed...." );
103
131
m_CLRRuntimeMap[std::wstring (strName)] = m_pClr;
132
+
133
+
134
+ HRESULT hrClrControl = m_pClr->GetCLRControl (&m_pClrControl);
135
+ if (FAILED (hrClrControl))
136
+ return hrClrControl;
137
+
138
+ // set ourselves up as the host control
139
+ HRESULT hrHostControl = m_pClr->SetHostControl (static_cast <IHostControl *>(this ));
140
+ if (FAILED (hrHostControl))
141
+
142
+ return hrHostControl;
143
+ // get the host protection manager
144
+ ICLRHostProtectionManager *pHostProtectionManager = NULL ;
145
+ HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager (
146
+ IID_ICLRHostProtectionManager,
147
+ reinterpret_cast <void **>(&pHostProtectionManager));
148
+ if (FAILED (hrGetProtectionManager))
149
+ return hrGetProtectionManager;
150
+
151
+ // setup host proctection to disallow any threading from partially trusted code.
152
+ // Why? well, if a thread is allowed to hang indefinitely the command could get stuck.
153
+ HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories (
154
+ (EApiCategories)(eSynchronization | eSelfAffectingThreading | eSelfAffectingProcessMgmt
155
+ | eExternalProcessMgmt | eExternalThreading | eUI));
156
+ pHostProtectionManager->Release ();
157
+
158
+ if (FAILED (hrHostProtection))
159
+ return hrHostProtection;
160
+
161
+
104
162
this ->m_lastCLR .assign (strName);
105
163
}
106
164
pRtEnum->Release ();
@@ -139,13 +197,6 @@ STDMETHODIMP CClrHost::GetHostManager(const IID &RIID, __deref_opt_out_opt void
139
197
if (ppvObject == NULL )
140
198
return E_POINTER;
141
199
142
- if (RIID == IID_IHostGCManager)
143
- {
144
- *ppvObject = (IHostGCManager*)this ;
145
- return S_OK;
146
- }
147
-
148
-
149
200
*ppvObject = NULL ;
150
201
return E_NOINTERFACE;
151
202
}
@@ -319,6 +370,8 @@ STDMETHODIMP CClrHost::ThreadIsBlockingForSuspension(){ return S_OK; }
319
370
STDMETHODIMP CClrHost::raw_CreateAppDomainForQuery (BSTR FnName, BSTR *pRetVal)
320
371
{
321
372
IManagedHostPtr pAppMgr = this ->GetDefaultManagedHost ();
373
+ auto clrVersion = pAppMgr->GetAssemblyCLRVersion (FnName);
374
+
322
375
IManagedHostPtr pNewDomain = pAppMgr->CreateAppDomain (FnName);
323
376
*pRetVal = (BSTR)pNewDomain->GetAppDomainName ;
324
377
this ->m_NewlyCreatedAppDomains [std::wstring (*pRetVal)] = pNewDomain;
0 commit comments