Calling main() from WinMain()

Artur Skawina art.08.09 at gmail.com
Tue Jun 5 08:31:22 PDT 2012


On 06/05/12 16:40, BLM768 wrote:
> I'm working on a cross-platform GUI library and I'd like to be able to call the user's main() function from WinMain() so users don't have to write both functions. However, I'm at a bit of a loss as to how to accomplish that. I know that D internally creates the extern(C) main(), which in turn calls D's main(), but I think that the C main() also performs some runtime initialization, which means that I probably shouldn't be calling it after WinMain() has already started. Since the D main() is defined at module scope, I'm not sure how to access it without importing one of the user's modules. I think that each module can define its own main() as well, which really complicates things. Is there a reasonably simple way to get the right main() function?
> 
> I hate to say it, but this is a problem that would actually be easier in C++ :)

D main's name is not mangled (which also causes trouble if you try to define
a "main" inside more than one module).

So you can just do

   extern (C) int _Dmain(string[] args);

and call this one (assuming that Windows doesn't need special treatment,
no idea about that)


artur


More information about the Digitalmars-d-learn mailing list