Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2931_OR-Tools_LP_MIP/HeuristicLab.ExtLibs/HeuristicLab.OrTools/7.0.0/HeuristicLab.OrTools-7.0.0/Plugin.cs.frame @ 16804

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

#2931: Added dependency to HeuristicLab.Protobuf for HeuristicLab.OrTools

File size: 3.1 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    public override void OnUnload() {
41      // HACK: free handle to native DLL used temporarily by the Hive Slave
42      // should be solved for all native DLLs used by the Hive Slave
43      var dllDir = new FileInfo(GetType().Assembly.Location).Directory;
44      if (dllDir == null || !dllDir.FullName.Contains(Path.DirectorySeparatorChar + "PluginTemp" + Path.DirectorySeparatorChar))
45        return;
46
47      var nativeDlls = GetType().GetCustomAttributes(typeof(PluginFileAttribute), true)
48        .Cast<PluginFileAttribute>()
49        .Where(pf => pf.FileType == PluginFileType.NativeDll)
50        .Select(pf => pf.FileName);
51
52      foreach (var nativeDll in dllDir.EnumerateFiles().Where(f => nativeDlls.Contains(f.Name))) {
53        if (Process.GetCurrentProcess().Modules.Cast<ProcessModule>().All(m => m.ModuleName != nativeDll.Name))
54          continue;
55
56        var handle = LoadLibrary(nativeDll.FullName);
57        if (handle == IntPtr.Zero)
58          continue;
59
60        FreeLibrary(handle); // close handle obtained above
61        FreeLibrary(handle); // close implicitly obtained handle
62      }
63    }
64
65    [DllImport("kernel32.dll", SetLastError = true)]
66    [return: MarshalAs(UnmanagedType.Bool)]
67    private static extern bool FreeLibrary(IntPtr hModule);
68
69    [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
70    private static extern IntPtr LoadLibrary([MarshalAs(UnmanagedType.LPStr)]string lpFileName);
71  }
72}
Note: See TracBrowser for help on using the repository browser.