//----------------------------------------------------------------------- // // Copyright © 2012 Nils Hammar. All rights reserved. // //----------------------------------------------------------------------- /* * Software to access vehicle information via the OBD-II connector. * * Copyright © 2012 Nils Hammar * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program. If not, see . * * Alternative licensing is possible, see the licensing document. * * The above text may not be removed or modified. */ namespace DynamicApiLoading.Api.Dynamic { using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.IO; using System.Runtime.CompilerServices; using System.Runtime.ConstrainedExecution; using System.Runtime.InteropServices; using System.Security.Permissions; using Microsoft.Win32; using Microsoft.Win32.SafeHandles; ////[SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)] /// /// Call to unmanaged code to load library. /// public class SafeLibraryHandle : SafeHandleZeroOrMinusOneIsInvalid { /// /// Prevents a default instance of the class from being created. /// private SafeLibraryHandle() : base(true) { } /// /// Load unmanaged DLL library. /// /// Name and path of DLL to load. /// Handle to loaded library. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true, CallingConvention = CallingConvention.StdCall)] internal static extern SafeLibraryHandle LoadLibraryW([MarshalAs(UnmanagedType.LPWStr)] string dllname); /// /// Get address of procedure in library. /// /// Name of procedure to get address for. /// Address to procedure. internal IntPtr GetProcAddress(string procedureName) { return GetProcAddress(this.handle, procedureName); } /// /// Free the handle. /// /// false = fail, true = success. protected override bool ReleaseHandle() { return SafeLibraryHandle.FreeLibrary(this.handle); } /// /// Get address of procedure in library. /// /// Handle to library. /// Name of procedure to get address for. /// Address to procedure. [DllImport("kernel32.dll", CharSet = CharSet.Ansi, BestFitMapping = false, ThrowOnUnmappableChar = true)] private static extern IntPtr GetProcAddress([In] IntPtr hModule, [In, MarshalAs(UnmanagedType.LPStr)] string procedureName); /// /// Free a loaded library from memory. /// /// Handle to library. /// false = fail, true = success. [DllImport("kernel32.dll", CharSet = CharSet.Unicode, ExactSpelling = true, SetLastError = true, CallingConvention = CallingConvention.StdCall)] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool FreeLibrary(IntPtr hModule); } }