gpt4 book ai didi

c# - 如何确定给定 .NET 程序集使用的子系统?

转载 作者:太空狗 更新时间:2023-10-29 23:11:20 26 4
gpt4 key购买 nike

在 C# 应用程序中,我想确定另一个 .NET 应用程序是否是控制台应用程序。

这可以使用反射 API 来完成吗?

编辑:好吧,看起来我不会得到这个问题的好答案,因为它看起来不像框架公开了我想要的功能。我仔细研究了 PE/COFF 规范并得出了这个结论:

/// <summary>
/// Parses the PE header and determines whether the given assembly is a console application.
/// </summary>
/// <param name="assemblyPath">The path of the assembly to check.</param>
/// <returns>True if the given assembly is a console application; false otherwise.</returns>
/// <remarks>The magic numbers in this method are extracted from the PE/COFF file
/// format specification available from 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'};
for (int index = 0; index < 4; index++)
{
Assert.That(coffHeader[index], Is.EqualTo(signature[index]),
"Attempted to check a non PE file for the 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*/
}
}

最佳答案

这超出了托管代码的范围。从 .NET 的角度来看,控制台和 Windows UI 应用程序是相同的。您必须查看 PE 文件头。在此页面上搜索“子系统”一词 http://msdn.microsoft.com/en-us/magazine/bb985997.aspx

关于c# - 如何确定给定 .NET 程序集使用的子系统?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3111669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com