c# - How can I determine the subsystem used by a given .NET assembly? -
in c# application, i'd determine whether .net application console application or not.
can done using reflection apis?
edit: ok, doesn't i'm going answer question because doesn't framework exposes functionality want. dug around in pe/coff spec , came this:
/// <summary> /// parses pe header , determines whether given assembly console application. /// </summary> /// <param name="assemblypath">the path of assembly check.</param> /// <returns>true if given assembly console application; false otherwise.</returns> /// <remarks>the magic numbers in method extracted pe/coff file /// format specification available http://www.microsoft.com/whdc/system/platform/firmware/pecoff.mspx /// </remarks> bool assemblyusesconsolesubsystem(string assemblypath) { using (var s = new filestream(assemblypath, filemode.open, fileaccess.read)) { var rawpesignatureoffset = new byte[4]; s.seek(0x3c, seekorigin.begin); s.read(rawpesignatureoffset, 0, 4); int pesignatureoffset = rawpesignatureoffset[0]; pesignatureoffset |= rawpesignatureoffset[1] << 8; pesignatureoffset |= rawpesignatureoffset[2] << 16; pesignatureoffset |= rawpesignatureoffset[3] << 24; var coffheader = new byte[24]; s.seek(pesignatureoffset, seekorigin.begin); s.read(coffheader, 0, 24); byte[] signature = {(byte)'p', (byte)'e', (byte)'\0', (byte)'\0'}; (int index = 0; index < 4; index++) { assert.that(coffheader[index], is.equalto(signature[index]), "attempted check non pe file console subsystem!"); } var subsystembytes = new byte[2]; s.seek(68, seekorigin.current); s.read(subsystembytes, 0, 2); int subsystem = subsystembytes[0] | subsystembytes[1] << 8; return subsystem == 3; /*image_subsystem_windows_cui*/ } }
this out of scope of managed code. .net view console , win applications same, have peek pe file header. search work "subsystem" on page http://msdn.microsoft.com/en-us/magazine/bb985997.aspx
Comments
Post a Comment