简体   繁体   中英

Change application type with Mono.Cecil?

如何使用 Mono.Cecil 将应用程序从控制台应用程序类型修改为 Windows 应用程序类型,反之亦然?

To convert a console .exe to windows .exe, you can use:

var file = "foo.exe";
var module = ModuleDefinition.ReadModule (file);
// module.Kind was previously ModuleKind.Console
module.Kind = ModuleKind.Windows;
module.Write (file);

The other way around is as simple as choosing the appropriate ModuleKind value. From Cecil's source:

public enum ModuleKind {
    Dll,
    Console,
    Windows,
    NetModule,
}

For people who needed more help on this like me :)

you may need the apt pacakge libmono-cecil-cil-dev

//mono-cecil-set-modulekind-windows.cs
using System;
using Mono.Cecil;

namespace CecilUtilsApp {
    class CecilUtils {
        static void Main(string[] args) {
            var file = args[0];
            var module = ModuleDefinition.ReadModule (file);
            module.Kind = ModuleKind.Windows;
            module.Write (file);
        }
    }
}

// -----
//Makefile
//mono-cecil-set-modulekind-eq-windows.exe:
//    mcs $(shell pkg-config --libs mono-cecil) ./mono-cecil-set-modulekind-windows.cs
./mono-cecil-set-modulekind-windows.exe myprog.exe

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM