是否存在从本机dll作为字节数组加载到RAM的DllImport函数

本文关键字:加载 数组 RAM 函数 DllImport 字节数 字节 存在 本机 dll 是否 | 更新日期: 2023-09-27 18:20:49

不管听起来有多奇怪,我都需要它O_O好吧,另一个解决方案是,如果可以将本机DLL作为字节数组加载到RAM中,但在没有DllImport>_<

有点像这样:

byte[] dll_data = File.RealAllBytes("native.dll"); //this is just example, in real architecture bytes comes from another source and should never be stored on HDD
//uhh... ohh... do something to call exported function named "WeirdNativeFunction"

是否存在从本机dll作为字节数组加载到RAM的DllImport函数

您需要调用适当的方法来将本机DLL加载到调用进程中。GitHub上的MemoryModule项目提供了一个(本机)API来处理此问题,您可以从C++/CLI项目中使用它。

一旦将native.dll加载到进程中,就可以使用p/Invoke调用GetProcAddress来获取"WeirdNativeFunction"的句柄,并使用Marshal.GetDelegateForFunctionPointer将其转换为托管委托,然后可以调用该托管委托。