openhardwaremonitor/Hardware/PInvokeDelegateFactory.cs

90 lines
3.4 KiB
C#
Raw Normal View History

2010-01-26 22:37:48 +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
Copyright (C) 2009-2012 Michael M<EFBFBD>ller <mmoeller@openhardwaremonitor.org>
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;
using OpenHardwareMonitor.Collections;
2010-01-26 22:37:48 +00:00
namespace OpenHardwareMonitor.Hardware {
internal static class PInvokeDelegateFactory {
2010-01-26 22:37:48 +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
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, DllImportSearchPath dllImportSearchPath =
DllImportSearchPath.System32) where T : class
2010-01-26 22:37:48 +00:00
{
Type wrapperType;
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, dllImportSearchPath);
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,
DllImportSearchPath dllImportSearchPath)
{
2010-01-26 22:37:48 +00:00
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);
methodBuilder.SetCustomAttribute(new CustomAttributeBuilder(
typeof(DefaultDllImportSearchPathsAttribute).GetConstructor(
new Type[] { typeof(DllImportSearchPath) }),
new object[] { dllImportSearchPath }));
2010-01-26 22:37:48 +00:00
foreach (ParameterInfo parameterInfo in parameterInfos)
methodBuilder.DefineParameter(parameterInfo.Position + 1,
parameterInfo.Attributes, parameterInfo.Name);
if (dllImportAttribute.PreserveSig)
methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
return typeBuilder.CreateType();
}
}
}