source: stable/HeuristicLab.ExtLibs/HeuristicLab.OrTools/7.0.0/HeuristicLab.OrTools-7.0.0/Plugin.cs.frame @ 17173

Last change on this file since 17173 was 17173, checked in by abeham, 2 months ago

#2931: merged r16940 to stable

File size: 3.3 KB
Line 
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;
23using System.Diagnostics;
24using System.IO;
25using System.Linq;
26using System.Runtime.InteropServices;
27using HeuristicLab.PluginInfrastructure;
28
29namespace HeuristicLab.OrTools {
30
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)]
33  [PluginFile("Google.OrTools.dll", PluginFileType.Assembly)]
34  [PluginFile("Google.OrTools-license.txt", PluginFileType.License)]
35  [PluginFile("Google.OrTools.runtime.win-x64.dll", PluginFileType.NativeDll)]
36  [PluginFile("Google.OrTools_version.txt", PluginFileType.Data)]
37  [PluginDependency("HeuristicLab.Protobuf", "3.6.1")]
38  public class HeuristicLabOrToolsPlugin : PluginBase {
39
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.
46      var dllDir = new FileInfo(GetType().Assembly.Location).Directory;
47      if (dllDir == null || !dllDir.FullName.Contains(Path.DirectorySeparatorChar + "PluginTemp" + Path.DirectorySeparatorChar))
48        return;
49
50      var nativeDlls = GetType().GetCustomAttributes(typeof(PluginFileAttribute), true)
51        .Cast<PluginFileAttribute>()
52        .Where(pf => pf.FileType == PluginFileType.NativeDll)
53        .Select(pf => pf.FileName);
54
55      foreach (var nativeDll in dllDir.EnumerateFiles().Where(f => nativeDlls.Contains(f.Name))) {
56        if (Process.GetCurrentProcess().Modules.Cast<ProcessModule>().All(m => m.ModuleName != nativeDll.Name))
57          continue;
58
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);
74  }
75}
Note: See TracBrowser for help on using the repository browser.