-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDllMap.cs
More file actions
48 lines (41 loc) · 1.72 KB
/
DllMap.cs
File metadata and controls
48 lines (41 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Xml.Linq;
namespace accEncoderTest
{
public static class DllMap
{
// Register a call-back for native library resolution.
public static void Register(Assembly assembly)
{
NativeLibrary.SetDllImportResolver(assembly, MapAndLoad);
}
// The callback: which loads the mapped library in place of the original
private static IntPtr MapAndLoad(string libraryName, Assembly assembly, DllImportSearchPath? dllImportSearchPath)
{
string mappedName = null;
mappedName = MapLibraryName(assembly.Location, libraryName, out mappedName) ? mappedName : libraryName;
return NativeLibrary.Load(mappedName, assembly, dllImportSearchPath);
}
// Parse the assembly.xml file, and map the old name to the new name of a library.
private static bool MapLibraryName(string assemblyLocation, string originalLibName, out string mappedLibName)
{
string xmlPath = Path.Combine(Path.GetDirectoryName(assemblyLocation),
Path.GetFileNameWithoutExtension(assemblyLocation) + ".xml");
mappedLibName = null;
if (!File.Exists(xmlPath))
return false;
XElement root = XElement.Load(xmlPath);
var map =
(from el in root.Elements("dllmap")
where (string) el.Attribute("dll") == originalLibName
select el).SingleOrDefault();
if (map != null)
mappedLibName = map.Attribute("target").Value;
return (mappedLibName != null);
}
}
}