How to port a 32-bit program to a 64-bit System

Source: Internet
Author: User

Recently, I want to compatible my program with a 64-bit system. I thought it was a big project. However, after reading the relevant information, I found this is not difficult. Because the General 32-bit program can run in a 64-bit System (of course, the general situation), this is mainly because the 64-bit system has considered this transition situation.

First, you must know that the 64 system has prepared an environment for 32-bit applications to run the 32-bit program, which is wow64.

Wow64 is short for Windows-32-on-Windows-64. It provides 32-bit Simulation for existing 32-bit applications, enabling most 32-bit applications to run on Windows 64-bit without modification. It is similar to the old wow32 subsystem and runs 16-bit code in Windows 32-bit version.

Next we will introduce some things to be used in programming.

1. Of course, you must also use the following method to determine whether the system is a 32-bit system or a 64-bit system? If you want to use a small change, you must know whether it is 32 or 64.

If iswow64 returns true, it is a 64-bit system; otherwise, it is a 32-bit system.

Code: VC passed the test

Typedef bool (winapi * lpfn_iswow64process) (handle, pbool );

Lpfn_iswow64process fniswow64process;

Bool iswow64 ()
{
Bool biswow64 = false;
Fniswow64process = (lpfn_iswow64process) getprocaddress (
Getmodulehandle (text ("Kernel32"), "iswow64process ");
If (null! = Fniswow64process)
{
If (! Fniswow64process (getcurrentprocess (), & biswow64 ))
{
// Handle error
Afxmessagebox ("iswow64 error! ");
}
}
Return biswow64;
}

2. About the registry.

If you go to the vista64 registry, you will find that the Registry has changed a lot in terms of program installation and uninstallation. But the good news is. Run wow64 in your program. So you don't need to worry about the changes. This wow64 will help you switch. Of course, this is only limited to some standard registry location changes, but you need to test it on your own. This change is mainly due to the fact that the 64-bit program installed in vista64 is significantly different from the 32-bit program in the installation location, because there is an additional program file (X ).

For example, the list of uninstalled programs:

XP is located in the following locations:

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall

Vista64 is located at the following position:

HKEY_LOCAL_MACHINE \ SOFTWARE \ wow6432node \ Microsoft \ Windows \ CurrentVersion \ Uninstall \

However, if you access the following location in Vista:

HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows \ CurrentVersion \ Uninstall is also correct.

The location of the specific program on the computer in XP is:

HKEY_LOCAL_MACHINE \ Software

Vista is located in HKEY_LOCAL_MACHINE \ SOFTWARE \ wow6432node

If you still access the following location under vista64,

HKEY_LOCAL_MACHINE \ Software

What you get is in Vista

HKEY_LOCAL_MACHINE \ SOFTWARE \ wow6432node

.

In this case, wow is actually converted.

Therefore, you don't have to worry about problems. Of course, some non-standard locations may change. But don't worry. These are rarely used.

Introduction to wow64:

The hardware itself has a 32-bit compatibility mode that can handle the actual execution of IA-32 commands, while the wow layer handles transactions such as switching the processor between 32-bit and 64-bit modes and simulating 32-bit systems. For example, 32-bit and 64-bit programs have different registry configuration units. There is also a different system directory for 32-bit binary files. The 64-bit binary file still uses the System32 directory. Therefore, when a 32-bit application is installed in the system, the wow layer ensures that the 32-bit binary file is placed in a new directory syswow64. This is achieved through the following method: intercept API calls (such as getsystemdirectory) and return the appropriate directory based on whether the application is running under wow. The same issue may exist in the registry. Because both 32-bit and 64-bit com servers can be installed on the system and are located under the same Class Identifier (CLSID, therefore, the wow layer needs to redirect registry calls to the appropriate 32-bit or 64-bit configuration units. The Wow layer also handles image changes between certain regions in the Registry to make it easier to support interaction between 32-bit and 64-bit code.

Wow64 is very important because it allows you to use most of the existing 32-bit code without worrying about performance and scalability issues. It is the best combination of the two methods. You can migrate your service to 64-bit while retaining the Microsoft Management Console (MMC) Configuration Management Unit as 32-bit. Windows 64-bit versions include the 32-bit and 64-bit versions of MMC. When the Retention management tool is set to 32-bit, communication between processes may encounter some problems, but as long as the interface design is correct, such as Remote Procedure Call (RPC) the protocol should be able to run between 32-bit and 64-bit processes. Another thing to note about wow64 is that it is not designed for applications requiring high performance. At least, the wow64 subsystem needs to extend the 32-bit parameter to 64-bit, And the 64-bit return value must be truncated to 32-bit. In the worst case, the wow64 subsystem will need to call the kernel, not only the conversion to the kernel, there is also a conversion from the 32-bit compatibility mode of the processor to the 64-Bit mode of its local machine. When running in wow64, the application cannot be properly adjusted. For applications that you want to retain as 32-bit, test them in wow64. If the performance does not meet your expectations, you need to consider migrating the application to 64-bit.

Wow64 is implemented in user mode and serves as the layer between NTDLL. dll and kernel. Wow64 and some of its supported DLL files are only 64-bit DLL files that can be loaded to 32-bit processes. In all other cases, the process remains pure. 32-bit processes cannot load 64-bit DLL, and vice versa.

It seems that you are clear about the above. Generally, the program does not need to be transplanted. However, if drivers are involved, such as driver installation, these need to be transplanted. Because the driver is installed in the kernel for running, the 32-bit driver does not work.

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.