Get the App
SLTechnology News&Howtos  ›  Development  › 

How to write sample code for C # to determine whether a DLL file is 32-bit or 64-bit?

Shulou Source: shulou.com Published: 2022-06-02 05:56:14 09月28日 Update

C # to determine whether the DLL file is 32-bit or 64-bit sample code how to write, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

C # determines whether the dll file is 32-bit or 64-bit. The example code is as follows:

Using System;using System.IO;namespace GetDllVersionDemo {/ https://www.cnblogs.com/LifeDecidesHappiness/p/15711169.html/// C# determines whether the DLL file is 32-bit or 64-bit / LDH @ 2021-12-20 / internal class Program {private static void Main () {Console.Title = "C # determines whether the DLL file is 32-bit or 64-bit"; 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-bit 267 32-bit if (result = = 523) Console.WriteLine (dllPath + "is [64] bit dll"); else if (result = = 267) Console.WriteLine (dllPath + "is [32] bit dll"); elseConsole.WriteLine ("execution error!") ;} / get whether the dll file is 32-bit or 64-bit / 523 64-bit 267 32-bit / dll file path / 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;}

After reading the above, have you mastered the method of how to write sample code in C # to determine whether the DLL file is 32-bit or 64-bit? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Documents or caching code examples content methods more problems at a loss for this reason reasons examples skills articles experience industry information information channels Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi MySQL Apple Huawei Shulou Information