19 using System.Collections.Generic;
23 using System.Runtime.InteropServices;
24 using System.ComponentModel;
33 #region Public static property 35 public static string AlternativeDir {
get;
set; }
39 #region Public static methods 50 Logger.Info(
"DllExists: {0}", dllFileName);
51 return !String.IsNullOrEmpty(LocateDll(dllFileName));
63 Logger.Info(
"LocateDll: {0}", dllName);
64 string dllPath = CompletePath(ApplicationDir(), dllName);
65 bool found = File.Exists(dllPath);
66 if (!found && !String.IsNullOrWhiteSpace(AlternativeDir))
68 Logger.Info(
"LocateDll: Looking in alternative directory '{0}'", AlternativeDir);
69 dllPath = CompletePath(AlternativeDir, dllName);
70 found = File.Exists(dllPath);
74 Logger.Info(
"LocateDll: Found");
79 Logger.Warn(
"LocateDll: {0} not found", dllName);
86 #region Private static methods 98 private static string CompletePath(
string baseDir,
string fileName)
100 if (!Path.HasExtension(fileName))
104 string s = Path.Combine(baseDir,
106 Environment.Is64BitProcess ?
"x64" :
"Win32",
108 Logger.Info(
"CompletePath: '{0}'", s);
112 private static string ApplicationDir()
114 return AppDomain.CurrentDomain.BaseDirectory;
119 #region Public methods 136 LoadDll(dllName, String.Empty);
155 public void LoadDll(
string dllName,
string expectedSha256Hash)
157 if (_dlls.Contains(dllName))
159 Logger.Info(
"LoadDll: '{0}' already registered in this instance", dllName);
163 Logger.Info(
"LoadDll: '{0}'", dllName);
165 string dllPath = LocateDll(dllName);
166 if (String.IsNullOrEmpty(dllPath))
168 Logger.Fatal(
"LoadDll: Unable to locate DLL!");
173 if (_globalDlls.TryGetValue(dllName, out dllFile))
175 Logger.Info(
"LoadDll: Already registered");
179 Logger.Info(
"LoadDll: Registering new DLL");
180 dllFile =
new DllFile(dllPath, expectedSha256Hash);
181 _globalDlls.Add(dllName, dllFile);
194 if (_dlls.Contains(dllName))
197 if (_globalDlls.TryGetValue(dllName, out dllFile))
199 Logger.Info(
"UnloadDll: Unloading '{0}'", dllName);
202 _globalDlls.Remove(dllName);
206 Logger.Warn(
"UnloadDll: Attempting to unload '{0}' which is not globally registered?!", dllName);
208 _dlls.Remove(dllName);
212 Logger.Warn(
"UnloadDll: '{0}' not known to this instance");
222 _dlls =
new List<string>();
228 Logger.Info(
"DllManager: Constructor invoked with loadDlls array");
229 foreach (
string dll
in loadDlls)
233 Logger.Info(
"DllManager: All loaded");
238 #region Destructing and disposing 245 public void Dispose()
248 GC.SuppressFinalize(
this);
251 protected virtual void Dispose(
bool calledFromDispose)
256 if (calledFromDispose)
259 Logger.Info(
"Dispose: Unloading {0} DLL(s).", _dlls.Count);
260 foreach (
string dll
in _dlls)
263 if (_globalDlls.TryGetValue(dll, out dllFile))
274 #region Private fields 276 private List<string> _dlls;
277 private bool _disposed;
281 #region Private static fields 286 private static readonly Dictionary<string, DllFile> _globalDlls =
new Dictionary<string, DllFile>();
290 #region Private constant 292 private const string LIBDIR =
"lib";
298 private static NLog.Logger Logger {
get {
return _logger.Value; } }
300 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
static bool DllExists(string dllFileName)
Checks whether a DLL exists in the application directory or in the custom AlternativeDir.
static string LocateDll(string dllName)
Attempts to locate a DLL file in the canonical location (subdirectory of the application directory) o...
void LoadDll(string dllName)
Loads the given DLL from the appropriate subdirectory, depending on the current bitness.
void UnloadDll(string dllName)
Unloads a previously loaded DLL.
void LoadDll(string dllName, string expectedSha256Hash)
Loads the given DLL from the appropriate subdirectory if its Sha1 hash matches the provided hash...
Represents a single DLL file.