将 Dll 嵌入并加载到项目中 - VB.NET / C#

发布时间:2021-03-07 13:05

我目前正在编写一个分析程序来检查木马和恶意软件。 为此,我还使用 Fiddler Core 来拦截和分析这些木马的流量。

Fiddler Core 还附带 3 个 dll,它们需要与程序本身位于同一启动文件夹中。因为我希望我的 Startup 文件夹只有 Exe,而不显示任何额外的 dll 我将这 3 个 dll 作为资源嵌入到我的项目中并在 ApplicationEvents 中加载它们。

这种方式适用于除 CertMaker.dll 之外的所有 DLL。 它负责创建拦截请求所需的根证书。

如果我嵌入 CertMaker.dll,它会一直工作到创建根证书的程度,但它不会拦截请求。我无法获得任何查询。它仅在我将 CertMaker.dll 从内存写入启动文件夹时才有效,但正如我上面写的,我希望启动文件夹仅为 exe。 >

这是我的代码,以备不时之需:

ApplicationEvents.vb:

Namespace My
Partial Friend Class MyApplication
    Private WithEvents Domain As AppDomain = AppDomain.CurrentDomain
    Private Function DomainCheck(sender As Object, e As System.ResolveEventArgs) As System.Reflection.Assembly Handles Domain.AssemblyResolve
        If e.Name.Contains("BCMakeCert") Then
            Return System.Reflection.Assembly.Load(My.Resources.Dll1)
        ElseIf e.Name.Contains("CertMaker") Then
            Return System.Reflection.Assembly.Load(My.Resources.Dll2)
        ElseIf e.Name.Contains("FiddlerCore4") Then
            Return System.Reflection.Assembly.Load(My.Resources.Dll3)
        Else
            Return Nothing
        End If
    End Function
End Class
End Namespace

仅当我将 CertMaker.dll 写入启动文件夹时才有效:

 Dim appPath As String = Application.StartupPath()
 File.WriteAllBytes(appPath & "/CertMaker.dll", My.Resources.Dll2)

是否有另一种方法可以在项目中包含和加载 dll,而 dll 不会出现在同一个启动文件夹中?我希望能够稍后将程序作为文件发送,而 dll 不在文件夹中。

回答1

如果提琴手的程序集是基于 .Net 的,请使用 AppDomain.Current.Load(assembly) 方法。