[Home]
[Search]
[D]
Instead of the:
#include <windows.h>of C, in D there is:
import std.c.windows.windows;
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.
These are required:
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 _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);
}
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.
EXETYPE NT SUBSYSTEM WINDOWSWithout those, Win32 will open a text console window whenever the application is run.