C # Determines whether the operating system is 32-bit or 64-bit

Source: Internet
Author: User

When I was doing a C # project, I encountered the problem of getting the number of operating system bits, and had not found a more complete solution on the internet for several hours. Not much to say, directly on the code that can be run (simple, rude)

usingSystem.Runtime.ConstrainedExecution;usingSystem.Runtime.InteropServices;Internal Static classwin32native {[ReliabilityContract (Consistency.willnotcorruptstate, Cer.mayfail)] [DllImport ("Kernel32.dll", bestfitmapping =false, CharSet = CharSet.Auto, SetLastError =true)]        Private Static externIntPtr GetModuleHandle (stringmodulename); [DllImport ("Kernel32.dll", bestfitmapping =false, CharSet = charset.ansi, exactspelling =true, SetLastError =true)]        Private Static externIntPtr GetProcAddress (IntPtr hmodule,stringmethodName); [DllImport ("Kernel32.dll", SetLastError =true)]        [return: MarshalAs (unmanagedtype.bool)]Internal Static extern BOOLIswow64process ([in] IntPtr hsourceprocesshandle, [MarshalAs (Unmanagedtype.bool)] out BOOLisWow64); [DllImport ("Kernel32.dll", CharSet = CharSet.Auto, SetLastError =true)]        Internal Static externIntPtr getcurrentprocess (); Internal Static BOOLDoeswin32methodexist (stringModuleName,stringmethodName) {INTPTR Modulehandle=Win32native.getmodulehandle (modulename); if(Modulehandle = =IntPtr.Zero) {return false; } IntPtr procaddress=win32native.getprocaddress (Modulehandle, methodName); returnProcaddress! =IntPtr.Zero; }         Public Static BOOLIs64bitoperatingsystem {Get            {                BOOLflag=default(BOOL); return(Win32native.doeswin32methodexist ("Kernel32.dll","iswow64process") && win32native.iswow64process (win32native.getcurrentprocess (), outFlag)) &Flag; }        }    }

C # Determines whether the operating system is 32-bit or 64-bit

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.