Skip to content

Commit 6a588f8

Browse files
committed
Work in progress.
1 parent f1696c3 commit 6a588f8

File tree

8 files changed

+134
-43
lines changed

8 files changed

+134
-43
lines changed

clr_host/ClrHost.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ HRESULT CClrHost::FinalConstruct()
8686
}
8787
pRtEnum->Release();
8888
pRtEnum = NULL;
89+
8990
if (!runtimesLoaded)
9091
{
9192
pMetaHost->EnumerateInstalledRuntimes(&pRtEnum);
@@ -290,9 +291,10 @@ STDMETHODIMP CClrHost::SuspensionEnding(DWORD generation){ return S_OK; }
290291
STDMETHODIMP CClrHost::SuspensionStarting(){ return S_OK; }
291292
STDMETHODIMP CClrHost::ThreadIsBlockingForSuspension(){ return S_OK; }
292293

293-
STDMETHODIMP CClrHost::CreateAppDomainForQuery(std::string FnName)
294+
std::wstring CClrHost::CreateAppDomainForQuery(std::string FnName)
294295
{
295296
IManagedHostPtr pAppMgr = this->GetDefaultManagedHost();
296297
_bstr_t retString = pAppMgr->CreateAppDomain(_bstr_t(FnName.c_str()));
298+
297299
return S_OK;
298300
}

clr_host/ClrHost.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ATL_NO_VTABLE CClrHost : public CComObjectRootEx<CComSingleThreadModel>,
4545

4646
public:
4747
static HRESULT BindToRuntime(IUnmanagedHost **pHost);
48-
STDMETHODIMP CreateAppDomainForQuery(std::string FnName);
48+
std::wstring CreateAppDomainForQuery(std::string FnName);
4949

5050
// IHostControl
5151
public:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace mysql_managed_interface
6+
{
7+
public interface ICustomAssembly
8+
{
9+
Int64 RunInteger(Int64 value);
10+
Int64 RunIntegers(Int64[] values);
11+
12+
13+
double RunReal(double value);
14+
double RunReals(double[] values);
15+
16+
17+
string RunString(string value);
18+
string RunStrings(string[] values);
19+
}
20+
}

mysql_managed_interface/IManagedHost.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ public interface IManagedHost
1313
/// </summary>
1414
/// <param name="name">name of the AppDomain to create</param>
1515
/// <returns>ID of the new AppDomain</returns>
16-
string CreateAppDomain([MarshalAs(UnmanagedType.BStr)]string name);
16+
string CreateAppDomain(string functionName);
17+
string CreateAppDomain(string assemblyName, string functionName);
1718

1819
/// <summary>
1920
/// Clean up the AppDomianManager
@@ -26,20 +27,16 @@ public interface IManagedHost
2627
/// <param name="unmanagedHost">unmanaged half of the host</param>
2728
void SetUnmanagedHost(IUnmanagedHost unmanagedHost);
2829

29-
/// <summary>
30-
/// Write a message
31-
/// </summary>
32-
/// <param name="message">message to write</param>
33-
void Write([MarshalAs(UnmanagedType.BStr)]string message);
30+
Int64 RunInteger(string functionName, Int64 value);
31+
Int64 RunIntegers(string functionName, Int64[] values);
3432

3533

36-
/// <summary>
37-
/// Write a message
38-
/// </summary>
39-
/// <param name="message">message to write</param>
40-
///
41-
[return: MarshalAs(UnmanagedType.I8)]
42-
Int64 Run([MarshalAs(UnmanagedType.I8)]Int64 path);
34+
double RunReal(string functionName, double value);
35+
double RunReals(string functionName, double[] values);
36+
37+
38+
string RunString(string functionName, string value);
39+
string RunStrings(string functionName, string[] values);
4340

4441
[return: MarshalAs(UnmanagedType.BStr)]
4542
string GetCLR();

mysql_managed_interface/MySQLHostManager.cs

Lines changed: 61 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,19 @@ namespace mysql_managed_interface
1010
{
1111
class MySQLHostManager : AppDomainManager, IManagedHost
1212
{
13+
private System.Collections.Generic.Dictionary<string, ICustomAssembly> functions = null;
14+
private string m_Assembly;
15+
private string m_Class;
1316

1417
static void ADIDelegate(string[] args)
1518
{
19+
//var asm = AppDomain.CurrentDomain.Load(args[0]);
20+
}
1621

22+
void CurrentDomain_AssemblyLoad(object sender, AssemblyLoadEventArgs args)
23+
{
24+
25+
throw new NotImplementedException();
1726
}
1827

1928
public static StrongName CreateStrongName(Assembly assembly)
@@ -42,27 +51,44 @@ public static StrongName CreateStrongName(Assembly assembly)
4251
/// </summary>
4352
public override void InitializeNewDomain(AppDomainSetup appDomainInfo)
4453
{
54+
if (appDomainInfo != null)
55+
{
56+
if (appDomainInfo.AppDomainInitializerArguments.Length > 0)
57+
{
58+
m_Assembly = appDomainInfo.AppDomainInitializerArguments[0];
59+
}
60+
if (appDomainInfo.AppDomainInitializerArguments.Length > 1)
61+
{
62+
m_Class = appDomainInfo.AppDomainInitializerArguments[1];
63+
}
64+
}
4565
// let the unmanaged host know about us
4666
InitializationFlags = AppDomainManagerInitializationOptions.RegisterWithHost;
47-
48-
return;
67+
68+
4969
}
5070

5171
public string CreateAppDomain(string name)
72+
{
73+
return CreateAppDomain(name, "");
74+
}
75+
76+
public string CreateAppDomain(string assemblyName, string className)
5277
{
5378
PermissionSet permissions = new PermissionSet(PermissionState.None);
54-
permissions.AddPermission(new SecurityPermission(PermissionState.Unrestricted));
55-
permissions.AddPermission(new UIPermission(PermissionState.Unrestricted));
79+
permissions.AddPermission(new SecurityPermission(SecurityPermissionFlag.Execution));
5680

5781
AppDomainSetup ads = new AppDomainSetup();
5882
ads.AppDomainInitializer = ADIDelegate;
83+
ads.AppDomainInitializerArguments = new string[] { assemblyName, className };
5984
ads.ConfigurationFile = "mysqldotnet.config";
6085
ads.PrivateBinPath = @"lib\plugin";
6186
ads.PrivateBinPathProbe = "";
62-
87+
88+
string AppDomainName = DateTime.Now.ToFileTime().ToString();
6389

6490
return string.Format("{0}||{1}", GetCLR(), AppDomain.CreateDomain(
65-
name,
91+
AppDomainName,
6692
AppDomain.CurrentDomain.Evidence,
6793
ads,
6894
permissions,
@@ -93,22 +119,40 @@ public void Write(string message)
93119
public Int64 Run(Int64 path)
94120
{
95121
return (path * 3);
96-
//new FileIOPermission(PermissionState.Unrestricted).Assert();
97-
//string fullPath = Path.Combine(
98-
// Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
99-
// path);
100-
//CodeAccessPermission.RevertAssert();
101-
102-
//new FileIOPermission(
103-
// FileIOPermissionAccess.Read | FileIOPermissionAccess.PathDiscovery,
104-
// fullPath).Assert();
105-
//AppDomain.CurrentDomain.ExecuteAssembly(fullPath);
106-
//CodeAccessPermission.RevertAssert();
107122
}
108123

109124
public string GetCLR()
110125
{
111126
return string.Format("v{0}", Environment.Version.ToString(3));
112127
}
128+
129+
public long RunInteger(string functionName, long value)
130+
{
131+
return functions[functionName].RunInteger(value);
132+
}
133+
134+
public long RunIntegers(string functionName, long[] values)
135+
{
136+
return functions[functionName].RunIntegers(values);
137+
}
138+
public double RunReal(string functionName, double value)
139+
{
140+
return functions[functionName].RunReal(value);
141+
}
142+
143+
public double RunReals(string functionName, double[] values)
144+
{
145+
return functions[functionName].RunReals(values);
146+
}
147+
148+
public string RunString(string functionName, string value)
149+
{
150+
return functions[functionName].RunString(value);
151+
}
152+
153+
public string RunStrings(string functionName, string[] values)
154+
{
155+
return functions[functionName].RunStrings(values);
156+
}
113157
}
114158
}

mysql_managed_interface/mysql_managed_interface.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<Reference Include="System.Xml" />
4444
</ItemGroup>
4545
<ItemGroup>
46+
<Compile Include="ICustomAssembly.cs" />
4647
<Compile Include="IManagedHost.cs" />
4748
<Compile Include="IUnmanagedHost.cs" />
4849
<Compile Include="MySQLHostManager.cs" />

mysql_udf.c

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,26 +76,31 @@ _COM_SMARTPTR_TYPEDEF(ISupportErrorInfo, __uuidof(ISupportErrorInfo));
7676
_COM_SMARTPTR_TYPEDEF(IErrorInfo, __uuidof(IErrorInfo));
7777

7878

79-
80-
IUnmanagedHostPtr pClrHost = NULL;
81-
8279
extern "C"
8380
{
81+
IUnmanagedHostPtr pClrHost = NULL;
82+
8483
my_bool myfunc_int_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
85-
longlong myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
84+
long long myfunc_int(UDF_INIT *initid, UDF_ARGS *args, char *is_null,
8685
char *error);
8786
void myfunc_int_deinit(UDF_INIT *initid);
8887

89-
longlong RunApplication(IUnmanagedHostPtr &pClr, longlong input);
90-
91-
longlong RunApplication(IUnmanagedHostPtr &pClr, longlong input)
88+
longlong RunInteger(IUnmanagedHostPtr &pClr, longlong input)
9289
{
9390
// Get the default managed host
9491
IManagedHostPtr pManagedHost = pClr->DefaultManagedHost;
9592

96-
return pManagedHost->Run(input);
93+
return pManagedHost->RunInteger(L"myfunc", input);
9794
}
9895

96+
//longlong RunIntegers(IUnmanagedHostPtr &pClr, longlong* input)
97+
//{
98+
// // Get the default managed host
99+
// IManagedHostPtr pManagedHost = pClr->DefaultManagedHost;
100+
// SAFEARRAY sa;
101+
// return pManagedHost->RunIntegers(L"myfunc", input);
102+
//}
103+
99104

100105

101106

@@ -149,10 +154,10 @@ extern "C"
149154
val += args->lengths[i];
150155
break;
151156
case INT_RESULT: /* Add numbers */
152-
val += RunApplication(pClrHost, *((longlong*)args->args[i]));
157+
val += RunInteger(pClrHost, *((longlong*)args->args[i]));
153158
break;
154159
case REAL_RESULT: /* Add numers as longlong */
155-
val += (longlong)((double)RunApplication(pClrHost, *((longlong*)args->args[i])));
160+
val += (longlong)((double)RunInteger(pClrHost, *((longlong*)args->args[i])));
156161
break;
157162
default:
158163
break;

mysql_udf.sln

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ALL_BUILD", "ALL_BUILD.vcxp
1010
EndProject
1111
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ZERO_CHECK", "ZERO_CHECK.vcxproj", "{EFEEAAAA-C165-4296-8B19-505B443F1651}"
1212
EndProject
13-
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "udf_example", "udf_example.vcxproj", "{5A481DF1-9C45-4152-BC05-9E9EFEDED7D4}"
13+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mysql_udf", "udf_example.vcxproj", "{5A481DF1-9C45-4152-BC05-9E9EFEDED7D4}"
1414
ProjectSection(ProjectDependencies) = postProject
1515
{EFEEAAAA-C165-4296-8B19-505B443F1651} = {EFEEAAAA-C165-4296-8B19-505B443F1651}
1616
{AA94A4C4-52E0-488A-B53E-3FE923E32C8B} = {AA94A4C4-52E0-488A-B53E-3FE923E32C8B}
@@ -23,6 +23,8 @@ Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "clr_host", "clr_host\clr_ho
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mysql_managed_interface", "mysql_managed_interface\mysql_managed_interface.csproj", "{A15DADAC-D3C8-4226-93CD-2DA7C85F51DC}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "mysqldotnet_custom", "..\mysqldotnet_custom\mysqldotnet_custom.csproj", "{4CBE7806-245F-47C5-9397-944AAB45391E}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -135,6 +137,26 @@ Global
135137
{A15DADAC-D3C8-4226-93CD-2DA7C85F51DC}.RelWithDebInfo|Mixed Platforms.ActiveCfg = Release|Any CPU
136138
{A15DADAC-D3C8-4226-93CD-2DA7C85F51DC}.RelWithDebInfo|Mixed Platforms.Build.0 = Release|Any CPU
137139
{A15DADAC-D3C8-4226-93CD-2DA7C85F51DC}.RelWithDebInfo|Win32.ActiveCfg = Release|Any CPU
140+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
141+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Debug|Any CPU.Build.0 = Debug|Any CPU
142+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
143+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
144+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Debug|Win32.ActiveCfg = Debug|Any CPU
145+
{4CBE7806-245F-47C5-9397-944AAB45391E}.MinSizeRel|Any CPU.ActiveCfg = Release|Any CPU
146+
{4CBE7806-245F-47C5-9397-944AAB45391E}.MinSizeRel|Any CPU.Build.0 = Release|Any CPU
147+
{4CBE7806-245F-47C5-9397-944AAB45391E}.MinSizeRel|Mixed Platforms.ActiveCfg = Release|Any CPU
148+
{4CBE7806-245F-47C5-9397-944AAB45391E}.MinSizeRel|Mixed Platforms.Build.0 = Release|Any CPU
149+
{4CBE7806-245F-47C5-9397-944AAB45391E}.MinSizeRel|Win32.ActiveCfg = Release|Any CPU
150+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Release|Any CPU.ActiveCfg = Release|Any CPU
151+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Release|Any CPU.Build.0 = Release|Any CPU
152+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
153+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Release|Mixed Platforms.Build.0 = Release|Any CPU
154+
{4CBE7806-245F-47C5-9397-944AAB45391E}.Release|Win32.ActiveCfg = Release|Any CPU
155+
{4CBE7806-245F-47C5-9397-944AAB45391E}.RelWithDebInfo|Any CPU.ActiveCfg = Release|Any CPU
156+
{4CBE7806-245F-47C5-9397-944AAB45391E}.RelWithDebInfo|Any CPU.Build.0 = Release|Any CPU
157+
{4CBE7806-245F-47C5-9397-944AAB45391E}.RelWithDebInfo|Mixed Platforms.ActiveCfg = Release|Any CPU
158+
{4CBE7806-245F-47C5-9397-944AAB45391E}.RelWithDebInfo|Mixed Platforms.Build.0 = Release|Any CPU
159+
{4CBE7806-245F-47C5-9397-944AAB45391E}.RelWithDebInfo|Win32.ActiveCfg = Release|Any CPU
138160
EndGlobalSection
139161
GlobalSection(SolutionProperties) = preSolution
140162
HideSolutionNode = FALSE

0 commit comments

Comments
 (0)