Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Create an app than doesnt show the console? (win32)

Moderators: larsivi kris

Posted: 03/15/07 16:54:28

Hi, how do i create an app that doesnt show a console window when it starts? I have a main() function which does the windows message loop thang.. i tried winMain but it seems that the compiler is expecting "main" as an entry point, but when i use that I get a console window aswell as my main app window.

I also tried to locate the tango source file that handles all this stuff but i couldnt find it.



Author Message

Posted: 03/15/07 19:41:52

It's kind of weird, but one way to do this is to create a WinMain? somewhere and call:

extern (C) int main(int argc, char **argv);

from that. Alternately, I understand there is some way to designate C main as the entry point for non-console programs. I think Build might even offer this as an option. In fact, I'd actually had a linkable module that followed the first approach until I heard about the option in Build. It seemed preferable so the module isn't available any more.

Posted: 03/15/07 20:47:20

Sorry I didnt explain myself very well. I'm not trying to designate main() as my entry point, im just trying to create a windows app that doesnt show a console window. Prior to installing Tango i just did this..

extern (Windows) int WinMain?(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)

and from that i can build a win32 app with no console window loitering about. If i use that same entry point with Tango i get the following message

C:\Documents and Settings\chris\Desktop\dproj>bud Project1.d -full OPTLINK (R) for Win32 Release 7.50B1 Copyright (C) Digital Mars 1989 - 2001 All Rights Reserved


Error 42: Symbol Undefined Dmain

Which looks to me like Tango's startup code is catching the main entry, starting up the gc and doing static initialization and then *always* looking for main() to pass on to.. So unless i use main() as my entry i cant compile with tango, and using main() always pops up a console window. So I cant work out how to make a WinMain? like entry point in Tango that doesnt show a console window.



Posted: 03/16/07 00:52:24

Strange, that portion of the startup code is identical to Phobos, as far as I know. But I think you need a D main() regardless, since this is the entry point for the D portion of the app. I'm surprised DMD doesn't complain if you omit this.

Posted: 03/16/07 00:59:49

Whether the console window pops up is generally controlled by a linker switch or a .def file. If you're using bud, you can just add the -gui switch. But then you can't use stdout for debugging, etc.

Posted: 03/16/07 01:11:00


IIRC, WinMain? replaces both the C main and the D main. Look at winsamp.d and dmain2.d that comes with dmd.

Posted: 03/16/07 02:32:18 -- Modified: 03/16/07 02:35:28 by
chris warwick

The -gui switch did it.

It turns out that bud, or somthing somwhere was rewriting the def file to be EXETYPE DOS. Which is weird cause it wasnt doing that before i installed tango.


Posted: 03/16/07 03:10:42 -- Modified: 03/16/07 03:11:40 by
chris warwick

Right well..

Apparently you dont have to use WinMain?, just main() will do, and the moduleCtors and gc_init ect.. are all called and set up prior to entry to main(). So this means you cant catch any exceptions that happen during static constructors / unit tests ect...

If you go for WinMain?, you have to call the moduleCtors and gc_init yourself. So you can wrap them in try catch and display the messages in a gui message box.

Here's the rub though.. since installing Tango I cannot use WinMain? unless I also have a main(), although main() is never actualy called. Omiting main() always gives this message..

C:\Documents and Settings\chris\Desktop\dproj>bud Project1.d -full -gui OPTLINK (R) for Win32 Release 7.50B1 Copyright (C) Digital Mars 1989 - 2001 All Rights Reserved


Error 42: Symbol Undefined Dmain

So you always need a main, but inclusion of WinMain? overrides its use.

IIRC it wasn't that way with DM phobos. And the winsamp example would point to that being the case.



Posted: 03/18/07 01:04:23

Everything is set up on entry to C main, which lives in the compiler runtime. This is the function you should start, if possible. If not, WinMain? can call it:

extern (C) int main( int argc, char** argv );

extern (Windows) int WinMain( HINSTANCE hInstance,
        	                  HINSTANCE hPrevInstance,
	                          LPSTR     lpCmdLine,
	                          int       nCmdShow )
    int       argc;
    wchar_t** argv;
    int       rval;

    argv = CommandLineToArgvW( GetCommandLineW(), &argc );
    rval = main( argc, null );
    LocalFree( argv );
    return rval;

The above code relies on the fact that the compiler runtime actually obtains its command-line arguments via GetCommandLineW. Portable, correct behavior would be to pass in a char** list of arguments instead, using WideCharToMultiByte? or whatever that function is called.