感谢您阅读我的博客,如果您现在工作、学习累了或者疲惫了,不妨聆听一下音乐,它能够减轻你的疲劳,还能够带给您一种舒适愉悦的心情。如果您认为这篇文章还不错或者有所收获,您可以在页面 右侧和底部 扫描二维码 打赏我,您的鼓励是我继续写作、分享的最大动力!

C#判断DLL文件是32位还是64位

using System;
using System.IO;

namespace GetDllVersionDemo
{
    /// <summary>
    ///     https://www.cnblogs.com/LifeDecidesHappiness/p/15711169.html
    ///     C#判断DLL文件是32位还是64位
    ///     LDH @ 2021-12-20
    /// </summary>
    internal class Program
    {
        private static void Main()
        {
            Console.Title = "C#判断DLL文件是32位还是64位";

            GetDll32Or64();

            Console.ReadKey();
        }

        private static void GetDll32Or64()
        {
            var dllPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Dll\IBM.Data.Informix.dll");
            var result = GetPeArchitecture(dllPath);
            //523 64位    267 32位
            if (result == 523)
                Console.WriteLine(dllPath + "是【64】位的dll");
            else if (result == 267)
                Console.WriteLine(dllPath + "是【32】位的dll");
            else
                Console.WriteLine("执行错误!");
        }

        /// <summary>
        ///     获取dll文件是32位还是64位
        ///     523 64位    267 32位
        /// </summary>
        /// <param name="dllFilePath">dll文件路径</param>
        /// <returns></returns>
        public static ushort GetPeArchitecture(string dllFilePath)
        {
            ushort architecture = 0;

            try
            {
                using (var fStream = new FileStream(dllFilePath, FileMode.Open, FileAccess.Read))
                {
                    using (var bReader = new BinaryReader(fStream))
                    {
                        if (bReader.ReadUInt16() == 23117) //check the MZ signature
                        {
                            fStream.Seek(0x3A, SeekOrigin.Current); //seek to e_lfanew.
                            fStream.Seek(bReader.ReadUInt32(), SeekOrigin.Begin); //seek to the start of the NT header.
                            if (bReader.ReadUInt32() == 17744) //check the PE\0\0 signature.
                            {
                                fStream.Seek(20, SeekOrigin.Current); //seek past the file header,
                                architecture = bReader.ReadUInt16(); //read the magic number of the optional header.
                            }
                        }
                    }
                }
            }
            catch
            {
                // ignored
            }

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

 

 

posted @ 2021-12-20 15:40  Love In Winter  阅读(379)  评论(0编辑  收藏  举报
作者: LifeDecidesHappiness
出处: http://www.cnblogs.com/LifeDecidesHappiness/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,否则保留追究法律责任的权利,且在文章页面明显位置给出原文连接,如有问题,可以通过以下邮箱地址 2468881301@qq.com  联系我,非常感谢。
踏实做一个为人民服务的搬运工!
如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能,您的支持和鼓励是我继续写作、分享的最大动力!

点击关注不迷路,让我带你上高速!