Portable APIs

Thus far, you’ve read a lot about API functions that have changed—in major or minor respects—from Windows 3.x to 98/95/NT. We’ve also covered elements that, despite changes, have been retained in one form or another to provide backward compatibility. There are other API functions that are the same in both Windows 3.x and 98/95/NT, but which have not been generally used by Windows 3.x programmers, simply because other, parallel functions have been the accepted standards.

The following code fragment example is typical of a Windows 3.x application that is setting a graphics mapping mode, nominally in response to a WM_PAINT message.

    SetMapMode( hdc, nCurMode );                     // 3.x
    SetWindowExt( hdc, xWinAspect, yWinAspect );
    SetViewportExt( hdc, xViewAspect, yViewAspect );
    SetWindowOrg( hdc, xOrg, yOrg );
    MoveTo( hdc, xPos, yPos );

Except for the first API function, SetMapMode, which remains the same under Windows 98/95/NT, none of the remaining API functions are supported by 98/95/NT. But each of the four unsupported API functions has a counterpart, portable API function that differs from the format shown in only minor respects.

The following code fragment shows the same code using the portable API functions and is compatible with both Windows 3.x and 98/95/NT.

    SetMapMode( hdc, nCurMode );               // 3.x, 98/95/NT
    SetWindowExtEx( hdc, xWinAspect, yWinAspect, NULL );
    SetViewportExtEx( hdc, xViewAspect, yViewAspect, NULL);
    SetWindowOrgEx( hdc, xOrg, yOrg, NULL );
    MoveToEx( hdc, xPos, yPos, NULL );

As you will notice, each of the four portable APIs has essentially the same function name; the only difference is the addition of the extension Ex to the name. They also have one additional argument, which, in this example, is NULL in each case.

The NULL arguments have been used for compatibility with the original functional intent, but they could be used as pointers to data structures that return previous settings. For example, for the SetWindowExtEx and SetViewportExtEx API functions, the final argument could be a pointer to a SIZE structure, which would return the previous window or viewport extents. In like fashion, the SetWindowOrgEx and MoveToEx API calls could use a pointer to a POINT structure, which would return the previous origin or the current position.

Remember that for either Windows 3.x or 98/95/NT applications, the final argument is optional in each of these API functions. If you do not need the return information, use the NULL argument.

The portable APIs described here are only a few of the portable APIs available, but they are representative of the ones most commonly encountered. When your conversion attempts encounter error messages reporting that a Windows 3.x function is no longer recognized, look for a similar API name. You will probably find a portable API with the same functionality, even though it may have slightly different parameters.

© 1998 SYBEX Inc. All rights reserved.