digitalmars.com                        
Last update Sun Mar 4 11:58:07 2018

Win32 Programming Guidelines

This chapter describes how to use the compiler to create Win32 and Win32s applications. For information about:

What's in This Chapter

Some Basic Information

The following will help you understand the information in this chapter. For machines based on the Intel 80x86 architecture, a 32-bit operating system improves the performance of data manipulation and calculations because it avoids the overhead of loading segment registers. Applications that are data-, memory-, or calculation-intensive may perform significantly better in 32-bit mode.

About Win32s

Win32s is an NT emulator or operating system extension that allows you to run Win32 applications on a Windows 3.1 host. The "s" in Win32s stands for Win32 subset: Win32s supports almost all Win32 API features.

Note: Win32s applications built with C++ Version 7 will only run on version 1.15 and above of Win32s.

Advantages of Win32s over Win16

Win32s, included with Digital Mars C++, gives you the following advantages over conventional Win16 applications: Win32s lets your Win32 applications make 32-bit calls to Windows 3.1, where graphics and windowing operations are performed. Win32s consists of a virtual device driver (VxD) and a set of dynamic link libraries (DLLs) that extend Windows to support 32-bit applications.

Key Win32s features

Many features are available through the Win32s Applications Programming Interface (API), such as:

Building Win32 and Win32s Applications

Digital Mars C++ includes a set of tools that make it easy to create and debug Win32 and Win32s applications from either the command line or within the integrated environment. By setting switches, all the tools- compiler, linker, resource compiler, and resource editors- generate the correct type of 32-bit application. The following is basic information useful for building Win32 and Win32s applications: The OPTLINK linker does not support COFF format .lib or .obj files produced by Microsoft tools.

Note: To developers using NT: OPTLINK (the linker) does not accept COFF resource files, but does accept standard Microsoft resources files.

Compiling Win32 Executables

This section explains how to create various kinds of Win32 executables.

Compiling with MFC 3.x

If your Win32 application uses MFC 3.x libraries, see the "READ ME" file included with the distribution for a list of the options, libraries, and #defines you need to build various targets with the latest MFC release. Note that, for all MFC 3.x libraries, you need to compile with the NT (-mn) memory model.

Compiling Win32 DLLs

To compile Win32 DLLs, use these compiler options:
-mn -WD 
To write a Win32 DLL, you need to specify the entry point DllMain(). When the compiler detects a call to DllMain() in a 32-bit compilation, it generates a reference to the _acrtused_ dll startup function, and links with the appropriate startup code.

Unlike previous versions of C++, this startup code calls static constructors and performs other initialization steps before calling DllMain().

Converting older Win32 DLLs

To convert Win32 DLLs built with previous versions of C++ to work with this version, make the changes listed below: Note: Win32 DLLs compiled with previous versions of C++, in which the entry point is specified with #pragma startaddress, need to be rewritten to call DllMain() instead. Otherwise any static constructors will not be called, and any run-time library calls will not work as expected because proper initialization has not taken place.

Building Console Applications

A console application is a character mode application that runs under Windows 95 or NT. Digital Mars C++ supports console mode applications.

To compile console applications, use the -mn compiler option, and specify the SUBSYSTEM CONSOLE directive in the .def file.

Startup code for console applications

When the compiler detects a call to main() in a 32-bit compilation, it generates a reference to the _acrtused_ con startup function, and links with the appropriate startup code. You no longer need to explicitly link in cc.obj, as with previous versions of C++; it is now part of snn.lib.

Compiling Win32s Executables

To compile Win32s executables, use the -mn and -WA compiler options. Note: HP Dashboard does not support Win32s. If you try to debug a Win32s application under Dashboard, the debugger receives a GPF message from the application being debugged.

Compiling Win32s DLLs

To compile Win32s executables, use the -mn and -WA compiler options.

Note: To create libraries from Windows NT System DLLs (kernel32.dll, for example), use the IMPLIB utility and specify the /system switch. IMPLIB is available only from the command line, not via the IDDE.

Win32 Definition File Directives

This section lists the directives used in definition files for Win32 programs. For descriptions of these directives see Definition File Directives.
NAME name [BASE=number] 

LIBRARY name [ BASE= number | PROCESSINIT |
	PROCESSTERM | THREADINIT | 
	THREADTERM ] 

DESCRIPTION 'descriptive line' 

EXETYPE NT 

HEAPSIZE [number, commit] 

STACKSIZE [number, commit] 

STUB 'filespec' 

SUBSYSTEM [ NATIVE | WINDOWS | CONSOLE | POSIX ] 

VERSION number[. number] 

CODE [READ] [WRITE] [EXECUTE ] [SHARED] 

DATA [READ] [WRITE] [EXECUTE] [SHARED] 

SEGMENTS
{   name [CLASS 'class'] 
	[READ] [WRITE] 
	[EXECUTE] [SHARED]
} 


IMPORTS { [internal=] externalfile. func } 

EXPORTS { parms [=intname] [@number [NONAME]] 
	[CONSTANT]} 

Recompiling Win16 Applications for Win32

To convert to a Win32 or Win32s application to a Win16 application created with the C/C++ compilers, try these suggestions: Note: For more tips on how to solve specific problems that might occur when porting 16-bit code to Win32, see Porting Guide.

Converting Non-Digital Mars Win16 Applications to Win32 Applications

The following information will help you convert 16-bit programs developed with other programming tools to be Win32 or Win32s applications.

Start with a bug-free Win16 application. Be sure your Win16 application compiles and runs without any compiler or linker errors or warnings.

Convert the application to be a Win16 application. For instructions on porting code from Borland or Microsoft, see Switching to Digital Mars C++. Be sure the converted program compiles and runs with Digital Mars C++ without any errors or warnings.

Save the original source code and makefile, preferably in another project directory. Now that the application is a Digital Mars Win16 application, convert it to Win32, following the hints above.

Finally, after a clean build, preferably with no warnings, test the application.

If you are targeting both Win32 and Win32s, you should test these applications thoroughly in both environments, as there are a number of significant differences between Win32 and Win32s.

Home | Runtime Library | IDDE Reference | STL | Search | Download | Forums