Embedding DLLs in a compiled executable

2018-12-31 02:03发布

You know, I haven't seen a good answer for this anywhere. Is it possible to embed a pre-existing DLL into a compiled C# executable (so that you only have one file to distribute)? If it is possible, how would one go about doing it?

Normally, I'm cool with just leaving the DLLs outside and having the setup program handle everything, but there have been a couple of people at work who have asked me this and I honestly don't know.

19条回答
低头抚发
2楼-- · 2018-12-31 02:42

If they're actually managed assemblies, you can use ILMerge. For native DLLs, you'll have a bit more work to do.

See also: How can a C++ windows dll be merged into a C# application exe?

查看更多
若你有天会懂
3楼-- · 2018-12-31 02:42

Yes, it is possible to merge .NET executables with libraries. There are multiple tools available to get the job done:

  • ILMerge is a utility that can be used to merge multiple .NET assemblies into a single assembly.
  • Mono mkbundle, packages an exe and all assemblies with libmono into a single binary package.
  • IL-Repack is a FLOSS alterantive to ILMerge, with some additional features.

In addition this can be combined with the Mono Linker, which does remove unused code and therefor makes the resulting assembly smaller.

Another possibility is to use .NETZ, which does not only allow compressing of an assembly, but also can pack the dlls straight into the exe. The difference to the above mentioned solutions is that .NETZ does not merge them, they stay separate assemblies but are packed into one package.

.NETZ is a open source tool that compresses and packs the Microsoft .NET Framework executable (EXE, DLL) files in order to make them smaller.

查看更多
千与千寻千般痛.
4楼-- · 2018-12-31 02:45

Another product that can handle this elegantly is SmartAssembly, at SmartAssembly.com . This product will, in addition to merging all dependencies into a single DLL, (optionally) obfuscate your code, remove extra meta-data to reduce the resulting file size, and can also actually optimize the IL to increase runtime performance. There is also some kind of global exception handling / reporting feature it adds to your software (if desired) that I didn't take the time to understand, but could be useful. I believe it also has a command line API so you can make it part of your build process.

查看更多
深知你不懂我心
5楼-- · 2018-12-31 02:46

It may sound simplistic, but WinRar gives the option to compress a bunch of files to a self-extracting executable.
It has lots of configurable options: final icon, extract files to given path, file to execute after extraction, custom logo/texts for popup shown during extraction, no popup window at all, license agreement text, etc.
May be useful in some cases.

查看更多
旧时光的记忆
6楼-- · 2018-12-31 02:48

ILMerge does exactly what you want.

查看更多
倾城一夜雪
7楼-- · 2018-12-31 02:52

It's possible but not all that easy, to create a hybrid native/managed assembly in C#. Were you using C++ instead it'd be a lot easier, as the Visual C++ compiler can create hybrid assemblies as easily as anything else.

Unless you have a strict requirement to produce a hybrid assembly, I'd agree with MusiGenesis that this isn't really worth the trouble to do with C#. If you need to do it, perhaps look at moving to C++/CLI instead.

查看更多
登录 后发表回答