Hardware/PInvokeDelegateFactory.cs
author moel.mich
Wed, 25 Jul 2012 18:10:10 +0000
changeset 375 01d283b11596
parent 195 0ee888c485d5
child 384 76f859f4aea1
permissions -rw-r--r--
Updated the version.
moel@165
     1
/*
moel@165
     2
 
moel@344
     3
  This Source Code Form is subject to the terms of the Mozilla Public
moel@344
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
moel@344
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
moel@165
     6
 
moel@344
     7
  Copyright (C) 2009-2010 Michael Möller <mmoeller@openhardwaremonitor.org>
moel@344
     8
	
moel@165
     9
*/
moel@165
    10
moel@165
    11
using System;
moel@165
    12
using System.Collections.Generic;
moel@165
    13
using System.Reflection;
moel@165
    14
using System.Reflection.Emit;
moel@165
    15
using System.Runtime.InteropServices;
moel@165
    16
moel@165
    17
namespace OpenHardwareMonitor.Hardware {
moel@165
    18
moel@195
    19
  internal static class PInvokeDelegateFactory {
moel@165
    20
moel@195
    21
    private static readonly ModuleBuilder moduleBuilder = 
moel@167
    22
      AppDomain.CurrentDomain.DefineDynamicAssembly(
moel@167
    23
        new AssemblyName("PInvokeDelegateFactoryInternalAssembly"),
moel@167
    24
        AssemblyBuilderAccess.Run).DefineDynamicModule(
moel@167
    25
        "PInvokeDelegateFactoryInternalModule");
moel@165
    26
moel@195
    27
    private static readonly IDictionary<DllImportAttribute, Type> wrapperTypes =
moel@165
    28
      new Dictionary<DllImportAttribute, Type>();
moel@165
    29
moel@165
    30
    public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
moel@165
    31
      out T newDelegate) where T : class 
moel@165
    32
    {
moel@165
    33
      Type wrapperType;
moel@165
    34
      wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
moel@165
    35
moel@165
    36
      if (wrapperType == null) {
moel@165
    37
        wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
moel@165
    38
        wrapperTypes.Add(dllImportAttribute, wrapperType);
moel@165
    39
      }
moel@165
    40
moel@165
    41
      newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
moel@165
    42
        dllImportAttribute.EntryPoint) as T;
moel@165
    43
    }
moel@165
    44
moel@165
    45
moel@165
    46
    private static Type CreateWrapperType(Type delegateType,
moel@165
    47
      DllImportAttribute dllImportAttribute) {
moel@165
    48
moel@165
    49
      TypeBuilder typeBuilder = moduleBuilder.DefineType(
moel@165
    50
        "PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count);
moel@165
    51
moel@165
    52
      MethodInfo methodInfo = delegateType.GetMethod("Invoke");
moel@165
    53
moel@165
    54
      ParameterInfo[] parameterInfos = methodInfo.GetParameters();
moel@165
    55
      int parameterCount = parameterInfos.GetLength(0);
moel@165
    56
moel@165
    57
      Type[] parameterTypes = new Type[parameterCount];
moel@165
    58
      for (int i = 0; i < parameterCount; i++)
moel@165
    59
        parameterTypes[i] = parameterInfos[i].ParameterType;
moel@165
    60
moel@165
    61
      MethodBuilder methodBuilder = typeBuilder.DefinePInvokeMethod(
moel@165
    62
        dllImportAttribute.EntryPoint, dllImportAttribute.Value,
moel@165
    63
        MethodAttributes.Public | MethodAttributes.Static |
moel@165
    64
        MethodAttributes.PinvokeImpl, CallingConventions.Standard,
moel@165
    65
        methodInfo.ReturnType, parameterTypes,
moel@165
    66
        dllImportAttribute.CallingConvention,
moel@165
    67
        dllImportAttribute.CharSet);
moel@165
    68
moel@165
    69
      foreach (ParameterInfo parameterInfo in parameterInfos)
moel@165
    70
        methodBuilder.DefineParameter(parameterInfo.Position + 1,
moel@165
    71
          parameterInfo.Attributes, parameterInfo.Name);
moel@165
    72
moel@165
    73
      if (dllImportAttribute.PreserveSig)
moel@165
    74
        methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
moel@165
    75
moel@165
    76
      return typeBuilder.CreateType();
moel@165
    77
    }
moel@165
    78
  }
moel@165
    79
}