Share via


Using Proper Data Types

Windows 3.x source code often uses the type WORD interchangeably with types such as HWND and HANDLE. For example, the type cast (WORD) might be used to cast a data type to a handle:

hWnd = (WORD) SendMessage( hWnd, WM_GETMDIACTIVATE, 0, 0 );

This code compiles Windows 3.x applications correctly because both the WORD type and handles are 16 bits. But the code produces errors when compiled for Win32 because handles (such as HWND types) grow to 32 bits while the WORD type is still 16 bits.

Examine each occurrence of WORD casts and data definitions in your code, and revise as follows:

  • If a variable or expression is to hold a handle, replace WORD with HWND, HPEN, HINSTANCE, or another handle type.

  • If a variable or expression is a graphics coordinate or some other integer value that grows from 16 to 32 bits, replace WORD with UINT, and short with int.

  • Continue to use the WORD type only if the data type needs to be 16 bits for all versions of Windows (usually because it is a function argument or structure member).

In the portable version of the previous example, the (WORD) cast is replaced by (HWND):

hWnd = (HWND) SendMessage( hWnd, WM_GETMDIACTIVATE, 0, 0 );

In general, it is best to use the most specific type possible. Avoid using a generic handle type such as HANDLE, and use a more specific type such as HPEN. You should also define specific types for application-specific objects you create.