首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何确定.NET程序集是为x86还是为x64生成的?

如何确定.NET程序集是为x86还是为x64生成的?
EN

Stack Overflow用户
提问于 2008-11-06 22:14:03
回答 11查看 187K关注 0票数 352

我有一个任意的.NET程序集列表。

我需要以编程方式检查每个DLL是否都是为x86 (而不是x64或任何CPU)构建的。这个是可能的吗?

EN

回答 11

Stack Overflow用户

发布于 2012-03-19 17:31:38

你就写你自己的怎么样?PE体系结构的核心自从在Windows95中实现以来,并没有发生重大的变化。下面是一个C#示例:

代码语言:javascript
复制
    public static ushort GetPEArchitecture(string pFilePath)
    {
        ushort architecture = 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.
                        }
                    }
                }
            }
        }
        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;
    }
}

现在,当前常量为:

代码语言:javascript
复制
0x10B - PE32  format.
0x20B - PE32+ format.

但是使用这种方法,它允许新常量的可能性,只要在你认为合适的时候验证返回即可。

票数 22
EN

Stack Overflow用户

发布于 2017-08-22 15:20:44

JetBrains的DotPeek提供了查看msil(anycpu)、x86、x64的快捷方式。

票数 13
EN

Stack Overflow用户

发布于 2013-02-18 20:45:49

尝试使用CorFlagsReader from this project at CodePlex。它没有对其他程序集的引用,可以按原样使用。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/270531

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档