Developers often refer to some third-party DLLs, and then compile the generated EXE files can not be separated from these DLLs run independently.
Ilmerge can combine a managed DLL with an EXE to generate a new EXE, but when we use an unmanaged DLL in the project, that is, when a third-party DLL is used, the merge succeeds but fails to run, prompting " not a valid Win32 application "
At this time we need to use a tool called Fody.costura. The Fody.costura is a plug-in in a fody framework that can be installed into the VS project through NuGet. Once installed, you can package the DLLs (or even PDB) files that your project depends on in the EXE file.
How to use
- In VS, Costura.fody is installed through NuGet for the target EXE project.
- Restart vs after successful installation, then regenerate
After the rebuild is completed, the newly generated EXE file is found in the output directory of the project, and you will also find those DLLs still exist under the output directory. But don't worry, this exe has been able to run independently. You can delete all these DLLs and then run EXE to try again.
In addition, Fody.costura supports some advanced features, such as:
- Temporary assembly file: Automatically extracts the DLL from the EXE to the folder system before running the EXE, and then loads the DLL in a regular manner.
- Merging unmanaged DLL:Fody.Costura can merge unmanaged DLLs, but they do not automatically fit. If your program involves an unmanaged DLL, you need to show it by modifying the Fody.costura configuration file to tell it which unmanaged DLLs you want to merge.
- Preloaded DLL:Fody.Costura can help you preload some DLLs when the program starts, and you can even specify the order in which these DLLs are loaded.
These advanced features require you to modify the Fody.costura configuration file to achieve, the specific steps can refer to its official documentation.
Method of merging WinForm program (with multiple unmanaged DLLs) into one EXE