zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }

     

     

    本文作者:Love In Winter
    本文链接:https://www.cnblogs.com/LifeDecidesHappiness/p/15711169.html
    版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
    声援博主:如果您觉得文章对您有帮助,可以扫一扫,任意打赏,您的鼓励是博主的最大动力!
    扫一扫,支付宝打赏 扫一扫,微信打赏
  • 相关阅读:
    redmineBUG系统
    MySQL错误代码
    一篇文章全面了解监控知识体系
    K2 blackpearl 流程开发(一)
    HTTP 协议详解
    http协议学习系列
    浅谈HTTP中Get与Post的区别
    iOS应用程序生命周期(前后台切换,应用的各种状态)详解
    深入浅出 iOS 之生命周期
    iOS-利用AFNetworking(AFN 1.x)-实现文件上传
  • 原文地址:https://www.cnblogs.com/LifeDecidesHappiness/p/15711169.html
Copyright © 2011-2022 走看看