Skip to content

Commit 58da956

Browse files
committed
Got a good working sample. Now to clean up the CLR host and allow some configuration from external files.
1 parent 6e54d2a commit 58da956

File tree

4 files changed

+76
-57
lines changed

4 files changed

+76
-57
lines changed

clr_host/ClrHost.cpp

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
const wchar_t *CClrHost::AppDomainManagerAssembly = L"mysql_managed_interface, Version=1.0.0.0, PublicKeyToken=71c4a5d4270bd29c";
55
const wchar_t *CClrHost::AppDomainManagerType = L"mysql_managed_interface.MySQLHostManager";
66

7+
bool g_CLRHasBeenLoaded = false;
8+
79
/// <summary>
810
/// Initialize the host
911
/// </summary>
12+
13+
1014
CClrHost::CClrHost() : m_started(false), m_pClr(NULL), m_pClrControl(NULL)
1115
{
1216
return;
@@ -36,15 +40,15 @@ CClrHost::~CClrHost()
3640
#pragma warning( disable : 4996 )
3741
HRESULT CClrHost::FinalConstruct()
3842
{
39-
// load the CLR into the process
43+
//load the CLR into the process
4044
return CorBindToRuntimeEx(NULL,
4145
NULL,
4246
0,
4347
CLSID_CLRRuntimeHost,
4448
IID_ICLRRuntimeHost,
4549
reinterpret_cast<LPVOID *>(&m_pClr));
4650

47-
//// load the CLR into the process
51+
// load the CLR into the process
4852
//ICLRMetaHost *pMetaHost = NULL;
4953
//ICLRMetaHostPolicy *pMetaHostPolicy = NULL;
5054
//ICLRDebugging *pCLRDebugging = NULL;
@@ -60,22 +64,41 @@ HRESULT CClrHost::FinalConstruct()
6064
//IEnumUnknown * pRtEnum = NULL;
6165
//ICLRRuntimeInfo *info = NULL;
6266
//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+
6384
//pMetaHost->EnumerateInstalledRuntimes(&pRtEnum);
6485
//while ((hr = pRtEnum->Next(1, (IUnknown **)&info, &fetched)) == S_OK && fetched > 0)
6586
//{
6687
// WCHAR strName[128];
6788
// ZeroMemory(strName, sizeof(strName));
6889
// DWORD len = 128;
6990
// info->GetVersionString(strName, &len);
70-
// hr = info->GetInterface(CLSID_CLRRuntimeHost,
71-
// IID_ICLRRuntimeHost,
91+
// hr = info->GetInterface(CLSID_CLRRuntimeHost,
92+
// IID_ICLRRuntimeHost,
7293
// reinterpret_cast<LPVOID *>(&m_pClr));
7394
// if (!SUCCEEDED(hr))
7495
// printf("hr failed....");
75-
//
96+
7697
//}
7798
//pRtEnum->Release();
78-
//return S_OK;
99+
100+
101+
return S_OK;
79102
}
80103

81104
/// <summary>
@@ -150,14 +173,18 @@ STDMETHODIMP CClrHost::raw_Start()
150173
// we should have bound to the runtime, but not yet started it upon entry
151174
_ASSERTE(m_pClr != NULL);
152175
_ASSERTE(!m_started);
176+
153177
if (m_pClr == NULL)
154178
return E_FAIL;
155179

156180
// get the CLR control object
157181
HRESULT hrClrControl = m_pClr->GetCLRControl(&m_pClrControl);
158182
if (FAILED(hrClrControl))
159183
return hrClrControl;
160-
184+
185+
// set ourselves up as the host control
186+
HRESULT hrHostControl = m_pClr->SetHostControl(static_cast<IHostControl *>(this));
187+
161188
// get the host protection manager
162189
ICLRHostProtectionManager *pHostProtectionManager = NULL;
163190
HRESULT hrGetProtectionManager = m_pClrControl->GetCLRManager(
@@ -174,27 +201,20 @@ STDMETHODIMP CClrHost::raw_Start()
174201
if (FAILED(hrHostProtection))
175202
return hrHostProtection;
176203

177-
// set ourselves up as the host control
178-
HRESULT hrHostControl = m_pClr->SetHostControl(static_cast<IHostControl *>(this));
179-
if (FAILED(hrHostControl))
180-
return hrHostControl;
181204

182205
// setup the AppDomainManager
183206
HRESULT hrSetAdm = m_pClrControl->SetAppDomainManagerType(AppDomainManagerAssembly, AppDomainManagerType);
184207
if (FAILED(hrSetAdm))
185208
return hrSetAdm;
186209

187-
188-
189-
210+
// mark as started
211+
m_started = true;
190212

191213
// finally, start the runtime
192214
HRESULT hrStart = m_pClr->Start();
193215
if (FAILED(hrStart))
194216
return hrStart;
195217

196-
// mark as started
197-
m_started = true;
198218
return S_OK;
199219
}
200220

@@ -205,7 +225,6 @@ STDMETHODIMP CClrHost::raw_Start()
205225
STDMETHODIMP CClrHost::raw_Stop()
206226
{
207227
_ASSERTE(m_started);
208-
209228
// first send a Dispose call to the managed hosts
210229
for (AppDomainManagerMap::iterator iAdm = m_appDomainManagers.begin(); iAdm != m_appDomainManagers.end(); iAdm++)
211230
iAdm->second->raw_Dispose();

clr_host/ClrHost.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ class ATL_NO_VTABLE CClrHost : public CComObjectRootEx<CComSingleThreadModel>,
4343

4444
// IHostControl
4545
public:
46-
STDMETHODIMP GetHostManager(const IID &riid, void **ppvObject);
47-
STDMETHODIMP SetAppDomainManager(DWORD dwAppDomainId, IUnknown *pUnkAppDomainManager);
46+
STDMETHODIMP GetHostManager(const IID &riid, void **ppvObject);
47+
STDMETHODIMP SetAppDomainManager(DWORD dwAppDomainId, IUnknown *pUnkAppDomainManager);
4848

4949
// IUnmanagedHost
5050
public:

clr_host/stdafx.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@
3131
#include <comdef.h>
3232
#include <tchar.h>
3333
#include <mscoree.h>
34+
#ifdef DEBUG
35+
#import "Debug/mysql_managed_interface.tlb" no_namespace
36+
#else
3437
#import "Release/mysql_managed_interface.tlb" no_namespace
38+
#endif
39+
3540
#pragma warning (pop)
3641
#include <metahost.h>
3742
#pragma comment(lib, "mscoree.lib")

udf_example.c

Lines changed: 33 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,15 @@ extern "C"
8484
longlong myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
8585
char *error);
8686
void myfunc_int_deinit(UDF_INIT *initid);
87-
int RunApplication(IUnmanagedHostPtr &pClr, char *input);
8887

89-
int RunApplication(IUnmanagedHostPtr &pClr, char *input)
88+
longlong RunApplication(IUnmanagedHostPtr &pClr, longlong input);
89+
90+
longlong RunApplication(IUnmanagedHostPtr &pClr, longlong input)
9091
{
9192
// Get the default managed host
9293
IManagedHostPtr pManagedHost = pClr->DefaultManagedHost;
9394

94-
// create a new AppDomain
95-
_bstr_t name(L"Second AppDomain");
96-
DWORD id = pManagedHost->CreateAppDomain(name);
97-
98-
// get its host, and print to the screen
99-
IManagedHostPtr pSecondManagedHost = pClr->GetManagedHost(id);
100-
//pSecondManagedHost->Write(_bstr_t(L"Hello, World."));
101-
pSecondManagedHost->Run(_bstr_t(input));
102-
103-
return 0;
95+
return pManagedHost->Run(input);
10496
}
10597

10698

@@ -114,11 +106,14 @@ extern "C"
114106
try
115107
{
116108
// bind the to CLR
117-
HRESULT hrBind = CClrHost::BindToRuntime(&pClr.GetInterfacePtr());
118-
if (FAILED(hrBind))
119-
_com_raise_error(hrBind);
120-
// start it up
121-
pClr->Start();
109+
if (pClr.GetInterfacePtr() == NULL)
110+
{
111+
HRESULT hrBind = CClrHost::BindToRuntime(&pClr.GetInterfacePtr());
112+
if (FAILED(hrBind))
113+
_com_raise_error(hrBind);
114+
// start it up
115+
pClr->Start();
116+
}
122117
}
123118
catch (const _com_error &e)
124119
{
@@ -140,10 +135,28 @@ extern "C"
140135
int returnCode = 0;
141136
try
142137
{
143-
// bind the to CLR
144-
138+
longlong val = 0;
139+
uint i;
140+
for (i = 0; i < args->arg_count; i++)
141+
{
142+
if (args->args[i] == NULL)
143+
continue;
144+
switch (args->arg_type[i]) {
145+
case STRING_RESULT: /* Add string lengths */
146+
val += args->lengths[i];
147+
break;
148+
case INT_RESULT: /* Add numbers */
149+
val += RunApplication(pClr, *((longlong*)args->args[i]));
150+
break;
151+
case REAL_RESULT: /* Add numers as longlong */
152+
val += (longlong)((double)RunApplication(pClr, *((longlong*)args->args[i])));
153+
break;
154+
default:
155+
break;
156+
}
157+
}
158+
return val;
145159
// run the application
146-
returnCode = RunApplication(pClr, args->args[0]);
147160
}
148161
catch (const _com_error &e)
149162
{
@@ -159,23 +172,5 @@ extern "C"
159172

160173
void myfunc_int_deinit(UDF_INIT *initid)
161174
{
162-
int returnCode = 0;
163-
try
164-
{
165-
166-
// finally, dispose of the managed hosts and shut down the runtime
167-
pClr->Stop();
168-
}
169-
catch (const _com_error &e)
170-
{
171-
const wchar_t *message = (wchar_t *)e.Description() == NULL ?
172-
L"" :
173-
(wchar_t *)e.Description();
174-
std::wcerr << L"Error 0x" << std::hex << e.Error() << L") : " << message << std::endl;
175-
176-
returnCode = e.Error();
177-
}
178-
179-
CoUninitialize();
180175
}
181176
}

0 commit comments

Comments
 (0)