How can I determine the subsystem used by a given .NET assembly?

前端 未结 4 507
野趣味
野趣味 2020-12-06 19:22

In a C# application, I\'d like to determine whether another .NET application is a Console application or not.

Can this be done using the reflection APIs?

EDI

4条回答
  •  臣服心动
    2020-12-06 19:59

    The SHGetFileInfo function can do this:

    [DllImport("shell32.dll", CharSet=CharSet.Auto, EntryPoint="SHGetFileInfo")]
    public static extern ExeType GetExeType(string pszPath, uint dwFileAttributes = 0, IntPtr psfi = default(IntPtr), uint cbFileInfo = 0, uint uFlags = 0x2000);
    
    [Flags]
    public enum ExeType
    {
        None = 0,
        WinNT = 0x04000000,
        PE = ((int)'P') | ((int)'E' << 8),
        NE = ((int)'N') | ((int)'E' << 8),
        MZ = ((int)'M') | ((int)'Z' << 8),
    }
    

    Then, according to the specification, if it's only MZ or PE, it is opened in console, otherwise (if a version is specified), it is open in a window.

    ExeType type = GetExeType("program.exe");
    if(type == ExeType.PE || type == ExeType.MZ) return "console";
    else return "window";
    

提交回复
热议问题