19 using System.Collections.Generic;
20 using System.ComponentModel;
22 using System.Runtime.InteropServices;
34 public string DllPath {
get;
private set; }
36 public IntPtr Handle {
get;
private set; }
38 public int UseCount {
get;
private set; }
42 #region Public methods 46 Logger.Info(
"Load: Use count of '{0}' was {1}", DllPath, UseCount);
50 Logger.Info(
"Load: Loading DLL...");
52 if (!String.IsNullOrWhiteSpace(_expectedSha256))
54 if (!VerifyChecksum())
56 Logger.Fatal(
"LoadDll: Checksum mismatch!");
58 "DLL checksum error: expected {0} on {1}", _expectedSha256, DllPath));
62 Handle = LoadLibrary(DllPath);
64 if (Handle == IntPtr.Zero)
66 Logger.Fatal(
"Load: Unable to load DLL!");
67 Win32Exception inner =
new Win32Exception(Marshal.GetLastWin32Error());
71 "Could not load DLL file: LoadLibrary failed with code {0} on {1}",
72 Marshal.GetLastWin32Error(), SanitizeDllPath()
79 Logger.Info(
"Load: Handle: 0x{0}", Handle.ToString(
"X8"));
89 Logger.Info(
"Unload: Use count for '{0}' is {1}", DllPath, UseCount);
92 Logger.Warn(
"Unload: Method call not matched by Load() call!");
100 Logger.Info(
"Unload: No more users, freeing handle 0x{0}", Handle.ToString(
"X8"));
101 if (!FreeLibrary(Handle))
103 Logger.Warn(
"Unload: FreeLibrary returned false");
114 Logger.Info(
"Constructor: path: {0}", path);
117 Handle = IntPtr.Zero;
120 public DllFile(
string path,
string sha256hash)
123 _expectedSha256 = sha256hash;
135 public void Dispose()
138 GC.SuppressFinalize(
this);
141 private void Dispose(
bool disposing)
156 #region Private methods 163 private string SanitizeDllPath()
167 string[] dirs =
System.IO.Path.GetDirectoryName(DllPath).Split(
System.IO.Path.DirectorySeparatorChar);
168 string result = DllPath;
170 if (n > 0) result =
System.IO.Path.Combine(dirs[n - 1], result);
171 if (n > 1) result =
System.IO.Path.Combine(dirs[n - 2], result);
172 if (n > 2) result =
System.IO.Path.Combine(
"...", result);
173 Logger.Info(
"SanitizeDllPath: {0}", result);
177 private bool VerifyChecksum()
180 if (actual != _expectedSha256)
182 Logger.Warn(
"VerifyChecksum: Checksum failed for '{0}'", DllPath);
183 Logger.Warn(
"VerifyChecksum: Expected: {0}", _expectedSha256);
184 Logger.Warn(
"VerifyChecksum: Actual: {0}", actual);
189 Logger.Info(
"VerifyChecksum: Confirmed: {0}", _expectedSha256);
196 #region Private fields 198 private bool _disposed;
199 private string _expectedSha256;
205 [DllImport(
"kernel32.dll", EntryPoint =
"LoadLibrary", SetLastError =
true)]
206 static extern IntPtr LoadLibrary(
string dllToLoad);
208 [DllImport(
"kernel32.dll", EntryPoint =
"GetProcAddress", SetLastError =
true)]
209 static extern IntPtr GetProcAddress(IntPtr hModule,
string procedureName);
211 [DllImport(
"kernel32.dll", EntryPoint =
"FreeLibrary", SetLastError =
true)]
212 static extern bool FreeLibrary(IntPtr hModule);
218 private static NLog.Logger Logger {
get {
return _logger.Value; } }
220 private static readonly Lazy<
NLog.Logger> _logger =
new Lazy<
NLog.Logger>(() =>
NLog.LogManager.GetCurrentClassLogger());
static string Sha256Hash(string file)
Computes the Sha256 hash of a given file.
Represents a single DLL file.