Removing the Windows console

When you write a GUI program on Windows, you'll often want to suppress the automatic allocation of a console window while building the release executable.

How to do it…

Let's remove the Windows console by executing the following steps:

  1. Write your program normally.
  2. While compiling, pass –L/subsystem:windows to dmd.

How it works…

A D program compiles the program to an .exe file, which means it can use all the same linker capabilities as a program in C, including subsystems, 16-bit stubs (though D itself cannot be compiled to 16 bits), resources, and manifests.

The –L option of dmd forwards the given option to the linker. This can be used to pass any command to the linker, including the platform-specific ones, such as /subsystem, seen here. By choosing the Windows subsystem, the operating system will not allocate a console.

There's more…

It is also possible to write a WinMain function in D. If you write WinMain instead of main, the linker will automatically mark it as using the Windows subsystem, without having to use the /subsystem switch. If you do, be sure to call Runtime.initalize() before doing anything. However, it is generally better to write programs with a normal main function and use the linker switch since this ensures the runtime functions are always called correctly without the boilerplate. If you need the module handle or command line in a main function, use GetModuleHandle(null) and GetCommandLineW, respectively.

See also

  • The dmd ZIP file that contains dmd2/samples/d/winsamp.d has a sample of how to write a WinMain function
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.156.93