Embedding DLLs in a compiled executable

前端 未结 16 2672
情深已故
情深已故 2020-11-21 07:07

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?<

相关标签:
16条回答
  • 2020-11-21 07:44

    .NET Core 3.0 natively supports compiling to a single .exe

    The feature is enabled by the usage of the following property in your project file (.csproj):

        <PropertyGroup>
            <PublishSingleFile>true</PublishSingleFile>
        </PropertyGroup>
    

    This is done without any external tool.

    See my answer for this question for further details.

    0 讨论(0)
  • 2020-11-21 07:46

    You could add the DLLs as embedded resources, and then have your program unpack them into the application directory on startup (after checking to see if they're there already).

    Setup files are so easy to make, though, that I don't think this would be worth it.

    EDIT: This technique would be easy with .NET assemblies. With non-.NET DLLs it would be a lot more work (you'd have to figure out where to unpack the files and register them and so on).

    0 讨论(0)
  • 2020-11-21 07:46

    I use the csc.exe compiler called from a .vbs script.

    In your xyz.cs script, add the following lines after the directives (my example is for the Renci SSH):

    using System;
    using Renci;//FOR THE SSH
    using System.Net;//FOR THE ADDRESS TRANSLATION
    using System.Reflection;//FOR THE Assembly
    
    //+ref>"C:\Program Files (x86)\Microsoft\ILMerge\Renci.SshNet.dll"
    //+res>"C:\Program Files (x86)\Microsoft\ILMerge\Renci.SshNet.dll"
    //+ico>"C:\Program Files (x86)\Microsoft CAPICOM 2.1.0.2 SDK\Samples\c_sharp\xmldsig\resources\Traffic.ico"
    

    The ref, res and ico tags will be picked up by the .vbs script below to form the csc command.

    Then add the assembly resolver caller in the Main:

    public static void Main(string[] args)
    {
        AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
        .
    

    ...and add the resolver itself somewhere in the class:

        static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            String resourceName = new AssemblyName(args.Name).Name + ".dll";
    
            using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
            {
                Byte[] assemblyData = new Byte[stream.Length];
                stream.Read(assemblyData, 0, assemblyData.Length);
                return Assembly.Load(assemblyData);
            }
    
        }
    

    I name the vbs script to match the .cs filename (e.g. ssh.vbs looks for ssh.cs); this makes running the script numerous times a lot easier, but if you aren't an idiot like me then a generic script could pick up the target .cs file from a drag-and-drop:

        Dim name_,oShell,fso
        Set oShell = CreateObject("Shell.Application")
        Set fso = CreateObject("Scripting.fileSystemObject")
    
        'TAKE THE VBS SCRIPT NAME AS THE TARGET FILE NAME
        '################################################
        name_ = Split(wscript.ScriptName, ".")(0)
    
        'GET THE EXTERNAL DLL's AND ICON NAMES FROM THE .CS FILE
        '#######################################################
        Const OPEN_FILE_FOR_READING = 1
        Set objInputFile = fso.OpenTextFile(name_ & ".cs", 1)
    
        'READ EVERYTHING INTO AN ARRAY
        '#############################
        inputData = Split(objInputFile.ReadAll, vbNewline)
    
        For each strData In inputData
    
            if left(strData,7)="//+ref>" then 
                csc_references = csc_references & " /reference:" &         trim(replace(strData,"//+ref>","")) & " "
            end if
    
            if left(strData,7)="//+res>" then 
                csc_resources = csc_resources & " /resource:" & trim(replace(strData,"//+res>","")) & " "
            end if
    
            if left(strData,7)="//+ico>" then 
                csc_icon = " /win32icon:" & trim(replace(strData,"//+ico>","")) & " "
            end if
        Next
    
        objInputFile.Close
    
    
        'COMPILE THE FILE
        '################
        oShell.ShellExecute "c:\windows\microsoft.net\framework\v3.5\csc.exe", "/warn:1 /target:exe " & csc_references & csc_resources & csc_icon & " " & name_ & ".cs", "", "runas", 2
    
    
        WScript.Quit(0)
    
    0 讨论(0)
  • 2020-11-21 07:47

    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.

    0 讨论(0)
  • 2020-11-21 07:47

    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.

    0 讨论(0)
  • 2020-11-21 07:48

    Generally you would need some form of post build tool to perform an assembly merge like you are describing. There is a free tool called Eazfuscator (eazfuscator.blogspot.com/) which is designed for bytecode mangling that also handles assembly merging. You can add this into a post build command line with Visual Studio to merge your assemblies, but your mileage will vary due to issues that will arise in any non trival assembly merging scenarios.

    You could also check to see if the build make untility NANT has the ability to merge assemblies after building, but I am not familiar enough with NANT myself to say whether the functionality is built in or not.

    There are also many many Visual Studio plugins that will perform assembly merging as part of building the application.

    Alternatively if you don't need this to be done automatically, there are a number of tools like ILMerge that will merge .net assemblies into a single file.

    The biggest issue I've had with merging assemblies is if they use any similar namespaces. Or worse, reference different versions of the same dll (my problems were generally with the NUnit dll files).

    0 讨论(0)
提交回复
热议问题