Aniket Inge
Aniket Inge

Reputation: 25705

What function actually calls WinMain

How is WinMain() actually called? I remember a function used by pro-hackers that started with (something) that looked like __startupWinMain().

The problem is, I have a Win32 EXE(compiled with /SUBSYSTEM:WINDOWS) but gets arguments from command-line. If the command line is incorrect, the process should print a help message to the console.

How can I manually deallocate(or FreeConsole()) from an exe with /SUBSYSTEM:WINDOWS linker option?

Upvotes: 4

Views: 1015

Answers (4)

Adrian McCarthy
Adrian McCarthy

Reputation: 47952

How is WinMain() actually called?

If you single-step to the first line of your program in a debugger, and then look at the stack, you can see how WinMain gets called. The actual start function for a typical build is a function pulled in from the run-time library. For me, it's _WinMainCRTStartup, but I suppose it might vary depending on the version of the compiler, linker, and library you build with. The startup function from the run-time library does some initialization and then calls WinMain.

Using dumpbin /headers (or another program that can inspect a PE binary), you can confirm which function is the "entry point" to your executable. Unless you've done something to change it, you'll probably see _WinMainCRTStartup, which is consistent with what the stack trace tells us.

That should answer your question, but it doesn't solve your problem. It looks like some others have posted good solutions.

Upvotes: 0

Chirag
Chirag

Reputation: 1

How does the compiler know to invoke wWinMain instead of the standard main function? What actually happens is that the Microsoft C runtime library (CRT) provides an implementation of main that calls either WinMain or wWinMain. Note The CRT does some additional work inside main. For example, any static initializers are called before wWinMain. Although you can tell the linker to use a different entry-point function, use the default if you link to the CRT. Otherwise, the CRT initialization code will be skipped, with unpredictable results. (For example, global objects will not be initialized correctly.)

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612954

As the very first act of your program, check the parameters. If they are fine, continue as normal.

Otherwise call AttachConsole passing ATTACH_PARENT_PROCESS. If that succeeds, then you can print your error to stdout and quit. If it doesn't, then you'll have to show the error in a message box.

Upvotes: 3

Steve Valliere
Steve Valliere

Reputation: 1207

Perhaps you should consider having the program pop up a message box when the command line is incorrect. Something like this:

MessageBox( NULL, "(description of command line error)",
            "MyProg - Command Line Error",
            MB_OK|MB_ICONEXCLAMATION );

This will open a message box in the center of the display and wait for the user to acknowledge it before actually terminating your program.

On the other hand, you could build your program as a console app and use printf() to write to the console. A console program may still create windows, but the console itself will hang around unless you figure out how to detach from it (and then, of course, you will no longer be able to use printf().)

Upvotes: 1

Related Questions