www.digitalmars.com Home | Search | D | Comments
Last update Sat Mar 18 10:52:49 2006
D
Language
Phobos
Comparisons

· Overview
· D for Win32
· Win32 DLLs in D
· C .h to D Modules
· FAQ
· Style Guide
· Example: wc
· Future
· D Change Log
· Tech Tips
· Rationale
· Exception Safety
· Templates Revisited
· Warnings
· Glossary
· Acknowledgements

Tools
· DMD D Compiler
· GDC D Compiler
· Linker
· Profiler
· Code Coverage
· DMD Script Shell

Community
· News
· Forum
· Announcements
· Learn
· D links

Archives
· digitalmars.D
· digitalmars.D.dtl
· digitalmars.D.announce
· digitalmars.D.dwt
· digitalmars.D.learn
· digitalmars.D.bugs
· D.gnu
· Old D

D for Win32

This describes the D implementation for 32 bit Windows systems. Naturally, Windows specific D features are not portable to other platforms.

Instead of the:

#include <windows.h>
of C, in D there is:
import std.c.windows.windows;

Calling Conventions

In C, the Windows API calling conventions are __stdcall. In D, it is simply:
extern (Windows)
{
	/* ... function declarations ... */
}
The Windows linkage attribute sets both the calling convention and the name mangling scheme to be compatible with Windows.

For functions that in C would be __declspec(dllimport) or __declspec(dllexport), use the export attribute:

export void func(int foo);
If no function body is given, it's imported. If a function body is given, it's exported.

Windows Executables

Windows GUI applications can be written with D. A sample such can be found in \dmd\samples\d\winsamp.d

These are required:

  1. Instead of a main function serving as the entry point, a WinMain function is needed.
  2. WinMain must follow this form:
    import std.c.windows.windows;
    
    extern (C) void gc_init();
    extern (C) void gc_term();
    extern (C) void _minit();
    extern (C) void _moduleCtor();
    extern (C) void _moduleDtor();
    extern (C) void _moduleUnitTests();
    
    extern (Windows)
    int WinMain(HINSTANCE hInstance,
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
    	int nCmdShow)
    {
        int result;
    
        gc_init();			// initialize garbage collector
        _minit();			// initialize module constructor table
    
        try
        {
    	_moduleCtor();		// call module constructors
    	_moduleUnitTests();	// run unit tests (optional)
    
    	result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
    
    	_moduleDtor();		// call module destructors
        }
    
        catch (Object o)		// catch any uncaught exceptions
        {
    	MessageBoxA(null, cast(char *)o.toString(), "Error",
    		    MB_OK | MB_ICONEXCLAMATION);
    	result = 0;		// failed
        }
    
        gc_term();			// run finalizers; terminate garbage collector
        return result;
    }
    
    int myWinMain(HINSTANCE hInstance,
    	HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine,
    	int nCmdShow)
    {
        /* ... insert user code here ... */
    }
    
    The myWinMain() function is where the user code goes, the rest of WinMain is boilerplate to initialize and shut down the D runtime system.
  3. A .def (Module Definition File) with at least the following two lines in it:
    EXETYPE NT
    SUBSYSTEM WINDOWS
    
    Without those, Win32 will open a text console window whenever the application is run.
  4. The presence of WinMain() is recognized by the compiler causing it to emit a reference to __acrtused_dll and the phobos.lib runtime library.