Utilities/PInvokeDelegateFactory.cs
author moel.mich
Tue, 25 May 2010 22:33:03 +0000
changeset 128 cea5477b4d72
permissions -rw-r--r--
Added an event handler to save the configuration when the user logs off without closing the application first (http://blogs.msdn.com/b/oldnewthing/archive/2008/04/21/8413175.aspx), because FormClosed is not called in that case.
     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   public sealed class PInvokeDelegateFactory {
    47 
    48     private static AssemblyBuilder assemblyBuilder;
    49     private static ModuleBuilder moduleBuilder;
    50 
    51     private static IDictionary<DllImportAttribute, Type> wrapperTypes =
    52       new Dictionary<DllImportAttribute, Type>();
    53 
    54     static PInvokeDelegateFactory() {
    55 
    56       AssemblyName assemblyName = new AssemblyName();
    57       assemblyName.Name = "PInvokeDelegateFactoryInternalAssembly";
    58 
    59       assemblyBuilder =
    60         AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
    61         AssemblyBuilderAccess.Run);
    62 
    63       moduleBuilder = assemblyBuilder.DefineDynamicModule(
    64         "PInvokeDelegateFactoryInternalModule");
    65     }
    66 
    67     private PInvokeDelegateFactory() { }
    68 
    69     public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
    70       out T newDelegate) where T : class 
    71     {
    72       Type wrapperType;
    73       wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
    74 
    75       if (wrapperType == null) {
    76         wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
    77         wrapperTypes.Add(dllImportAttribute, wrapperType);
    78       }
    79 
    80       newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
    81         dllImportAttribute.EntryPoint) as T;
    82     }
    83 
    84 
    85     private static Type CreateWrapperType(Type delegateType,
    86       DllImportAttribute dllImportAttribute) {
    87 
    88       TypeBuilder typeBuilder = moduleBuilder.DefineType(
    89         "PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count);
    90 
    91       MethodInfo methodInfo = delegateType.GetMethod("Invoke");
    92 
    93       ParameterInfo[] parameterInfos = methodInfo.GetParameters();
    94       int parameterCount = parameterInfos.GetLength(0);
    95 
    96       Type[] parameterTypes = new Type[parameterCount];
    97       for (int i = 0; i < parameterCount; i++)
    98         parameterTypes[i] = parameterInfos[i].ParameterType;
    99 
   100       MethodBuilder methodBuilder = typeBuilder.DefinePInvokeMethod(
   101         dllImportAttribute.EntryPoint, dllImportAttribute.Value,
   102         MethodAttributes.Public | MethodAttributes.Static |
   103         MethodAttributes.PinvokeImpl, CallingConventions.Standard,
   104         methodInfo.ReturnType, parameterTypes,
   105         dllImportAttribute.CallingConvention,
   106         dllImportAttribute.CharSet);
   107 
   108       foreach (ParameterInfo parameterInfo in parameterInfos)
   109         methodBuilder.DefineParameter(parameterInfo.Position + 1,
   110           parameterInfo.Attributes, parameterInfo.Name);
   111 
   112       if (dllImportAttribute.PreserveSig)
   113         methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
   114 
   115       return typeBuilder.CreateType();
   116     }
   117   }
   118 }