資源簡介
純C# 實現的 從內存加載動態鏈接庫,支持加殼的DLL,使用方便
class Program
{
public delegate Int32 CompressHandle(ref Byte dest,ref Int32 len, Byte [] source,Int32 sourcelen);
static void Main(string[] args)
{
Byte[] source = new byte[10000];
Byte[] dest = new byte[10000];
Int32 len = source.Length;
Byte[] dllBin = File.ReadAllBytes("zlib1.dll");
using (var dll = new DllLoader())
{
if (dll.LoadLibrary(dllBin))
{
var Compress = dll.GetProcDelegate("compress");
if (Compress != null)
{
var result = Compress.Invoke(ref dest[0], ref len, source, len);
Console.WriteLine(result);
}
}
}
}
}

代碼片段和文件信息
using?System;
using?System.IO;
using?System.Runtime.InteropServices;
using?InvokeDll.Win32;
namespace?InvokeDll
{
????///?
????///?DllMain函數委托
????///?
????///?
????///?
????///?
????///?
????public?delegate?Boolean?DllMainHandle(IntPtr?hModule?UInt32?ul_reason_for_call?IntPtr?lpReserved);
????///?
????///?動態鏈接庫內存加載器
????///?
????public?class?DllLoader?:?IDisposable
????{
????????///?
????????///?從Byte數組加載Dll
????????///?
????????///?
????????///?
????????public?Boolean?LoadLibrary(Byte[]?data)
????????{
????????????IntPtr?buffer?=?IntPtr.Zero;
????????????try
????????????{
????????????????buffer?=?Marshal.AllocHGlobal(data.Length);
????????????????Marshal.Copy(data?0?buffer?data.Length);
????????????????return?LoadDll(buffer?data.Length);
????????????}
????????????catch?(Exception?ex)
????????????{
????????????????if?(hModule?!=?IntPtr.Zero)
????????????????{
????????????????????API.VirtualFreeEx(-1?hModule?hModuleSize?API.MEM_RELEASE);
????????????????????hModule?=?IntPtr.Zero;
????????????????}
????????????????throw?ex;
????????????}
????????????finally
????????????{
????????????????Marshal.FreeHGlobal(buffer);
????????????}
????????}
????????///?
????????///?加載dLL
????????///?
????????///?
????????///?
????????///?
????????private?unsafe?Boolean?LoadDll(IntPtr?localPtr?Int32?DataLength)
????????{
????????????IMAGE_DOS_HEADER*?dosHeader?=?(IMAGE_DOS_HEADER*)localPtr;
????????????IMAGE_NT_HEADERS*?peHeader?=?(IMAGE_NT_HEADERS*)(localPtr?+?dosHeader->e_lfanew);
????????????IMAGE_SECTION_HEADER*?sectionHeader?=?(IMAGE_SECTION_HEADER*)(localPtr?+?dosHeader->e_lfanew?+?sizeof(IMAGE_NT_HEADERS));
????????????if?(dosHeader->e_magic?!=?0x5A4D)
????????????{
????????????????throw?new?Exception(“DOS?file?format?error“);
????????????}
????????????if?(DataLength?e_lfanew?+?sizeof(IMAGE_DOS_HEADER))
????????????{
????????????????throw?new?Exception(“DOS?file?header?data?error“);
????????????}
????????????if?(peHeader->Signature?!=?API.IMAGE_NT_SIGNATURE)
????????????{
????????????????throw?new?Exception(“windows?file?Signature?error“);
????????????}
????????????if?((peHeader->FileHeader.Characteristics?&?API.IMAGE_FILE_DLL)?!=?API.IMAGE_FILE_DLL)
????????????{
????????????????throw?new?Exception(“Dll?Not?dynamic?library“);
????????????}
????????????for?(int?i?=?0;?i?FileHeader.NumberOfSections;?i++)
????????????{
????????????????if?(sectionHeader->PointerToRawData?+?sectionHeader->SizeOfRawData?>?DataLength)
????????????????{
????????????????????throw?new?Exception(“Section?data?error“);
????????????????}
????????????}
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2018-09-08?08:53??InvokeDll\
?????目錄???????????0??2018-09-08?08:53??InvokeDll\.vs\
?????目錄???????????0??2018-09-08?16:11??InvokeDll\.vs\InvokeDll\
?????目錄???????????0??2018-09-08?16:11??InvokeDll\.vs\InvokeDll\DesignTimeBuild\
?????文件?????????226??2018-09-08?16:11??InvokeDll\.vs\InvokeDll\DesignTimeBuild\.dtbcache
?????目錄???????????0??2018-09-08?08:53??InvokeDll\.vs\InvokeDll\v15\
?????文件???????52224??2018-09-08?16:11??InvokeDll\.vs\InvokeDll\v15\.suo
?????目錄???????????0??2018-09-08?08:53??InvokeDll\.vs\InvokeDll\v15\Server\
?????目錄???????????0??2018-09-08?08:53??InvokeDll\.vs\InvokeDll\v15\Server\sqlite3\
?????文件???????????0??2018-09-08?08:53??InvokeDll\.vs\InvokeDll\v15\Server\sqlite3\db.lock
?????文件??????483328??2018-09-08?15:57??InvokeDll\.vs\InvokeDll\v15\Server\sqlite3\storage.ide
?????文件???????32768??2018-09-08?11:22??InvokeDll\.vs\InvokeDll\v15\Server\sqlite3\storage.ide-shm
?????文件?????4152992??2018-09-08?16:11??InvokeDll\.vs\InvokeDll\v15\Server\sqlite3\storage.ide-wal
?????目錄???????????0??2018-09-08?16:09??InvokeDll\InvokeDll\
?????文件?????????184??2018-09-08?16:02??InvokeDll\InvokeDll\App.config
?????目錄???????????0??2018-09-08?16:02??InvokeDll\InvokeDll\bin\
?????目錄???????????0??2018-09-08?16:07??InvokeDll\InvokeDll\bin\Debug\
?????文件???????28672??2018-09-08?16:09??InvokeDll\InvokeDll\bin\Debug\InvokeDll.exe
?????文件?????????184??2018-09-08?16:02??InvokeDll\InvokeDll\bin\Debug\InvokeDll.exe.config
?????文件???????69120??2018-09-08?16:09??InvokeDll\InvokeDll\bin\Debug\InvokeDll.pdb
?????文件?????1426944??2016-08-12?07:58??InvokeDll\InvokeDll\bin\Debug\Msvbvm60.dll
?????文件??????180832??2012-07-10?12:07??InvokeDll\InvokeDll\bin\Debug\System.Net.Http.dll
?????文件???????72192??2012-05-11?16:39??InvokeDll\InvokeDll\bin\Debug\zlib.dll
?????文件???????42496??2015-01-10?20:53??InvokeDll\InvokeDll\bin\Debug\zlib1.dll
?????目錄???????????0??2018-09-08?10:46??InvokeDll\InvokeDll\bin\Release\
?????目錄???????????0??2018-09-08?16:02??InvokeDll\InvokeDll\bin\x86\
?????目錄???????????0??2018-09-08?16:02??InvokeDll\InvokeDll\bin\x86\Debug\
?????文件???????14641??2018-09-08?15:57??InvokeDll\InvokeDll\DllLoader.cs
?????文件????????3598??2018-09-08?16:02??InvokeDll\InvokeDll\InvokeDll.csproj
?????目錄???????????0??2018-09-08?16:02??InvokeDll\InvokeDll\obj\
?????目錄???????????0??2018-09-08?16:01??InvokeDll\InvokeDll\obj\Debug\
............此處省略28個文件信息
評論
共有 條評論