How to programmatically generate C# files from a DLL or EXE
2011-11-09 14:05
1121 查看
I’m working on a project that involves researching and comparing many versions of the same DLL. I don’t have the source for every version of this DLL so I needed a way to convert it to C# files and diff those. Since there are thousands of versions of this file out there I needed to be able to generate the C# files programmatically.
I started with Reflector, but it doesn’t have a simple API needed to generate the files programmatically.
I came across JustDecompile from Telerik and found that they do have a command line API that works great. I could have just invoked the EXE with Process.Start, but I figured I should see how they are doing and see if I could do everything inproc. So I decompiled the JustDecompile.exe file and found a very simple DLL method that they use to output C# files.
2) Open up your project and Add References to these two DLLs.
C:\Program Files (x86)\Telerik\JustDecompile\Libraries\JustDecompile.Tools.MSBuildProjectBuilder.dll
C:\Program Files (x86)\Telerik\JustDecompile\Libraries\JustDecompiler.dll
3) Add these namespaces:
using JustDecompile.Tools.MSBuildProjectBuilder;
using Telerik.JustDecompiler.Languages.CSharp;
using System.Threading;
4) Add this code to your project where you want to programmatically decompile code
[/code]
Where dll is the path to the dll that you want to decompile and outfolder is the folder you want to output the C# files.
5) Add this event handler to be notified when the Visual Studio Project file has been created
[/code]
Hope this saves you some time.
Jon
I started with Reflector, but it doesn’t have a simple API needed to generate the files programmatically.
I came across JustDecompile from Telerik and found that they do have a command line API that works great. I could have just invoked the EXE with Process.Start, but I figured I should see how they are doing and see if I could do everything inproc. So I decompiled the JustDecompile.exe file and found a very simple DLL method that they use to output C# files.
1) Download JustDecompile
2) Open up your project and Add References to these two DLLs.
C:\Program Files (x86)\Telerik\JustDecompile\Libraries\JustDecompile.Tools.MSBuildProjectBuilder.dll
C:\Program Files (x86)\Telerik\JustDecompile\Libraries\JustDecompiler.dll
3) Add these namespaces:
using JustDecompile.Tools.MSBuildProjectBuilder;
using Telerik.JustDecompiler.Languages.CSharp;
using System.Threading;
4) Add this code to your project where you want to programmatically decompile code
MSBuildProjectBuilder projectBuilder = new MSBuildProjectBuilder(dll, outfolder, new CSharpV4());
[code]projectBuilder.ProjectFileCreated += new EventHandler<ProjectFileCreatedEvent>(projectBuilder_ProjectFileCreated);
projectBuilder.BuildProject(new CancellationToken());
[/code]
Where dll is the path to the dll that you want to decompile and outfolder is the folder you want to output the C# files.
5) Add this event handler to be notified when the Visual Studio Project file has been created
static void projectBuilder_ProjectFileCreated(object sender, ProjectFileCreatedEvent e)
[code]{
//throw new NotImplementedException();
}
[/code]
Hope this saves you some time.
Jon
相关文章推荐
- How to extract msu/msp/msi/exe files from the command line
- how to generate pb file from ckpt files?
- [zz]How to sign .EXE, .DLL and .CAB files?
- How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
- How to generate DLL files by GCC in the MinGW?
- How to Digitally Sign Microsoft Files (.exe, .cab, .dll, .ocx, .msi, .xpi)
- How to Digitally Sign Microsoft Files (.exe, .cab, .dll, .ocx, .msi, .xpi)
- How to enable download EXE files from the Sharepoint website
- How to tell if a file is an EXE or a DLL?
- How to Dynamically Load a Control from a DLL
- How to Delete From or Add Resource to OCR in Oracle Clusterware (文档 ID 1069369.1)
- How to generate a log file in VC++ or Embedded VC++ (For Pocket PC or WinCE devices)
- how to generate an analog output from a in-built pwm of Atmega 32AVR microcontrloller?
- Howto implement callback interface from unmanaged DLL to .net app?
- 如何阻止文件被iTunes和iCloud同步 How do I prevent files from being backed up to iCloud and iTunes?
- How to copy files remotely from another machine
- How to automate Microsoft Excel from Microsoft Visual C#.NET
- 从项目中生成dll文件的快速方法(a fast way to generate dll from the project)
- How to call Visual Basic .NET run-time library members from Visual C#
- How to call C/C++ sytle function from C# solution?