D Programming Language
Improve this page Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone. Page wiki View or edit the community-maintained wiki page associated with this page.

# Windows

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 core.sys.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 \dmd2\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 core.runtime;
       import core.sys.windows.windows;
       import std.string;

       extern (Windows)
       int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine, int nCmdShow)
       {
           int result;

           void exceptionHandler(Throwable e) {
               throw e;
           }

           try
           {
               Runtime.initialize(&exceptionHandler);
               result = myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
               Runtime.terminate(&exceptionHandler);
           }
           catch (Throwable e) // catch any uncaught exceptions
           {
               MessageBoxA(null, e.toString().toStringz(), "Error",
                           MB_OK | MB_ICONEXCLAMATION);
               result = 0;     // failed
           }

         return result;
       }

       int myWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpCmdLine, int nCmdShow)
       {
           // ... insert user code here ...
           return 0;
       }

   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.

## Windows Programming Examples

A collection of over 140 Windows D programming code examples is available at this Github repository.
Forums | Comments | Search | Downloads | Home
Copyright © 1999-2013 by Digital Mars ®, All Rights Reserved | Page generated by Ddoc on Sat Oct 12 14:20:57 2013