How to extract from exe file the same .ico file as the one used to create this exe?

后端 未结 2 1785
梦如初夏
梦如初夏 2021-01-26 05:48

I\'m trying to make some kind of SFX: make a program generating a wrapping.exe around another wrapped.exe.

Wrapping.exe embed wrapped.exe as resource and, when executed,

相关标签:
2条回答
  • 2021-01-26 06:32

    This is very easy using IconLib. The response was already in this question: Thx to @Plutonix!

    With following helper function (of course, extraction icon file name will not be hardcoded):

    static Stream GetIconStream_ExtractIconUsingIconLib(string fileToExecute)
    {
        var multiIcon = new MultiIcon();
        multiIcon.Load(fileToExecute);
    
        var extractedicoFileName = @"c:\temp\icon.ico";
        multiIcon.Save(extractedicoFileName, MultiIconFormat.ICO);
    
        return File.OpenRead(extractedicoFileName);
    }
    

    We just have to replace:

    File.OpenRead(@"wrapped.ico")
    

    with

    GetIconStream_ExtractIconUsingIconLib("wrapped.exe")
    

    This gives us full solution:

    var compilation = CSharpCompilation.Create(...);
    var resourceDescription = new ResourceDescription( resourceName: "SFX.resourceName",
                                                       dataProvider: () => File.OpenRead("wrapped.exe"),
                                                       isPublic:     false);
    
    using (var iconStream = GetIconStream_ExtractIconUsingIconLib("wrapped.exe"))
    using (var peStream = File.Create("wrapping.exe"))
    using (var pdbStream = File.Create("wrapping.pdb"))
    using (var win32resStream = compilation.CreateDefaultWin32Resources(
                                                                  versionResource:  true,
                                                                  noManifest:       false,
                                                                  manifestContents: null,
                                                                  iconInIcoFormat:  iconStream))
    {
        var emitResult = compilation.Emit( peStream:          peStream,
                                           pdbStream:         pdbStream,
                                           manifestResources: new[] { resourceDescription },
                                           win32Resources:    win32resStream,
                                           options:           new EmitOptions(subsystemVersion: SubsystemVersion.Windows7));
      return emitResult;
    }
    
    0 讨论(0)
  • 2021-01-26 06:41

    There is also online websites that do this job https://extracico.com/

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