Two Methods for the WPF program to embed DLL into the EXE
This article can be viewed as a continuation of Visual Studio version Conversion Tool WPF open source, for more information about open-source Visual Studio Conversion Tool WPF, see the underground address (the two are the same ):
- Open source http://my.oschina.net/chinesedragon/blog/308336 China
- Http://www.cnblogs.com/luoshupeng/p/3946635.html cnblogs
Introduction
The first few of them wrote a small tool named "Visual Studio version Conversion Tool". Because WPF is used as the interface, this small program runs with two dll: Microsoft. expression. interactions. DLL and system. windows. interactivity. DLL, at the same time because I have also written a library, a small program requires three DLL, this experience is really uncomfortable, so the DLL is embedded into the exe.
Setbacks
For C # programs, to embed DLL into EXE, the most authoritative and common method is to use the ilmerge tool, which is a command line tool with many parameters, the dll can be perfectly embedded into the exe. If the command line is too troublesome, some people have developed the graphical interface ilmergegui. The download and help addresses of these two tools are as follows:
- Ilmerge http://www.microsoft.com/en-us/download/details.aspx? Id = 17630
- Ilmerge-Gui http://ilmergegui.codeplex.com/
So I downloaded the two tools, but the DLL was embedded into the exe. I checked the reason on the Internet. It turned out that ilmerge does not support the WPF program,Microsoft, what do you want me to say about you?
Ilmerge can perfectly embed the DLL of the winform program into the EXE (I personally tested this point, but it is not like it), but it is not supported by WPF, the reason is that the WPF dll contains resources that cannot be solved,Microsoft, do you mean that this tool was developed by you?
The winform program embeds the DLL into the EXE (1) -- use the command line
Download ilmerge or ilmerge-Gui at the same time. The graphic interface is the same as the command line, but the graphic interface is simpler.
After downloading ilmergeinstallation, I copied ilmerge.exe to the C: \ Windows directory. In this way, you can directly use it in the command line without setting environment variables. In any case, you only need to use this tool in the command line.
Ilmerge has many parameters, several of which are important:
- /Target: the target has two options: library and winexe. You can use library when integrating multiple DLL into one DLL. when integrating multiple DLL into EXE, you should use winexe.
- /Out: output file path and name.
- /Log: Input. If the input is exe, you can directly write this parameter without using this parameter. If the input is DLL, you 'd better use this parameter.
There are other parameters that can be used by Baidu or Google. This is an image I tested:
The winform program embeds the DLL into the EXE (2) -- use ilmerge. msbuild. Tasks
Ilmerge also used nuget to release the tool. The benefits of using nuget must be known.This method is recommended..
Step 1: Use the nuget graph or nuget command to download ilmerge. msbuild. Tasks
PM> Install-Package ILMerge.MSBuild.Tasks
Step 2: Open the vs project file notepad or other text editing tools. I use sublime Text 3 and modify it according to the following format:
<! -- Code to merge the assemblies into one: setup.exe --> <usingtask taskname = "ilmerge. msbuild. tasks. ilmerge "assemblyfile =" $ (solutiondir) \ packages \ ilmerge. msbuild. tasks.1.0.0.3 \ tools \ ilmerge. msbuild. tasks. DLL "/> <target name =" afterbuild "> <itemgroup> <mergeasm include =" $ (outputpath) $ (targetfilename) "/> <mergeasm include =" $ (outputpath) libjavasto_merge.dll "/> <! -- Change the DLL name to be embedded here --> <mergeasm include = "$ (outputpath) lib2_to_merge.dll "/> </itemgroup> <propertygroup> <mergedassembly> values (projectdir1_1_(outdir1_merged_assembly_name.exe </mergedassembly> <! -- Change the EXE name to be output here --> </propertygroup> <message text = "ilmerge @ (mergeasm)-& gt; $ (mergedassembly) "importance =" high "/> <ilmerge inputassemblies =" @ (mergeasm) "outputfile =" $ (mergedassembly) "targetkind =" sameasprimaryassembly "/> </Target>
After compilation, you can.
The WPF program embeds the DLL into the exe. (1) -- automatically converts the DLL into an embedded resource.
Step 1: Modify the project file and automatically convert the DLL to an embedded resource.
Open the vs project file notepad or other text editing tools. I use sublime Text 3 and add the following content to the end of the file:
<Target Name="AfterResolveReferences"> <ItemGroup> <EmbeddedResource Include="@(ReferenceCopyLocalPaths)" Condition="‘%(ReferenceCopyLocalPaths.Extension)‘ == ‘.dll‘"> <LogicalName>%(ReferenceCopyLocalPaths.DestinationSubDirectory)%(ReferenceCopyLocalPaths.Filename)%(ReferenceCopyLocalPaths.Extension)</LogicalName> </EmbeddedResource> </ItemGroup></Target>
Step 2: Modify the app. XAML file and load resources when the program starts.
public partial class App : Application{ private static Assembly OnResolveAssembly(object sender, ResolveEventArgs args) { Assembly executingAssembly = Assembly.GetExecutingAssembly(); var executingAssemblyName = executingAssembly.GetName(); var resName = executingAssemblyName.Name + ".resources"; AssemblyName assemblyName = new AssemblyName(args.Name); string path = ""; if (resName == assemblyName.Name) { path = executingAssemblyName.Name + ".g.resources"; ; } else { path = assemblyName.Name + ".dll"; if (assemblyName.CultureInfo.Equals(CultureInfo.InvariantCulture) == false) { path = String.Format(@"{0}\{1}", assemblyName.CultureInfo, path); } } using (Stream stream = executingAssembly.GetManifestResourceStream(path)) { if (stream == null) return null; byte[] assemblyRawBytes = new byte[stream.Length]; stream.Read(assemblyRawBytes, 0, assemblyRawBytes.Length); return Assembly.Load(assemblyRawBytes); } } protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); AppDomain.CurrentDomain.AssemblyResolve += OnResolveAssembly; }}
Step 3: After the DLL is embedded into the EXE, the DLL in the directory will be useless. Configure the post buid script to automatically delete the DLL:
cd $(TargetDir)del *.dll
In some cases, the above method does not work, so you can try eazfuscator. net
Eazfuscator. Net was previously free and has now become a paid software. However, you can find a free version 3.3 that supports vs2010 and vs2012.
The WPF program embeds the DLL into the EXE (2) -- use libz container
Libz is another option of ilmerge. It can also embed DLL into exe. In my test, it can embed the DLL of WPF program into exe, however, it seems that there are not many people using this component.
The project home page for libz container is a http://libz.codeplex.com/
Libz also provides nuget download, which has many advantages,This method is recommended..
Download libz. Bootstrap using nuget graphics or commands
Install-Package LibZ.Bootstrap
Then, configure the post buid script:
set LIBZ=$(SolutionDir)packages\LibZ.Bootstrap.1.1.0.2\tools\libz.exe%LIBZ% inject-dll --assembly VSConverter.WPF.exe --include *.dll --move
After compilation, you can. HereNote that the parameter after-assembly is the file name generated by the project..
Libz also has many usage instructions. You can learn from the project documentation.
References
- Combining Multiple assemblies into a single EXE for a WPF Application
- DLL embedded in exe
- Libz Project
Nuget is a very powerful tool. Using nuget can often simplify the solution and give nuget a thumbs up!
Run the following advertisement: http://git.oschina.net/shupengluo/vsconverter.
Finally, we despise Microsoft ,_
Two Methods for the WPF program to embed DLL into the EXE