Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
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 a local clone.

Windows Programming

This covers Windows programming with 32 bit MSCOFF, 64 bit MSCOFF, console programs, GUI programs, and DLLs.

All Console Programs

A D console program is specified by having a function with D linkage at module level called main. When the compiler sees this, it triggers the mixin template core.internal.entrypoint._d_cmain to be added. The _d_cmain template looks like:

template _d_cmain()
{
    extern(C)
    {
        int _d_run_main(int argc, char **argv, void* mainFunc);

        int _Dmain(char[][] args);

        int main(int argc, char **argv)
        {
            return _d_run_main(argc, argv, &_Dmain);
        }

        // Solaris, for unknown reasons, requires both a main() and an _main()
        version (Solaris)
        {
            int _main(int argc, char** argv)
            {
                return main(argc, argv);
            }
        }
    }
}

The main function in it, because it is marked as extern(C), is the entry point of a C console program. The executable starts up as a C executable, with the C runtime library initialization and then the C main() function is called.

The C runtime library runs any D functions tagged with pragma(crt_constructor) as part of its initialization, and before it calls C main(). The order in which these are run is not specified.

The C main function then calls the D runtime library function _d_run_main, passing it argc and argv, and the address of _Dmain. The compiler renames the D main function in the source code to _Dmain.

_d_run_main then initializes the D runtime library, converts argc and argv to args, and calls the D main function.

When the D main function returns, control is back in _d_run_main which shuts down the D runtime library, and then returns to the C main, which then returns to the C library which shuts down the C runtime library, then exits the program.

The C runtime library runs any D functions tagged with pragma(crt_destructor) as part of its shutdown, in the reverse order that the pragma(crt_constructor)s were run.

Windows 32 and 64 bit MSCOFF Programs

32 bit and 64 bit MSCOFF programs use the Microsoft Visual C/C++ compiler as the Associated C Compiler, generate object files in the MSCOFF format, and use the Microsoft linker to link them.

Console Programs

Windows GUI Programs

DLLs

Live Functions