2010-01-26 22:37:48 +00:00
|
|
|
|
/*
|
|
|
|
|
|
2012-05-27 14:23:31 +00:00
|
|
|
|
This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2012-10-27 11:40:28 +00:00
|
|
|
|
Copyright (C) 2009-2012 Michael M<EFBFBD>ller <mmoeller@openhardwaremonitor.org>
|
2012-05-27 14:23:31 +00:00
|
|
|
|
|
2010-01-26 22:37:48 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
using System.Reflection.Emit;
|
|
|
|
|
using System.Runtime.InteropServices;
|
2012-10-27 11:40:28 +00:00
|
|
|
|
using OpenHardwareMonitor.Collections;
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
namespace OpenHardwareMonitor.Hardware {
|
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
internal static class PInvokeDelegateFactory {
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2010-09-21 20:32:36 +00:00
|
|
|
|
private static readonly ModuleBuilder moduleBuilder =
|
2010-08-15 14:46:58 +00:00
|
|
|
|
AppDomain.CurrentDomain.DefineDynamicAssembly(
|
|
|
|
|
new AssemblyName("PInvokeDelegateFactoryInternalAssembly"),
|
|
|
|
|
AssemblyBuilderAccess.Run).DefineDynamicModule(
|
|
|
|
|
"PInvokeDelegateFactoryInternalModule");
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
2012-10-27 11:40:28 +00:00
|
|
|
|
private static readonly IDictionary<Pair<DllImportAttribute, Type>, Type> wrapperTypes =
|
|
|
|
|
new Dictionary<Pair<DllImportAttribute, Type>, Type>();
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
|
|
|
|
|
out T newDelegate) where T : class
|
|
|
|
|
{
|
|
|
|
|
Type wrapperType;
|
2012-10-27 11:40:28 +00:00
|
|
|
|
Pair<DllImportAttribute, Type> key =
|
|
|
|
|
new Pair<DllImportAttribute, Type>(dllImportAttribute, typeof(T));
|
|
|
|
|
wrapperTypes.TryGetValue(key, out wrapperType);
|
2010-01-26 22:37:48 +00:00
|
|
|
|
|
|
|
|
|
if (wrapperType == null) {
|
|
|
|
|
wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
|
2012-10-27 11:40:28 +00:00
|
|
|
|
wrapperTypes.Add(key, wrapperType);
|
2010-01-26 22:37:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
|
|
|
|
|
dllImportAttribute.EntryPoint) as T;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static Type CreateWrapperType(Type delegateType,
|
|
|
|
|
DllImportAttribute dllImportAttribute) {
|
|
|
|
|
|
|
|
|
|
TypeBuilder typeBuilder = moduleBuilder.DefineType(
|
|
|
|
|
"PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count);
|
|
|
|
|
|
|
|
|
|
MethodInfo methodInfo = delegateType.GetMethod("Invoke");
|
|
|
|
|
|
|
|
|
|
ParameterInfo[] parameterInfos = methodInfo.GetParameters();
|
|
|
|
|
int parameterCount = parameterInfos.GetLength(0);
|
|
|
|
|
|
|
|
|
|
Type[] parameterTypes = new Type[parameterCount];
|
|
|
|
|
for (int i = 0; i < parameterCount; i++)
|
|
|
|
|
parameterTypes[i] = parameterInfos[i].ParameterType;
|
|
|
|
|
|
|
|
|
|
MethodBuilder methodBuilder = typeBuilder.DefinePInvokeMethod(
|
|
|
|
|
dllImportAttribute.EntryPoint, dllImportAttribute.Value,
|
|
|
|
|
MethodAttributes.Public | MethodAttributes.Static |
|
|
|
|
|
MethodAttributes.PinvokeImpl, CallingConventions.Standard,
|
|
|
|
|
methodInfo.ReturnType, parameterTypes,
|
|
|
|
|
dllImportAttribute.CallingConvention,
|
|
|
|
|
dllImportAttribute.CharSet);
|
|
|
|
|
|
|
|
|
|
foreach (ParameterInfo parameterInfo in parameterInfos)
|
|
|
|
|
methodBuilder.DefineParameter(parameterInfo.Position + 1,
|
|
|
|
|
parameterInfo.Attributes, parameterInfo.Name);
|
|
|
|
|
|
|
|
|
|
if (dllImportAttribute.PreserveSig)
|
|
|
|
|
methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
|
|
|
|
|
|
|
|
|
|
return typeBuilder.CreateType();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|