Win32 and win64 programming are permanent; differences between 64-bit and 32-bit Programming

Source: Internet
Author: User

1. Data Types, especially int-related types, have different lengths on the platform of machines with different digits. The c99 standard does not specify the length of a specific data type, but only specifies the level. For comparison:

16-bit Platform

Char, 1 byte, 8 bits

Short 2-byte 16-bit

Int, 2 bytes, 16 bits

Long 4-byte 32-bit

Pointer 2 bytes

32-bit Platform

Char, 1 byte, 8 bits

Short 2-byte 16-bit

Int 4-byte 32-bit

Long 4 bytes

Long long 8 bytes

Pointer 4 bytes

64-bit Platform

Char 1 byte

Short 2 bytes

Int 4 bytes

Long 8 bytes (difference)

Long long 8 bytes

8 bytes pointer (difference)

Ii. Programming considerations

To ensure the versatility of the Platform,ProgramDo not use the long database type. You can use a macro definition of a fixed data type:

Typedef signed Char int8_t

Typedef short int int16_t;

Typedef int int32_t;

# If _ wordsize = 64
Typedef long int int64_t;
# Else
_ Extension __
Typedef long int int64_t;

# Endif

Iii. intptr_t can also be used when int is used to ensure the universality of the platform. It has different length for compiling on different platforms, but is a standard platform length, for example, the length of a 64-bit machine is 8 bytes, and that of a 32-bit machine is 4 bytes. The definition is as follows:

# If _ wordsize = 64
Typedef long int intptr_t;
# Else
Typedef int intptr_t;
# Endif
In programming, sizeof should be used whenever possible to calculate the data type size

All the preceding types have corresponding unsigned types.

In addition, ssize_t and size_t are respectively sign size_t and unsigned signed size of computer word size. They are also the word lengths of computers. They are int type on 32-bit machines and long type on 64-bit machines. In a sense, they are equivalent to intptr_t and uintptr_t. They are defined in stddef. h. It should be noted that the socket's accept function is incorrect in some operating systems to use size_t, because the accept receives the int * type, while the size_t may be a long int type. Later, BSD replaced it with sock_t.

 

Since the launch of Windows 95 in 1995, the vast majority of Windows applications have been transferred from the Win16 platform to the Win32 platform. Windows 3. X and its 16-bit windows programming technology have been quickly entered into the cold Palace. Now Microsoft software engineers have begun designing 64-bit windows programming (win64) technology for Intel's IA-64 processor architecture, windows 98 is the last product in the Windows 9x series, so win64 will be included in the Windows NT 5.0 being developed. When Intel's next-generation 64-bit processor Merced is launched, software developers can start to use 64-bit programming technology, and NT's enterprise computing capability will inevitably be greatly improved.

 

Because the 4 GB address space meets the needs of most applications, the difference between win64 and Win32 is much smaller than that between Win32 and Win16, so win64 will not replace Win32 as quickly as Win32 replaces Win16. Win64 and Win32 will coexist and complement each other for a long period of time in the future. Software developers need to select the development platform based on the characteristics of the application, or establish the win64 and Win32 versions of the application at the same time, because win64 and Win32 programming are not much different, when programming in advanced languages, you only need to follow certain principles and spend a very small amount of time and effort to create corresponding versions for different platforms, in most cases, you only need to re-compile the source program and connect it once.

 
· Llp64 abstract data model ·


the C language standard does not specify the number of variables, such as integer, long integer, and pointer, but is left to the computing platform for determination, therefore, each system and its application must adopt a default abstract data model as the basis for computing. Win32 adopts a model called llp32, assume that the characters of integer, long integer, and pointer variables are both 32 characters long, and the Data Types of int, uint, long, ulong, and handle are also 32 characters, this is a reasonable rule for 32-bit windows programming environments. If the length of integer, long integer, and pointer variable is changed to 64-bit in win64, it will not only occupy the storage space of the previous two times, in addition, the existing applications and most of their data types need to be changed, so it is quite difficult to achieve portability between the two platforms. In fact, after the introduction of the 64-bit platform, the application needs to implement 64-bit addressing, and only 64-bit data is needed in a few places. In most cases, 32-bit data is enough, therefore, win64 adopts an abstract data model called llp64. In addition to extending pointer variables to 64-bit, basic data types such as integer and long integer variables remain 32-bit. Because the basic data types remain 32-bit unchanged, the data stored on the disk does not need to change its structure and size, and shared data between remote or local processes (such as memory ing files) it does not need to change its structure and size, which greatly reduces the workload of programmers.

To achieve the same copySource codeIt can run both in Win32 and win64. Defining the llp64 model is only the first step. Next, we need to define some pointer-related data types, for example, the data type required for pointer calculation, the compiler determines whether the data type is 32-bit or 64-bit based on the target platform. These new data types are defined using the int and long types in C language, so they maintain backward compatibility with Win32 and some Windows API functions, microsoft plans to include these new data types in Beta 2 of Windows NT 5.0 and gradually port all Windows API functions to the 64-bit platform, after the launch of NT 5.0 Beta 2 and the corresponding Platform SDK, programmers can start to use these data types. After the release of the 64-bit platform, most of the programs you write only need to re-compile the connection once to generate a 64-bit version.


· New Data Types ·


There are three types of new data: Fixed-precision data types, pointer-precision data type, and specific-precision pointers ).

1. Fixed precision data types have the same word length in Win32 and win64. To facilitate memory, their names contain the word length.

· Int32 and int64: The characters are 32-bit and 64-bit signed integer numbers, respectively;

· Long32 and long64: The characters are 32-bit and 64-Bit Signed long integer respectively;

· Uint32 and uint64: The characters are 32-bit and 64-bit unsigned integer respectively;

· Ulong32 and ulong64: 32-bit and 64-bit unsigned long integer respectively.

2. the pointer precision data type is the same as the pointer length of the Target Platform (determined by the compiler). In this way, you can safely convert the pointer to the pointer precision data type for algebra and bit operations, instead of programming, consider the type of the Target Platform everywhere.

· Int-PTR and uint-PTR: the number of signed and unsigned integer types with pointer precision. The length of Win32 characters is 32 bits and that of win64 characters is 64 bits;

· Ssize-T and size-T: pointer precision counters, used to determine the length of the pointer precision data type. The former is a signed counter.

3. Specific precision pointers maintain the same length in both Win32 and win64, so they are only useful in some special cases.

·-Ptr64 (64-bit pointer): in Win32, 32-bit pointers generate a 64-bit pointer through symbol extension. The extension result may be meaningless and cannot be used as a pointer;

·-Ptr32 (32-bit pointer): In win64, a 64-bit pointer generates a 32-bit pointer by dropping the 32-bit high. The result may be meaningless, it cannot be used as a pointer.

Using these new data types can clearly show where the computation in the program is actually related to the pointer, so that the type conversion is not prone to errors, the data type originally defined in Win32 does not have this advantage, so the new data type is helpful for us to write more robustCodeAnd is ready for porting to the 64-bit platform in the future.


· Auxiliary development tools ·


Programming in win64 is slightly different from programming in Win32, because the types of pointer parameters in Windows API functions that you are familiar with may be changed, the original knowledge of programmers is still useful. To help programmers modify existing source code and use new data types, Microsoft will include some development auxiliary tools in NT 5.0 Beta 2, this includes a header file basetsd that defines the new data type. H and A syntax checker. The syntax Checker can check for incorrect type conversion, pointer truncation, and other 64-bit problems in the source program. For example, it indicates that the following code has a pointer truncation warning of 4311:

Buff = (puchar) srbcontrol;

(Ulong) buffer + = srbcontrol-> headerlength;

To eliminate the warning, you should change ulong to uint-PTR to ensure that this code can be run on both Win32 and win64. The programmer's goal is to eliminate all syntax checker warnings, especially the 4311 pointer truncation warning.


· Programming rules ·


To achieve source code portability of the two platforms, programmers should follow the following rules to write programs or modify existing programs.

1. you cannot convert a pointer to a 32-bit type with a fixed length of 32 characters, such as int, uint, long, ulong, or DWORD, the pointer should be converted to int-PTR or uint-PTR. The two types have the correct length on different platforms. In addition, because handle is essentially a pointer (void *), it is incorrect to convert handle to long or ulong type.

2. if it is determined that the pointer needs to be truncated, use the ptrtolong () and ptrtoulong () functions (in basetsd. h). They can block the pointer truncation warning, but the truncated result cannot be used again.

3. when an out parameter of an API function can return a pointer, you should be careful when processing the parameter. in Win32, You can forcibly convert the address of a ulong variable and pass it to the API function, the returned pointer is saved in the ulong variable, but in win64, the returned pointer has 64 bits. If the ulong variable is used, the contents of other variables will be damaged, the correct and simple method is to directly define a pointer variable and pass the address of the pointer variable as a parameter to the API function.

4. process the polymorphism parameters with caution. In Win32, a function can use a DWORD parameter to accept polymorphism parameters, that is, the parameter may have different meanings under different circumstances, such as interpreted as an integer or pointer. In win64, if a polymorphism parameter may be interpreted as a pointer, you must set the polymorphism parameter to the DWORD type instead of the uint-PTR or pvoid type. Some API functions of Win32 (such as raiseexception () need to be modified because they do not comply with the rule.

5. Use the new get/setwindowlongptr and get/setclasslongptr API functions. If the pointer is stored in the window or data area of the class, you need to call the above function to access the corresponding variables. To help the programmer correctly handle this in programming, the header file winuser. h. The definitions of the index values GWL-wndproc, GWL-hinstance, GWL-hwdparent, and gwl_userdata are canceled, instead, the new index values gwlp-wndproc, gwlp-hinstance, gwlp-hwdparent, and gwlp-userdata are defined. The following code will cause compilation errors:

Setwindowlong (hwnd, GWL-wndproc, (long) mywndproc );

Because GW-wndproc is not defined, the correct code should be:

Setwindowlongptr (hwnd, gwlp-wndproc, (INT-PTR) mywndproc );

6. Many data structures of windows and classes contain pointers. Therefore, you cannot specify an offset in the code to access data members. Instead, you should use the field-offset macro to calculate the offset.

7. since lparam, wparam, and lresult are usually used to store pointers or integers, all of them are extended to 64-bit in win64, therefore, they cannot be mixed with DWORD, ulong, uint, Int, Int, and long. Otherwise, they may be truncated unconsciously.

If you are interested in programming in the win64 environment, as well as API function changes and new functions on the win64 platform, you can check related information on the Microsoft Web site, or refer to the latest Platform SDK and msdn oline library.

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.