Hardware/PInvokeDelegateFactory.cs
author moel.mich
Sun, 17 Oct 2010 16:10:08 +0000
changeset 229 940d86b60be7
parent 167 b7cc9d09aefe
child 344 3145aadca3d2
permissions -rw-r--r--
Renamed folder Wmi to WMI.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Reflection;
    41 using System.Reflection.Emit;
    42 using System.Runtime.InteropServices;
    43 
    44 namespace OpenHardwareMonitor.Hardware {
    45 
    46   internal static class PInvokeDelegateFactory {
    47 
    48     private static readonly ModuleBuilder moduleBuilder = 
    49       AppDomain.CurrentDomain.DefineDynamicAssembly(
    50         new AssemblyName("PInvokeDelegateFactoryInternalAssembly"),
    51         AssemblyBuilderAccess.Run).DefineDynamicModule(
    52         "PInvokeDelegateFactoryInternalModule");
    53 
    54     private static readonly IDictionary<DllImportAttribute, Type> wrapperTypes =
    55       new Dictionary<DllImportAttribute, Type>();
    56 
    57     public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
    58       out T newDelegate) where T : class 
    59     {
    60       Type wrapperType;
    61       wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
    62 
    63       if (wrapperType == null) {
    64         wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
    65         wrapperTypes.Add(dllImportAttribute, wrapperType);
    66       }
    67 
    68       newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
    69         dllImportAttribute.EntryPoint) as T;
    70     }
    71 
    72 
    73     private static Type CreateWrapperType(Type delegateType,
    74       DllImportAttribute dllImportAttribute) {
    75 
    76       TypeBuilder typeBuilder = moduleBuilder.DefineType(
    77         "PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count);
    78 
    79       MethodInfo methodInfo = delegateType.GetMethod("Invoke");
    80 
    81       ParameterInfo[] parameterInfos = methodInfo.GetParameters();
    82       int parameterCount = parameterInfos.GetLength(0);
    83 
    84       Type[] parameterTypes = new Type[parameterCount];
    85       for (int i = 0; i < parameterCount; i++)
    86         parameterTypes[i] = parameterInfos[i].ParameterType;
    87 
    88       MethodBuilder methodBuilder = typeBuilder.DefinePInvokeMethod(
    89         dllImportAttribute.EntryPoint, dllImportAttribute.Value,
    90         MethodAttributes.Public | MethodAttributes.Static |
    91         MethodAttributes.PinvokeImpl, CallingConventions.Standard,
    92         methodInfo.ReturnType, parameterTypes,
    93         dllImportAttribute.CallingConvention,
    94         dllImportAttribute.CharSet);
    95 
    96       foreach (ParameterInfo parameterInfo in parameterInfos)
    97         methodBuilder.DefineParameter(parameterInfo.Position + 1,
    98           parameterInfo.Attributes, parameterInfo.Name);
    99 
   100       if (dllImportAttribute.PreserveSig)
   101         methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
   102 
   103       return typeBuilder.CreateType();
   104     }
   105   }
   106 }