On the analysis of C # writing x86 and x64 program

Source: Internet
Author: User

Computer hardware CPU can be divided into x86 and x64, x86 machine can only install 32-bit operating system, such as XP, win7_86, x64 machine can install 32-bit system, but also can install 64-bit system, just install 32-bit system on the x64 machine, Can not make full use of the resources of this machine. x86 program, which is applicable to 32 operating system programs, x64 is applicable to 64-bit operating system programs. 64-bit systems can still run 32-bit programs, but this is run through WOW64, in layman's words, is to simulate a 32-bit CPU to run the program.

Next is the C # program compilation run, divided into two steps, the first step is to compile into IL, when writing a C # program, you need to consider when the final program needs to run in which environment. The default environment in build is any CPU and x86,x64. What's the difference? According to the name, we may think that the compiler will generate different binaries depending on the environment you choose. However, the C # compiler just compiles the code into IL code, in the form of a DLL. Then, when the program runs, the JIT compiler compiles the IL code into a binary code that the CPU can recognize. Therefore, regardless of which environment to choose, will not affect the DLL generation, but only in the DLL's header file to add some platform information, the final run time JIT will be based on this information to compile the DLL to the CPU processing.

x86 compiled DLLs and x64 compiled DLLs cannot be referenced to each other, or they will throw exceptions that the assembly cannot download. Any CPU-compiled DLLs that are called by which type of program are compiled into which type of DLL.

Now consider a situation
Project A is the console application and the B project is the class Library. On a 64-bit operating system machine, the A project is Start Project, which determines the 32-bit or 64-bit properties of the program. If a is set to x86, a determines that the program is 32 bits and B is set to x86 because two items are of the same type, although all are 32 bits, but can be run on a 64-bit system. If a is any CPU and B is x86, there will be a problem. Because A is an any CPU, but running on a 64-bit operating system, it translates into a 64-bit dll,a that determines that the program is 64-bit and then calls the 32-bit DLL, which can cause problems. If A is x86 and B is any CPU, there will be no problem. Because a determines that the program is 32 bits, the call to B will turn B into 32 bits, everything is OK.

Some people say, can't you always set project to any CPU? Because many of the systems are now 64-bit, so our program is set to any CPU, 64-bit machine run is a 64-bit program, but these programs often need to use some components, such as COM components, and these components are only 32-bit DLLs, do not support 64-bit, There is a problem when we call these DLLs with a 64-bit program. So, at this point, we need to set our program to x86.

Then someone says, that's all set to x86. So now we're going to make sure that our program references DLLs that are 32-bit. Although many third-party DLLs are available in 32-bit and 64-bit two versions, this is basically guaranteed. But if there is no need to make 32-bit program, purely for convenience, we said before the 64-bit system running 32-bit programs, inefficient, wasting our resources.

On the analysis of C # writing x86 and x64 program

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.