Currently, the form of WinMain documented for D2 is the same as that for D1. However, it's no good. _moduleUnitTests() doesn't exist in D2 for a start; a more subtle yet more serious problem is that it doesn't initialise the GC properly.
After a day or three of driving myself mad trying to figure why one of my apps was unstable under D2, I've finally come up with a form for WinMain that seems to work:
----------
import std.c.windows.windows;
import core.runtime;
extern (Windows)
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow) {
try {
Runtime.initialize();
runModuleUnitTests();
return myWinMain(hInstance, hPrevInstance, lpCmdLine, nCmdShow);
} catch (Object o) {
MessageBoxA(null, toStringz(o.toString()),
"Fatal Internal Error", MB_OK | MB_ICONEXCLAMATION);
return 0;
} finally {
Runtime.terminate();
}
}
Comment #1 by dfj1esp02 — 2009-01-13T05:53:08Z
Isn't proper startup code can be found in dmain2.d ?
Comment #2 by smjg — 2009-01-13T08:21:15Z
That's exactly what Runtime.initialize and Runtime.terminate do - call the "proper startup code".
Comment #3 by smjg — 2009-01-24T06:51:40Z
Correction: Runtime.initialize calls the proper startup code, and Runtime.terminate calls the proper shutdown code.
Comment #4 by sean — 2009-01-30T20:01:00Z
That looks about right. I had thought that people wouldn't want unit tests run for dynamic libraries, which is why that routine needs to be called manually. If this is incorrect then I can add it to Runtime.initialize() as well. I'll look into changing the docs.
Comment #5 by smjg — 2009-01-30T20:53:07Z
(In reply to comment #4)
> That looks about right. I had thought that people wouldn't want
> unit tests run for dynamic libraries,
In which case they'll compile them without --unittest - problem solved.
> which is why that routine needs to be called manually. If this is
> incorrect then I can add it to Runtime.initialize() as well. I'll
> look into changing the docs.
You're right - Runtime.initialize() ought to call runModuleUnitTests(). People using the currently correct WinMain will find that the unit tests run twice, but at least they just have to remove the extra call in order to fix it.