Skip to content

Commit 4a5e06b

Browse files
committed
feat: Added support for assemblyresolve event
1 parent fcd103e commit 4a5e06b

17 files changed

Lines changed: 389 additions & 35 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
##
44
## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore
55

6+
# Some test stuff:
7+
*.l
8+
*.o
9+
610
# User-specific files
711
*.rsuser
812
*.suo

DemoApp/DemoApp.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
<OutputType>Exe</OutputType>
55
<Platforms>AnyCPU;x86;x64</Platforms>
66
</PropertyGroup>
7+
<ItemGroup>
8+
<None Remove="Secret.dll" />
9+
<None Remove="Secret64.dll" />
10+
</ItemGroup>
711
<ItemGroup>
812
<Content Include="SampleDLL.dll">
913
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1014
</Content>
1115
<Content Include="SampleDLL64.dll">
1216
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
1317
</Content>
18+
<EmbeddedResource Include="Secret.dll">
19+
<CopyToOutputDirectory>Never</CopyToOutputDirectory>
20+
</EmbeddedResource>
21+
<EmbeddedResource Include="Secret64.dll" />
1422
</ItemGroup>
1523
<ItemGroup>
1624
<ProjectReference Include="..\MemoryModule\MemoryModule.csproj" />

DemoApp/Program.cs

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MemoryModule;
22
using System;
33
using System.IO;
4+
using System.Reflection;
45
using System.Runtime.InteropServices;
56

67
namespace DemoApp
@@ -18,13 +19,10 @@ static class Program
1819

1920
static int Main(string[] args)
2021
{
21-
if (Environment.Is64BitProcess)
22-
{
23-
Greet64();
24-
}
25-
2622
var asmBytes = File.ReadAllBytes($"SampleDLL{(Environment.Is64BitProcess ? "64" : "")}.dll");
2723

24+
NativeAssembly.AssemblyResolve += NativeAssembly_AssemblyResolve;
25+
2826
var asm = NativeAssembly.Load(asmBytes);
2927

3028
Console.WriteLine("Successfully loaded library.");
@@ -40,5 +38,28 @@ static int Main(string[] args)
4038
asm.Dispose();
4139
return 0;
4240
}
41+
42+
private static NativeAssembly NativeAssembly_AssemblyResolve(object sender, NativeResolveEventArgs e)
43+
{
44+
Console.WriteLine(e.RequestingAssembly);
45+
Console.WriteLine(e.Name);
46+
47+
string secretAsmName = $"Secret{(Environment.Is64BitProcess ? "64" : "")}.dll";
48+
if (e.Name.Equals(secretAsmName, StringComparison.InvariantCultureIgnoreCase))
49+
{
50+
using (var resourceStream =
51+
Assembly.GetExecutingAssembly().GetManifestResourceStream($"DemoApp.{secretAsmName}"))
52+
{
53+
var asm = NativeAssembly.Load(resourceStream, secretAsmName);
54+
// We are returning a newly-loaded assembly,
55+
// so the resolver should dispose this library,
56+
// and decrement the reference count.
57+
e.ShouldDisposeAssembly = true;
58+
return asm;
59+
}
60+
}
61+
62+
return null;
63+
}
4364
}
4465
}

DemoApp/SampleDLL.dll

440 KB
Binary file not shown.

DemoApp/SampleDLL.l

-1.53 KB
Binary file not shown.

DemoApp/SampleDLL64.dll

858 KB
Binary file not shown.

DemoApp/SampleDll.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
#include <iostream>
2+
#include "Secret.h"
23

34
extern "C"
45
{
56
__declspec(dllexport) __cdecl void Greet()
67
{
78
std::cout << "Hello World!" << std::endl;
9+
std::cout << "Here is my secret: " << GetSecret() << std::endl;
810
}
911

1012
__declspec(dllexport) __cdecl int addNumbers(int a, int b)

DemoApp/SampleDll.o

-81.9 KB
Binary file not shown.

DemoApp/Secret.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <random>
2+
#include "Secret.h"
3+
4+
std::random_device rd;
5+
std::uniform_int_distribution dist(0, 100);
6+
7+
extern "C"
8+
{
9+
__declspec(dllexport) __cdecl int GetSecret()
10+
{
11+
return dist(rd);
12+
}
13+
}

DemoApp/Secret.dll

214 KB
Binary file not shown.

0 commit comments

Comments
 (0)