Win32 and win64 programming considerations

Source: Internet
Author: User
Tags integer numbers truncated
Data Model (lp32 ilp32 lp64 llp64 ilp64)

The 32-bit environment involves the "ilp32" data model because the C data type is 32-bit int, long, and pointer. The 64-bit environment uses different data models. At this time, the long and pointer are already 64-bit, which is called the "lp64" data model.

Currently, all 64-bit UNIX platforms use the lp64 data model, while 64-bit Windows uses the llp64 data model. Except for the 64-bit pointer, the basic types are not changed.

Type lp32 ilp32 lp64 ilp64 llp64

Char 8 8 8 8 8

Short 16 16 16 16 16

Int 16 32 32 64 32

Long 32 32 64 64 32

Long long 64 64 64 64 64

Pointer 32 32 64 64 64

 

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, do not use the long database type in the program. 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. Note that the socket accept function is incorrect in some operating systems because
The int * type received by accept, while the size_t type may be long Int. Later, BSD replaced it with sock_t.

 

 
Since Windows 95 was launched in 1995, most Windows applications have been transferred from Win16 to Win32.
3. X and its 16-bit windows programming technology were quickly entered into the cold Palace. Microsoft software engineers are now targeting intel's IA-64 Processor Architecture
The design of the 64-bit windows programming (win64) technology was started, because Windows 98 is a Windows
The last product in the 9x series, so win64 will be included in the Windows NT
5.0, when Intel's next-generation 64-bit processor Merced was launched, software developers could start to use 64-bit programming technology, and NT's enterprise computing capability would inevitably be greatly improved.

 

Because the 4 GB address space is sufficient
Currently, the majority of applications are required. The difference between win64 and Win32 is much smaller than that between Win32 and Win16, so win64 won't replace Win16 like Win32.
Quickly replace Win32. Win64 and Win32 will coexist and complement each other for a long period of time in the future. Software developers need to select a development platform based on the characteristics of the application, or
The win64 and Win32 versions of the application are created. Since win64 and Win32 programming are not much different, it takes a very small amount of time and effort to follow certain principles when programming in advanced languages.
You can create corresponding versions for different platforms, and most of the time you just 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.
Based on computing, Win32 uses a model called llp32, That is, assuming that the length of the integer, long integer, and pointer variable is 32 bits, the corresponding definitions of int, uint, long, And ulong
And handle and other data types are also 32-bit, which is reasonable for the 32-bit windows programming environment. If the length of integer, long integer, and pointer variable is changed
64-bit storage space not only occupies two times of the previous storage space, but also the existing applications and most data types used need to be changed, it is quite difficult to achieve portability between the two platforms. Things
In fact, after introducing a 64-bit platform, the application needs to implement 64-bit addressing, and only 64-bit data needs to be used in a few places. In most cases, 32-bit data is enough, therefore, win64 uses
An abstract data model called llp64, in addition to extending pointer variables to 64 bits, basic data types such as integer and long integer variables remain 32 bits. Because all basic data types are not 32-bit
Therefore, 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) does not need to change its structure and size, this greatly reduces the programmer's work.
Quantity.

To implement the same source code
Run in Win32 and run in win64. Defining the llp64 model is just the first step. Next, we need to define some pointer-related data types, such as the ones 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
Win32 and some Windows API functions are backward compatible. Microsoft plans to test Windows NT 5.0 Beta.
Version 2 contains these new data types, and gradually migrates all Windows API functions to a 64-bit platform. programmers in version NT 5.0 Beta 2 and the corresponding Platform
After the SDK is released, you can use these data types. After the 64-bit platform is officially launched, most of the programs you write only need to re-compile the connection 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. Therefore, the new data type helps us to write more robust code and prepare for porting to the 64-bit platform in the future.


· Auxiliary development tools ·

 
Programming in win64 is very different from programming in Win32, because you are familiar with Windows
In addition to the possible changes in the type of pointer-related parameters, API functions have no more changes. The programmer's original knowledge is still useful. To help programmers modify the existing source code, they can use the new
Data Type, Microsoft will be in NT 5.0 Beta
Version 2 contains some development auxiliary tools, including a header file basetsd. h defining the new data type and a syntax checker. The syntax Checker can check for incorrect type conversion in the source program,
Pointer truncation and other 64-bit problems. 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. the pointer cannot be converted
Int, uint, long, ulong, DWORD, and so on are fixed to 32-bit types. If you need to operate the pointer, convert the pointer 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
*). Therefore, it is incorrect to convert handle to the 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 API Function
When the out parameter can return a pointer, 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
In the ulong variable, but in win64, the returned pointer has 64 bits. If the ulong variable is used, the content of other variables will be destroyed. The correct and simple method is to define a pointer directly.
To pass the address of the pointer variable to the API function as a parameter.

4. process the polymorphism parameters with caution. In
In Win32, a function can use a DWORD parameter to accept the polymorphism parameter, that is, the parameter may have different meanings under different circumstances, for example, interpreted as an integer or pointer. In win64, if
A polymorphism parameter may be interpreted as a pointer. Therefore, the polymorphism parameter cannot be set to the DWORD type, but should be set to the uint-PTR or pvoid type. Some API functions of Win32 itself
(For example, raiseexception () must be modified because it does 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 removes the definitions of the index values GWL-wndproc, GWL-hinstance, GWL-hwdparent, and gwl_userdata,
Instead, the new index values gwlp-wndproc, gwlp-hinstance, gwlp-hwdparent, and gwlp-userdata are defined. In this case
The 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.