Im­prove this page Quickly fork, edit on­line, and sub­mit a pull re­quest for this page. Re­quires a signed-in GitHub ac­count. This works well for small changes. If you'd like to make larger changes you may want to con­sider using local clone. Page wiki View or edit the com­mu­nity-main­tained wiki page as­so­ci­ated with this page.

Win­dows

This de­scribes the D im­ple­men­ta­tion for 32 bit Win­dows sys­tems. Nat­u­rally, Win­dows spe­cific D fea­tures are not portable to other plat­forms.

In­stead of the:

#include <windows.h>

of C, in D there is:

import core.sys.windows.windows;

Call­ing Con­ven­tions

In C, the Win­dows API call­ing con­ven­tions are __stdcall. In D, it is sim­ply:

extern (Windows)
{
	/* ... function declarations ... */
}

The Win­dows link­age at­tribute sets both the call­ing con­ven­tion and the name man­gling scheme to be com­pat­i­ble with Win­dows.

For func­tions that in C would be __declspec(dllimport) or __declspec(dllexport), use the export at­tribute:

export void func(int foo);

If no func­tion body is given, it's im­ported. If a func­tion body is given, it's ex­ported.

Win­dows Ex­e­cuta­bles

Win­dows GUI ap­pli­ca­tions can be writ­ten with D. A sam­ple such can be found in \samples\d\winsamp.d

These are re­quired:

  1. In­stead of a main func­tion serv­ing as the entry point, a WinMain func­tion is needed.
  2. WinMain must fol­low 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() func­tion is where the user code goes, the rest of WinMain is boil­er­plate to ini­tial­ize and shut down the D run­time sys­tem.
  3. A .def (Mod­ule De­f­i­n­i­tion File) with at least the fol­low­ing two lines in it:
    EXETYPE NT
    SUBSYSTEM WINDOWS
    
    With­out those, Win32 will open a text con­sole win­dow when­ever the ap­pli­ca­tion is run.
  4. The pres­ence of WinMain() is rec­og­nized by the com­piler caus­ing it to emit a ref­er­ence to __acr­tused_dll and the phobos.​lib run­time li­brary.

Win­dows Pro­gram­ming Ex­am­ples

A col­lec­tion of over 140 Win­dows D pro­gram­ming code ex­am­ples is avail­able at this Github repos­i­tory.
Forums | Comments | Search | Downloads | Home