Simple GUI test

Daniel Keep daniel.keep.lists at gmail.com
Mon Apr 23 08:21:31 PDT 2007



smithfox wrote:
> Thanks all,
> 
> Actually, My simple GUI test code is from winsamp.d, and I also have written def file. If I use main function instead of WinMain. Everything is OK.
> 
> (1) I'm confused that how to write startup code in a GUI program without 3rd lib/framework(like dfl,dwt).
> 
> D language suggests to use main function as starup code in GUI program,
> but why the "winsamp.d"  starup with "WinMain"? 
> 
> (2) And I found following codes in "winsamp.d".
> extern (C) void gc_init();
> extern (C) void gc_term();
> extern (C) void _minit();
> extern (C) void _moduleCtor();
> extern (C) void _moduleUnitTests();
> 
> I searched all codes in dfl source and dwt source , but didn't found above codes.
> 
> (3) How to write a standard GUI code just like using win32 SDK?
> 
> Thanks

I'm a bit out of it at the moment, and I'm not 100% sure what's wrong,
but I think I know what the problem is.

See, main() functions in D aren't simply jumped into when the executable
is run.  When your D program is run, there's another main() function
that gets called *first*; for the sake of discussion, let's call this
pre-main.  Pre-main's job is to start up and shut down the garbage
collector, call module constructors, run unit tests and wrap your main()
function in try{}catch(){} handlers so that uncaught exceptions can be
printed to the user.

*However* if your program uses WinMain as the entry point, none of this
is done.  That means that when your program starts at WinMain, the
garbage collector isn't running, none of the imported modules have been
initialised, etc.

This is why you need to write all that extra horrible code: since you're
bypassing pre-main, you need to do it's job manually.  All those
extern(C) functions are either part of the standard library (like
gc_init and gc_term) or generated by the compiler when you make your
program, and are what pre-main calls to set everything up.

Basically, you should copy+paste everything from line 106 down in
winsamp.d into your program.  You can play around with line 135 to
change what function is called to actually "run" your program, and what
you pass to it.

Once you've taken care of all that nasty startup code, everything should
work like any other Win32 GUI program--no need for dfl or dwt or
anything else.  You might want to grab the win32 headers from (this is
off the top of my head, so it might be off): dsource.org/projects/bindings.

Hope that helps :)

	-- Daniel

-- 
int getRandomNumber()
{
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

http://xkcd.com/

v2sw5+8Yhw5ln4+5pr6OFPma8u6+7Lw4Tm6+7l6+7D
i28a2Xs3MSr2e4/6+7t4TNSMb6HTOp5en5g6RAHCP  http://hackerkey.com/



More information about the Digitalmars-d mailing list