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.
3 Version: MPL 1.1/GPL 2.0/LGPL 2.1
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
9 http://www.mozilla.org/MPL/
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.
15 The Original Code is the Open Hardware Monitor code.
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.
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.
39 using System.Collections.Generic;
40 using System.Reflection;
41 using System.Reflection.Emit;
42 using System.Runtime.InteropServices;
44 namespace OpenHardwareMonitor.Hardware {
46 public sealed class PInvokeDelegateFactory {
48 private static AssemblyBuilder assemblyBuilder;
49 private static ModuleBuilder moduleBuilder;
51 private static IDictionary<DllImportAttribute, Type> wrapperTypes =
52 new Dictionary<DllImportAttribute, Type>();
54 static PInvokeDelegateFactory() {
56 AssemblyName assemblyName = new AssemblyName();
57 assemblyName.Name = "PInvokeDelegateFactoryInternalAssembly";
60 AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName,
61 AssemblyBuilderAccess.Run);
63 moduleBuilder = assemblyBuilder.DefineDynamicModule(
64 "PInvokeDelegateFactoryInternalModule");
67 private PInvokeDelegateFactory() { }
69 public static void CreateDelegate<T>(DllImportAttribute dllImportAttribute,
70 out T newDelegate) where T : class
73 wrapperTypes.TryGetValue(dllImportAttribute, out wrapperType);
75 if (wrapperType == null) {
76 wrapperType = CreateWrapperType(typeof(T), dllImportAttribute);
77 wrapperTypes.Add(dllImportAttribute, wrapperType);
80 newDelegate = Delegate.CreateDelegate(typeof(T), wrapperType,
81 dllImportAttribute.EntryPoint) as T;
85 private static Type CreateWrapperType(Type delegateType,
86 DllImportAttribute dllImportAttribute) {
88 TypeBuilder typeBuilder = moduleBuilder.DefineType(
89 "PInvokeDelegateFactoryInternalWrapperType" + wrapperTypes.Count);
91 MethodInfo methodInfo = delegateType.GetMethod("Invoke");
93 ParameterInfo[] parameterInfos = methodInfo.GetParameters();
94 int parameterCount = parameterInfos.GetLength(0);
96 Type[] parameterTypes = new Type[parameterCount];
97 for (int i = 0; i < parameterCount; i++)
98 parameterTypes[i] = parameterInfos[i].ParameterType;
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);
108 foreach (ParameterInfo parameterInfo in parameterInfos)
109 methodBuilder.DefineParameter(parameterInfo.Position + 1,
110 parameterInfo.Attributes, parameterInfo.Name);
112 if (dllImportAttribute.PreserveSig)
113 methodBuilder.SetImplementationFlags(MethodImplAttributes.PreserveSig);
115 return typeBuilder.CreateType();