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