@@ -6,13 +6,18 @@ const wchar_t *CClrHost::AppDomainManagerType = L"mysql_managed_interface.MySQLH
6
6
7
7
bool g_CLRHasBeenLoaded = false ;
8
8
9
+
9
10
// / <summary>
10
11
// / Initialize the host
11
12
// / </summary>
12
13
14
+ class CADMHostModule : public CAtlExeModuleT < CADMHostModule > { };
15
+ CADMHostModule _AtlModule;
13
16
14
- CClrHost::CClrHost () : m_started(false ), m_pClr( NULL ), m_pClrControl(NULL )
17
+ CClrHost::CClrHost () : m_started(false ), m_pClrControl(NULL )
15
18
{
19
+
20
+
16
21
return ;
17
22
}
18
23
@@ -21,16 +26,13 @@ CClrHost::CClrHost() : m_started(false), m_pClr(NULL), m_pClrControl(NULL)
21
26
// / </summary>
22
27
CClrHost::~CClrHost ()
23
28
{
24
- // free the AppDomainManagers
25
- for (AppDomainManagerMap::iterator iAdm = m_appDomainManagers.begin (); iAdm != m_appDomainManagers.end (); iAdm++)
26
- iAdm->second ->Release ();
27
-
28
- // release the CLR
29
- if (m_pClrControl != NULL )
30
- m_pClrControl->Release ();
31
- if (m_pClr != NULL )
32
- m_pClr->Release ();
29
+ // // free the AppDomainManagers
30
+ // for (AppDomainManagerMap::iterator iAdm = m_appDomainManagers.begin(); iAdm != m_appDomainManagers.end(); iAdm++)
31
+ // iAdm->second->Release();
33
32
33
+ // // release the CLR
34
+ // if (m_pClrControl != NULL)
35
+ // m_pClrControl->Release();
34
36
return ;
35
37
}
36
38
@@ -41,66 +43,73 @@ CClrHost::~CClrHost()
41
43
HRESULT CClrHost::FinalConstruct ()
42
44
{
43
45
// load the CLR into the process
44
- return CorBindToRuntimeEx (NULL ,
46
+ /* return CorBindToRuntimeEx(NULL,
45
47
NULL,
46
48
0,
47
49
CLSID_CLRRuntimeHost,
48
50
IID_ICLRRuntimeHost,
49
- reinterpret_cast <LPVOID *>(&m_pClr));
50
-
51
- // load the CLR into the process
52
- // ICLRMetaHost *pMetaHost = NULL;
53
- // ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
54
- // ICLRDebugging *pCLRDebugging = NULL;
55
- // HRESULT hr;
56
- // hr = CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost,
57
- // (LPVOID*)&pMetaHost);
58
- // hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy,
59
- // (LPVOID*)&pMetaHostPolicy);
60
- // hr = CLRCreateInstance(CLSID_CLRDebugging, IID_ICLRDebugging,
61
- // (LPVOID*)&pCLRDebugging);
62
-
63
- // // Enumeration example from COM books.
64
- // IEnumUnknown * pRtEnum = NULL;
65
- // ICLRRuntimeInfo *info = NULL;
66
- // ULONG fetched = 0;
67
- // pMetaHost->EnumerateLoadedRuntimes(GetCurrentProcess(), &pRtEnum);
68
- // while ((hr = pRtEnum->Next(1, (IUnknown **)&info, &fetched)) == S_OK && fetched > 0)
69
- // {
70
- // WCHAR strName[128];
71
- // ZeroMemory(strName, sizeof(strName));
72
- // DWORD len = 128;
73
- // info->GetVersionString(strName, &len);
74
- // hr = info->GetInterface(CLSID_CLRRuntimeHost,
75
- // IID_ICLRRuntimeHost,
76
- // reinterpret_cast<LPVOID *>(&m_pClr));
77
- // if (!SUCCEEDED(hr))
78
- // printf("hr failed....");
79
-
80
- // }
81
- // pRtEnum->Release();
82
- // pRtEnum = NULL;
83
-
84
- // pMetaHost->EnumerateInstalledRuntimes(&pRtEnum);
85
- // while ((hr = pRtEnum->Next(1, (IUnknown **)&info, &fetched)) == S_OK && fetched > 0)
86
- // {
87
- // WCHAR strName[128];
88
- // ZeroMemory(strName, sizeof(strName));
89
- // DWORD len = 128;
90
- // info->GetVersionString(strName, &len);
91
- // hr = info->GetInterface(CLSID_CLRRuntimeHost,
92
- // IID_ICLRRuntimeHost,
93
- // reinterpret_cast<LPVOID *>(&m_pClr));
94
- // if (!SUCCEEDED(hr))
95
- // printf("hr failed....");
96
-
97
- // }
98
- // pRtEnum->Release();
99
-
51
+ reinterpret_cast<LPVOID *>(&m_pClr));*/
100
52
53
+ // load the CLR into the process
54
+ ICLRMetaHost *pMetaHost = NULL ;
55
+ ICLRMetaHostPolicy *pMetaHostPolicy = NULL ;
56
+ ICLRDebugging *pCLRDebugging = NULL ;
57
+ HRESULT hr;
58
+ hr = CLRCreateInstance (CLSID_CLRMetaHost, IID_ICLRMetaHost,
59
+ (LPVOID*)&pMetaHost);
60
+ /* hr = CLRCreateInstance(CLSID_CLRMetaHostPolicy, IID_ICLRMetaHostPolicy,
61
+ (LPVOID*)&pMetaHostPolicy);*/
62
+ /* hr = CLRCreateInstance(CLSID_CLRDebugging, IID_ICLRDebugging,
63
+ (LPVOID*)&pCLRDebugging);*/
64
+
65
+ // Enumeration example from COM books.
66
+ IEnumUnknown * pRtEnum = NULL ;
67
+ ICLRRuntimeInfo *info = NULL ;
68
+ ULONG fetched = 0 ;
69
+ ICLRRuntimeHost *m_pClr = NULL ;
70
+ bool runtimesLoaded = false ;
71
+ pMetaHost->EnumerateLoadedRuntimes (GetCurrentProcess (), &pRtEnum);
72
+ WCHAR strName[128 ];
73
+ DWORD len = 128 ;
74
+
75
+ while ((hr = pRtEnum->Next (1 , (IUnknown **)&info, &fetched)) == S_OK && fetched > 0 )
76
+ {
77
+ ZeroMemory (strName, sizeof (strName));
78
+ info->GetVersionString (strName, &len);
79
+ hr = info->GetInterface (CLSID_CLRRuntimeHost,
80
+ IID_ICLRRuntimeHost,
81
+ reinterpret_cast <LPVOID *>(&m_pClr));
82
+ if (!SUCCEEDED (hr))
83
+ printf (" hr failed...." );
84
+ runtimesLoaded = true ;
85
+ this ->m_lastCLR .assign (strName);
86
+ }
87
+ pRtEnum->Release ();
88
+ pRtEnum = NULL ;
89
+ if (!runtimesLoaded)
90
+ {
91
+ pMetaHost->EnumerateInstalledRuntimes (&pRtEnum);
92
+ while ((hr = pRtEnum->Next (1 , (IUnknown **)&info, &fetched)) == S_OK && fetched > 0 )
93
+ {
94
+ ZeroMemory (strName, sizeof (strName));
95
+
96
+ info->GetVersionString (strName, &len);
97
+ hr = info->GetInterface (CLSID_CLRRuntimeHost,
98
+ IID_ICLRRuntimeHost,
99
+ reinterpret_cast <LPVOID *>(&m_pClr));
100
+ if (!SUCCEEDED (hr))
101
+ printf (" hr failed...." );
102
+ m_CLRRuntimeMap[std::wstring (strName)] = m_pClr;
103
+ this ->m_lastCLR .assign (strName);
104
+ }
105
+ pRtEnum->Release ();
106
+ }
107
+ pMetaHost->Release ();
101
108
return S_OK;
102
109
}
103
110
111
+
112
+
104
113
// / <summary>
105
114
// / Create a host object, and bind to the CLR
106
115
// / </summary>
@@ -154,14 +163,14 @@ STDMETHODIMP CClrHost::SetAppDomainManager(DWORD dwAppDomainId, __in IUnknown *p
154
163
_ASSERTE (!" AppDomainManager does not implement IManagedHost" );
155
164
return E_NOINTERFACE;
156
165
}
157
-
158
166
// register ourselves as the unmanaged host
159
167
HRESULT hrSetUnmanagedHost = pAppDomainManager->raw_SetUnmanagedHost (static_cast <IUnmanagedHost *>(this ));
160
168
if (FAILED (hrSetUnmanagedHost))
161
169
return hrSetUnmanagedHost;
162
170
171
+ auto clr = std::wstring (pAppDomainManager->GetCLR ());
163
172
// save a copy
164
- m_appDomainManagers[dwAppDomainId ] = pAppDomainManager;
173
+ m_appDomainManagers[clr ] = pAppDomainManager;
165
174
return S_OK;
166
175
}
167
176
@@ -171,50 +180,57 @@ STDMETHODIMP CClrHost::SetAppDomainManager(DWORD dwAppDomainId, __in IUnknown *p
171
180
STDMETHODIMP CClrHost::raw_Start ()
172
181
{
173
182
// we should have bound to the runtime, but not yet started it upon entry
174
- _ASSERTE (m_pClr != NULL );
175
- _ASSERTE (!m_started);
183
+ // _ASSERTE(m_pClr != NULL
184
+ if (!m_started)
185
+ {
186
+ _ASSERTE (!m_started);
187
+ // if (m_pClr == NULL)
188
+ // return E_FAIL;
176
189
177
- if (m_pClr == NULL )
178
- return E_FAIL;
190
+ for ( auto &x : m_CLRRuntimeMap )
191
+ {
179
192
180
- // get the CLR control object
181
- HRESULT hrClrControl = m_pClr->GetCLRControl (&m_pClrControl);
182
- if (FAILED (hrClrControl))
183
- return hrClrControl;
193
+ ICLRRuntimeHost *m_pClr = x.second ;
194
+ // get the CLR control object
195
+ HRESULT hrClrControl = m_pClr->GetCLRControl (&m_pClrControl);
196
+ if (FAILED (hrClrControl))
197
+ return hrClrControl;
184
198
185
- // set ourselves up as the host control
186
- HRESULT hrHostControl = m_pClr->SetHostControl (static_cast <IHostControl *>(this ));
199
+ // set ourselves up as the host control
200
+ HRESULT hrHostControl = m_pClr->SetHostControl (static_cast <IHostControl *>(this ));
187
201
188
- // get the host protection manager
189
- ICLRHostProtectionManager *pHostProtectionManager = NULL ;
190
- HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager (
191
- IID_ICLRHostProtectionManager,
192
- reinterpret_cast <void **>(&pHostProtectionManager));
193
- if (FAILED (hrGetProtectionManager))
194
- return hrGetProtectionManager;
202
+ // get the host protection manager
203
+ ICLRHostProtectionManager *pHostProtectionManager = NULL ;
204
+ HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager (
205
+ IID_ICLRHostProtectionManager,
206
+ reinterpret_cast <void **>(&pHostProtectionManager));
207
+ if (FAILED (hrGetProtectionManager))
208
+ return hrGetProtectionManager;
195
209
196
- // setup host proctection
197
- HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories (
198
- (EApiCategories)(eSynchronization | eSelfAffectingThreading));
199
- pHostProtectionManager->Release ();
210
+ // setup host proctection
211
+ HRESULT hrHostProtection = pHostProtectionManager->SetProtectedCategories (
212
+ (EApiCategories)(eSynchronization | eSelfAffectingThreading));
213
+ pHostProtectionManager->Release ();
200
214
201
- if (FAILED (hrHostProtection))
202
- return hrHostProtection;
215
+ if (FAILED (hrHostProtection))
216
+ return hrHostProtection;
203
217
204
218
205
- // setup the AppDomainManager
206
- HRESULT hrSetAdm = m_pClrControl->SetAppDomainManagerType (AppDomainManagerAssembly, AppDomainManagerType);
207
- if (FAILED (hrSetAdm))
208
- return hrSetAdm;
219
+ // setup the AppDomainManager
220
+ HRESULT hrSetAdm = m_pClrControl->SetAppDomainManagerType (AppDomainManagerAssembly, AppDomainManagerType);
221
+ if (FAILED (hrSetAdm))
222
+ return hrSetAdm;
209
223
210
- // mark as started
211
- m_started = true ;
212
224
213
- // finally, start the runtime
214
- HRESULT hrStart = m_pClr->Start ();
215
- if (FAILED (hrStart))
216
- return hrStart;
225
+ // finally, start the runtime
226
+ HRESULT hrStart = m_pClr->Start ();
227
+ if (FAILED (hrStart))
228
+ return hrStart;
229
+ }
217
230
231
+ // mark as started
232
+ m_started = true ;
233
+ }
218
234
return S_OK;
219
235
}
220
236
@@ -230,7 +246,7 @@ STDMETHODIMP CClrHost::raw_Stop()
230
246
iAdm->second ->raw_Dispose ();
231
247
232
248
// then, shut down the CLR
233
- return m_pClr->Stop ();
249
+ return S_OK; // m_pClr->Stop();
234
250
}
235
251
236
252
// / <summary>
@@ -239,31 +255,31 @@ STDMETHODIMP CClrHost::raw_Stop()
239
255
STDMETHODIMP CClrHost::get_DefaultManagedHost (__out IManagedHost **ppHost)
240
256
{
241
257
// just get the AppDomainManager for the default AppDomain
242
- return raw_GetManagedHost (1 , ppHost);
258
+ return raw_GetManagedHost (1 , BSTR (m_lastCLR. c_str ()), ppHost);
243
259
}
244
260
245
261
// / <summary>
246
262
// / Get the AppDomainManager for a specific AppDomain
247
263
// / </summary>
248
- STDMETHODIMP CClrHost::raw_GetManagedHost (long appDomain, IManagedHost **ppHost)
264
+ STDMETHODIMP CClrHost::raw_GetManagedHost (long appDomain, BSTR clr, IManagedHost **ppHost)
249
265
{
250
266
_ASSERTE (m_started);
251
267
252
268
if (ppHost == NULL )
253
269
return E_POINTER;
254
270
255
271
// get the AppDomainManager for the specified domain
256
- AppDomainManagerMap::const_iterator iHost = m_appDomainManagers. find (appDomain) ;
272
+ auto iHost = m_appDomainManagers[clr] ;
257
273
258
274
// see if we've got a host
259
- if (iHost == m_appDomainManagers. end () )
275
+ if (iHost == NULL )
260
276
{
261
277
*ppHost = NULL ;
262
278
return E_NOMANAGEDHOST;
263
279
}
264
280
else
265
281
{
266
- *ppHost = iHost-> second ;
282
+ *ppHost = iHost;
267
283
(*ppHost)->AddRef ();
268
284
return S_OK;
269
285
}
@@ -272,4 +288,11 @@ STDMETHODIMP CClrHost::raw_GetManagedHost(long appDomain, IManagedHost **ppHost)
272
288
// IHostGCManager
273
289
STDMETHODIMP CClrHost::SuspensionEnding (DWORD generation){ return S_OK; }
274
290
STDMETHODIMP CClrHost::SuspensionStarting (){ return S_OK; }
275
- STDMETHODIMP CClrHost::ThreadIsBlockingForSuspension (){ return S_OK; }
291
+ STDMETHODIMP CClrHost::ThreadIsBlockingForSuspension (){ return S_OK; }
292
+
293
+ STDMETHODIMP CClrHost::CreateAppDomainForQuery (std::string FnName)
294
+ {
295
+ IManagedHostPtr pAppMgr = this ->GetDefaultManagedHost ();
296
+ _bstr_t retString = pAppMgr->CreateAppDomain (_bstr_t (FnName.c_str ()));
297
+ return S_OK;
298
+ }
0 commit comments