判断应用程序是控制台程序还是窗体程序

PE结构参考:

https://www.cnblogs.com/lzjsky/archive/2011/09/22/2184942.html

 

复制代码
@@@code

public static UInt16 Is64bit(string pFilePath, out UInt16 subSystem)

{

ushort architecture = 0;

subSystem = 0;

try

{

using (System.IO.FileStream fStream = new System.IO.FileStream(pFilePath, System.IO.FileMode.Open, System.IO.FileAccess.Read))

{

using (System.IO.BinaryReader bReader = new System.IO.BinaryReader(fStream))

{

if (bReader.ReadUInt16() == 23117) //check the MZ signature

{

fStream.Seek(0x3A, System.IO.SeekOrigin.Current); //seek to e_lfanew.

fStream.Seek(bReader.ReadUInt32(), System.IO.SeekOrigin.Begin); //seek to the start of the NT header.

if (bReader.ReadUInt32() == 17744) //check the PE\0\0 signature.

{

fStream.Seek(20, System.IO.SeekOrigin.Current); //seek past the file header,

architecture = bReader.ReadUInt16(); //read the magic number of the optional header.

fStream.Seek(0x42, System.IO.SeekOrigin.Current); //0x44h

subSystem = bReader.ReadUInt16();

}

}

}

}

}

catch (Exception) { /* TODO: Any exception handling you want to do, personally I just take 0 as a sign of failure */}

//if architecture returns 0, there has been an error.

return architecture;

}

@@#

复制代码

 

 

复制代码
@@@code

MachineType = x64 == 0x10b ? "32" : (x64 == 0x20b ? "64" : ""),

SubSystem = subSystem == 2 ? "GUI" : (subSystem == 3 ? "CUI" : ""),

@@#

复制代码

 

处理结果

 

 

 

另可使用dumpbin查看exe或dll的头信息

 

 

posted @   秦秋随  阅读(813)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗
点击右上角即可分享
微信分享提示