Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.ExtLibs/HeuristicLab.OrTools/7.0.0/HeuristicLab.OrTools-7.0.0/Plugin.cs.frame @ 16940

Last change on this file since 16940 was 16940, checked in by ddorfmei, 5 years ago

#2931:

  • Added StorableType attribute to interfaces and classes where it was missing
  • Moved code from HeuristicLabOrToolsPlugin.OnUnload() to finalizer because native DLL was unloaded to early
File size: 3.3 KB
RevLine 
[16070]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
[16582]23using System.Diagnostics;
[16288]24using System.IO;
25using System.Linq;
26using System.Runtime.InteropServices;
[16070]27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.OrTools {
[16288]30
[16717]31  [Plugin("HeuristicLab.OrTools", "Provides functionality of Google OR-Tools in HeuristicLab. Requires Windows 64-bit", "7.0.0.$WCREV$")]
32  [PluginFile("HeuristicLab.OrTools-7.0.0.dll", PluginFileType.Assembly)]
[16070]33  [PluginFile("Google.OrTools.dll", PluginFileType.Assembly)]
34  [PluginFile("Google.OrTools-license.txt", PluginFileType.License)]
[16233]35  [PluginFile("Google.OrTools.runtime.win-x64.dll", PluginFileType.NativeDll)]
[16070]36  [PluginFile("Google.OrTools_version.txt", PluginFileType.Data)]
[16910]37  [PluginDependency("HeuristicLab.Protobuf", "3.6.1")]
[16070]38  public class HeuristicLabOrToolsPlugin : PluginBase {
[16288]39
[16940]40    ~HeuristicLabOrToolsPlugin() {
41      // HACK: Free handle to native DLL used temporarily by the Hive Slave.
42      // Finalizer must be used because generated finalizers used in
43      // HeuristicLab.ExactOptimization that call destructors in native DLL
44      // are called after HeuristicLabOrToolsPlugin.OnUnload().
45      // This should be solved for all native DLLs used by the Hive Slave.
[16373]46      var dllDir = new FileInfo(GetType().Assembly.Location).Directory;
[16582]47      if (dllDir == null || !dllDir.FullName.Contains(Path.DirectorySeparatorChar + "PluginTemp" + Path.DirectorySeparatorChar))
[16288]48        return;
49
[16373]50      var nativeDlls = GetType().GetCustomAttributes(typeof(PluginFileAttribute), true)
[16288]51        .Cast<PluginFileAttribute>()
52        .Where(pf => pf.FileType == PluginFileType.NativeDll)
53        .Select(pf => pf.FileName);
54
[16373]55      foreach (var nativeDll in dllDir.EnumerateFiles().Where(f => nativeDlls.Contains(f.Name))) {
[16582]56        if (Process.GetCurrentProcess().Modules.Cast<ProcessModule>().All(m => m.ModuleName != nativeDll.Name))
57          continue;
58
[16288]59        var handle = LoadLibrary(nativeDll.FullName);
60        if (handle == IntPtr.Zero)
61          continue;
62
63        FreeLibrary(handle); // close handle obtained above
64        FreeLibrary(handle); // close implicitly obtained handle
65      }
66    }
67
68    [DllImport("kernel32.dll", SetLastError = true)]
69    [return: MarshalAs(UnmanagedType.Bool)]
70    private static extern bool FreeLibrary(IntPtr hModule);
71
72    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
73    private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);
[16070]74  }
75}
Note: See TracBrowser for help on using the repository browser.