| 1 |
Ddoc |
|---|
| 2 |
|
|---|
| 3 |
$(D_S D for Win32, |
|---|
| 4 |
|
|---|
| 5 |
$(P This describes the D implementation for 32 bit Windows systems. |
|---|
| 6 |
Naturally, |
|---|
| 7 |
Windows specific D features are not portable to other platforms. |
|---|
| 8 |
) |
|---|
| 9 |
|
|---|
| 10 |
$(P Instead of the:) |
|---|
| 11 |
|
|---|
| 12 |
$(CCODE |
|---|
| 13 |
#include <windows.h> |
|---|
| 14 |
) |
|---|
| 15 |
$(P of C, in D there is:) |
|---|
| 16 |
|
|---|
| 17 |
-------------------- |
|---|
| 18 |
import std.c.windows.windows; |
|---|
| 19 |
-------------------- |
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
<h2>Calling Conventions</h2> |
|---|
| 23 |
|
|---|
| 24 |
$(P In C, the Windows API calling conventions are $(CODE __stdcall). |
|---|
| 25 |
In D, it is simply: |
|---|
| 26 |
) |
|---|
| 27 |
|
|---|
| 28 |
-------------------- |
|---|
| 29 |
extern (Windows) |
|---|
| 30 |
{ |
|---|
| 31 |
/* ... function declarations ... */ |
|---|
| 32 |
} |
|---|
| 33 |
-------------------- |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
$(P The Windows linkage attribute sets both the calling convention |
|---|
| 37 |
and the name mangling scheme to be compatible with Windows. |
|---|
| 38 |
) |
|---|
| 39 |
|
|---|
| 40 |
$(P For functions that in C would be $(CODE __declspec(dllimport)) or |
|---|
| 41 |
$(CODE __declspec(dllexport)), use the $(CODE export) attribute: |
|---|
| 42 |
) |
|---|
| 43 |
|
|---|
| 44 |
-------------------- |
|---|
| 45 |
export void func(int foo); |
|---|
| 46 |
-------------------- |
|---|
| 47 |
|
|---|
| 48 |
$(P If no function body is given, it's imported. If a function body |
|---|
| 49 |
is given, it's exported. |
|---|
| 50 |
) |
|---|
| 51 |
|
|---|
| 52 |
<h2>Windows Executables</h2> |
|---|
| 53 |
|
|---|
| 54 |
$(P Windows GUI applications can be written with D. |
|---|
| 55 |
A sample such can be found in $(TT $(DMDDIR)\samples\d\winsamp.d) |
|---|
| 56 |
) |
|---|
| 57 |
|
|---|
| 58 |
$(P These are required:) |
|---|
| 59 |
|
|---|
| 60 |
$(OL |
|---|
| 61 |
|
|---|
| 62 |
$(LI Instead of a $(CODE main) function serving as the entry point, |
|---|
| 63 |
a $(CODE WinMain) function is needed. |
|---|
| 64 |
) |
|---|
| 65 |
|
|---|
| 66 |
$(LI $(CODE WinMain) must follow this form: |
|---|
| 67 |
-------------------- |
|---|
| 68 |
import core.runtime; |
|---|
| 69 |
import std.c.windows.windows; |
|---|
| 70 |
|
|---|
| 71 |
extern (Windows) |
|---|
| 72 |
int $(B WinMain)(HINSTANCE hInstance, |
|---|
| 73 |
HINSTANCE hPrevInstance, |
|---|
| 74 |
LPSTR lpCmdLine, |
|---|
| 75 |
int nCmdShow) |
|---|
| 76 |
{ |
|---|
| 77 |
int result; |
|---|
| 78 |
|
|---|
| 79 |
void exceptionHandler(Exception e) |
|---|
| 80 |
{ |
|---|
| 81 |
throw e; |
|---|
| 82 |
} |
|---|
| 83 |
|
|---|
| 84 |
try |
|---|
| 85 |
{ |
|---|
| 86 |
Runtime.initialize(&exceptionHandler); |
|---|
| 87 |
|
|---|
| 88 |
result = $(B myWinMain)(hInstance, hPrevInstance, lpCmdLine, nCmdShow); |
|---|
| 89 |
|
|---|
| 90 |
Runtime.terminate(&exceptionHandler); |
|---|
| 91 |
} |
|---|
| 92 |
|
|---|
| 93 |
catch (Object o) // catch any uncaught exceptions |
|---|
| 94 |
{ |
|---|
| 95 |
MessageBoxA(null, cast(char *)o.toString(), "Error", |
|---|
| 96 |
MB_OK | MB_ICONEXCLAMATION); |
|---|
| 97 |
result = 0; // failed |
|---|
| 98 |
} |
|---|
| 99 |
|
|---|
| 100 |
return result; |
|---|
| 101 |
} |
|---|
| 102 |
|
|---|
| 103 |
int $(B myWinMain)(HINSTANCE hInstance, |
|---|
| 104 |
HINSTANCE hPrevInstance, |
|---|
| 105 |
LPSTR lpCmdLine, |
|---|
| 106 |
int nCmdShow) |
|---|
| 107 |
{ |
|---|
| 108 |
/* ... insert user code here ... */ |
|---|
| 109 |
} |
|---|
| 110 |
-------------------- |
|---|
| 111 |
|
|---|
| 112 |
The $(TT myWinMain()) function is where the user code goes, the |
|---|
| 113 |
rest of $(TT WinMain) is boilerplate to initialize and shut down |
|---|
| 114 |
the D runtime system. |
|---|
| 115 |
) |
|---|
| 116 |
|
|---|
| 117 |
$(LI A $(CODE .def) |
|---|
| 118 |
($(LINK2 http://www.digitalmars.com/ctg/ctgDefFiles.html, Module Definition File)) |
|---|
| 119 |
with at least the following |
|---|
| 120 |
two lines in it: |
|---|
| 121 |
|
|---|
| 122 |
$(MODDEFFILE |
|---|
| 123 |
EXETYPE NT |
|---|
| 124 |
SUBSYSTEM WINDOWS |
|---|
| 125 |
) |
|---|
| 126 |
|
|---|
| 127 |
Without those, Win32 will open a text console window whenever |
|---|
| 128 |
the application is run. |
|---|
| 129 |
) |
|---|
| 130 |
|
|---|
| 131 |
$(LI The presence of $(TT WinMain()) is recognized by the compiler |
|---|
| 132 |
causing it to emit a reference to |
|---|
| 133 |
$(LINK2 http://www.digitalmars.com/ctg/acrtused.html, __acrtused_dll) |
|---|
| 134 |
and the phobos.lib runtime library. |
|---|
| 135 |
) |
|---|
| 136 |
|
|---|
| 137 |
) |
|---|
| 138 |
) |
|---|
| 139 |
|
|---|
| 140 |
Macros: |
|---|
| 141 |
TITLE=Windows |
|---|
| 142 |
WIKI=Windows |
|---|