# dnlib [![NuGet](https://img.shields.io/nuget/v/dnlib.svg)](https://www.nuget.org/packages/dnlib/) [![](https://github.com/0xd4d/dnlib/workflows/GitHub%20CI/badge.svg)](https://github.com/0xd4d/dnlib/actions)
.NET module/assembly reader/writer library
Opening a .NET assembly/module
------------------------------
First of all, the important namespaces are `dnlib.DotNet` and `dnlib.DotNet.Emit`. `dnlib.DotNet.Emit` is only needed if you intend to read/write method bodies. All the examples below assume you have the appropriate using statements at the top of each source file:
```C#
using dnlib.DotNet;
using dnlib.DotNet.Emit;
```
ModuleDefMD is the class that is created when you open a .NET module. It has several `Load()` methods that will create a ModuleDefMD instance. If it's not a .NET module/assembly, a `BadImageFormatException` will be thrown.
Read a .NET module from a file:
```C#
// Create a default assembly resolver and type resolver and pass it to Load().
// If it's a .NET Core assembly, you'll need to disable GAC loading and add
// .NET Core reference assembly search paths.
ModuleContext modCtx = ModuleDef.CreateModuleContext();
ModuleDefMD module = ModuleDefMD.Load(@"C:\path\to\file.exe", modCtx);
```
Read a .NET module from a byte array:
```C#
byte[] data = System.IO.File.ReadAllBytes(@"C:\path\of\file.dll");
// See comment above about the assembly resolver
ModuleContext modCtx = ModuleDef.CreateModuleContext();
ModuleDefMD module = ModuleDefMD.Load(data, modCtx);
```
You can also pass in a Stream instance, an address in memory (HINSTANCE) or even a System.Reflection.Module instance:
```C#
System.Reflection.Module reflectionModule = typeof(void).Module; // Get mscorlib.dll's module
// See comment above about the assembly resolver
ModuleContext modCtx = ModuleDef.CreateModuleContext();
ModuleDefMD module = ModuleDefMD.Load(reflectionModule, modCtx);
```
To get the assembly, use its Assembly property:
```C#
AssemblyDef asm = module.Assembly;
Console.WriteLine("Assembly: {0}", asm);
```
If it's an obfuscated Unity/Mono assembly, you need to create a `ModuleCreationOptions` instance and write `CLRRuntimeReaderKind.Mono` to `ModuleCreationOptions.Runtime` and pass in this `ModuleCreationOptions` instance to one of the `ModuleDefMD.Load(...)` methods.
Saving a .NET assembly/module
-----------------------------
Use `module.Write()`. It can save the assembly to a file or a Stream.
```C#
module.Write(@"C:\saved-assembly.dll");
```
If it's a C++/CLI assembly, you should use `NativeWrite()`
```C#
module.NativeWrite(@"C:\saved-assembly.dll");
```
To detect it at runtime, use this code:
```C#
if (module.IsILOnly) {
// This assembly has only IL code, and no native code (eg. it's a C# or VB assembly)
module.Write(@"C:\saved-assembly.dll");
}
else {
// This assembly has native code (eg. C++/CLI)
module.NativeWrite(@"C:\saved-assembly.dll");
}
```
PDB files
---------
PDB files are read from disk by default. You can change this behaviour by creating a `ModuleCreationOptions` and passing it in to the code that creates a module.
To save a PDB file, create a `ModuleWriterOptions` / `NativeModuleWriterOptions` and set its `WritePdb` property to `true`. By default, it will create a PDB file with the same name as the output assembly but with a `.pdb` extension. You can override this by writing the PDB file name to `PdbFileName` or writing your own stream to `PdbStream`. If `PdbStream` is initialized, `PdbFileName` should also be initialized because the name of the PDB file will be written to the PE file.
```C#
// Create a default assembly resolver and type resolver
ModuleContext modCtx = ModuleDef.CreateModuleContext();
var mod = ModuleDefMD.Load(@"C:\myfile.dll", modCtx);
// ...
var wopts = new dnlib.DotNet.Writer.ModuleWriterOptions(mod);
wopts.WritePdb = true;
// wopts.PdbFileName = @"C:\out2.pdb"; // Set other file name
mod.Write(@"C:\out.dll", wopts);
```
dnlib supports Windows PDBs, portable PDBs and embedded portable PDBs.
Windows PDBs
------------
It's only possible to write Windows PDBs on Windows (portable PDBs can be written on any OS). dnlib has a managed Windows PDB reader that supports all OSes.
There are two *native* Windows PDB reader and writer implementations, the old `diasymreader.dll` that ships with .NET Framework and `Microsoft.DiaSymReader.Native` which has been updated with more features and bug fixes.
dnlib will use `Microsoft.DiaSymReader.Native` if it exists and fall back to `diasymreader.dll` if needed. `PdbReaderOptions` and `PdbWriterOptions` can be used to disable one of them.
`Microsoft.DiaSymReader.Native` is a NuGet package with 32-bit and 64-bit native DLLs. You have to add a reference to this NuGet package if you want dnlib to use it. dnlib doesn't add a reference to it.
Strong name signing an assembly
-------------------------------
Use the following code to strong name sign the assembly when saving it:
```C#
using dnlib.DotNet.Writer;
...
// Open or create an assembly
ModuleDef mod = ModuleDefMD.Load(.....);
// Create writer options
var opts = new ModuleWriterOptions(mod);
// Open or create the strong name key
var signatureKey = new StrongNameKey(@"c:\my\file.snk");
// This method will initialize the required properties
opts.InitializeStrongNameSigning(mod, signatureKey);
// Write and strong name sign the assembly
mod.Write(@"C:\out\file.dll", opts);
```
Enhanced strong name signing an assembly
----------------------------------------
See this [MSDN article](http://msdn.microsoft.com/en-us/library/hh415055.aspx) for info on enhanced strong naming.
Enhanced strong name signing without key migration:
```C#
using dnlib.DotNet.Writer;
...
// Open or create an assembly
ModuleDef mod = ModuleDefMD.Load(....);
// Open or create the signature keys
var signatureKey = new StrongNameKey(....);
var signaturePubKey = new StrongNamePublicKey(....);
// Create module writer options
var opts = new ModuleWriterOptions(mod);
// This method will initialize the required properties
opts.InitializeEnhancedStrongNameSigning(mod, signatureKey, signaturePubKey);
// Write and strong name sign the assembly
mod.Write(@"C:\out\file.dll", opts);
```
Enhanced strong name signing with key migration:
```C#
using dnlib.DotNet.Writer;
...
// Open or create an assembly
ModuleDef mod = ModuleDefMD.Load(....);
// Open or create the identity and signature keys
var signatureKey = new StrongNameKey(....);
var signaturePubKey = new StrongNamePublicKey(....);
var identityKey = new StrongNameKey(....);
var identityPubKey = new StrongNamePublicKey(....);
// Create module writer options
var opts = new ModuleWriterOptions(mod);
// This method will initialize the required properties and add
// the required attribute to the assembly.
opts.InitializeEnhancedStrongNameSigning(mod, signatureKey, signaturePubKey, identityKey, identityPubKey);
// Write and strong name sign the assembly
mod.Write(@"C:\out\file.dll", opts);
```
Exporting managed methods (DllExport)
-------------------------------------
dnlib supports exporting managed methods so the managed DLL file can be loaded by native code and then executed. .NET Framework supports this feature, but there's no guarantee that other CLRs (eg. .NET Core or Mono/Unity) support this feature.
In case of .NET Core please be aware that `ijwhost.dll` has to be loaded prior to calling your exported method and that ijwhost currently (as of .NET Core 3.0) does not work if the calling app is self-contained.
The `MethodDef` class has an `ExportInfo` property. If it gets initialized, the method gets exported when saving the module. At most
没有合适的资源?快使用搜索试试~ 我知道了~
dnlib-master.zip
共389个文件
cs:379个
csproj:2个
txt:1个
需积分: 9 9 下载量 5 浏览量
2020-10-30
07:46:05
上传
评论
收藏 672KB ZIP 举报
温馨提示
这是一个.net dll 解析库,可以轻易解析dll里面所有的数据,以及重新编译dll。 里面包含源代码,编译,示例 等
资源推荐
资源详情
资源评论
收起资源包目录
dnlib-master.zip (389个子文件)
SigComparer.cs 145KB
Metadata.cs 124KB
ModuleDefMD.cs 78KB
TypeDef.cs 77KB
FullNameFactory.cs 75KB
ModuleDef.cs 51KB
ModuleWriterBase.cs 46KB
OpCodes.cs 44KB
Importer.cs 41KB
PreserveTokensMetadata.cs 40KB
ComInterfaces.cs 38KB
MethodDef.cs 38KB
TypeSig.cs 38KB
AssemblyDef.cs 38KB
TablesStream_Read.cs 37KB
CallingConventionSig.cs 37KB
RawTableRows.cs 34KB
MDTableWriter.cs 34KB
PdbCustomDebugInfo.cs 32KB
ICodedToken.cs 32KB
NativeModuleWriter.cs 31KB
FrameworkRedirect.cs 31KB
TypeNameParser.cs 27KB
SignatureReader.cs 27KB
AssemblyResolver.cs 27KB
MetadataBase.cs 26KB
DataReader.cs 25KB
Instruction.cs 25KB
FieldDef.cs 25KB
MethodBodyReader.cs 24KB
UTF8String.cs 24KB
WinMDHelpers.cs 23KB
CustomAttributeWriter.cs 21KB
ExportedType.cs 21KB
TablesHeap.cs 20KB
CustomAttributeReader.cs 20KB
DynamicMethodBodyReader.cs 20KB
DotNetTableSizes.cs 20KB
MethodBodyReaderBase.cs 19KB
ManagedExportsWriter.cs 19KB
ModuleLoader.cs 19KB
RawRowEqualityComparer.cs 17KB
ILogger.cs 17KB
StrongNameKey.cs 17KB
ENCMetadata.cs 17KB
Win32ResourcesChunk.cs 17KB
MemberFinder.cs 17KB
AssemblyRef.cs 17KB
PortablePdbReader.cs 16KB
PEHeaders.cs 16KB
MethodBodyWriterBase.cs 15KB
MemberRef.cs 15KB
WindowsPdbWriter.cs 15KB
PropertyDef.cs 15KB
ParameterList.cs 14KB
GenericParam.cs 14KB
LazyList.cs 14KB
Metadata.cs 14KB
PdbState.cs 14KB
TablesStream.cs 14KB
CustomAttribute.cs 13KB
MethodUtils.cs 13KB
CpuArch.cs 13KB
MarshalType.cs 13KB
PdbCustomDebugInfoWriter.cs 12KB
EventDef.cs 12KB
TypeRef.cs 12KB
ParamDef.cs 12KB
TypeHelper.cs 11KB
MetaDataImport.cs 11KB
PEImage.cs 11KB
PdbReader.cs 11KB
ImplMap.cs 11KB
PdbImport.cs 11KB
MemoryMappedDataReaderFactory.cs 11KB
NormalMetadata.cs 11KB
PdbCustomDebugInfoReader.cs 11KB
Utils.cs 11KB
ModuleWriter.cs 10KB
MethodBodyWriter.cs 10KB
MDEmitter.cs 10KB
ResourceDataFactory.cs 10KB
PortablePdbCustomDebugInfoWriter.cs 9KB
TypeSpec.cs 9KB
SignatureWriter.cs 9KB
Win32Resources.cs 9KB
ResourceReader.cs 9KB
PortablePdbCustomDebugInfoReader.cs 9KB
AssemblyNameInfo.cs 8KB
ImageOptionalHeader32.cs 8KB
ImageOptionalHeader64.cs 8KB
StringsHeap.cs 8KB
LocalConstantSigBlobWriter.cs 8KB
ResourceDirectory.cs 8KB
TypeDefFinder.cs 8KB
AssemblyNameComparer.cs 8KB
MethodSpec.cs 8KB
ManifestResource.cs 8KB
OpCode.cs 8KB
MetadataFactory.cs 7KB
共 389 条
- 1
- 2
- 3
- 4
资源评论
HeZhongHao
- 粉丝: 327
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- libADLMIDI1-1.5.1-bp155.1.7.aarch64.rpm
- 基于python和resnet50的垃圾分类识别项目源码+全部数据.zip
- C语言进阶学习资料,关于预处理,文件处理,结构体,位移运算的学习 大学生期末复习资料,程序设计课程复习资料
- c++少儿编程专业教师使用资料
- box1234567890
- 少儿编程资料,c++少儿编程专业资料
- libADLMIDI1-1.5.1-bp156.2.6.aarch64.rpm
- java源码 仿360buy京东商城源码 京东JavaWeb项目源代码.rar
- libADLMIDI1-1.5.1-1.2.riscv64.rpm
- 基于ASP的BS架构工艺品展示系统.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功