views:

261

answers:

3

I was under the impression that this code

#include <windows.h>

#include <stdio.h>

int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR szCmdLine, int iCmdShow)
{
    printf("WinMain\n");

    return 0;
}

int main()
{
    printf("main\n");

    return 0;
}

would output WinMain, but of course nothing ever works how you expects.

Anyways, could somebody please tell me how to get this program to run WinMain first (I do have a reason for using both). I'm running windows 7 with mingw if that helps anything.

+4  A: 

You need to put -mwindows on the command line when you call MinGw. Check this out as a gentle introduction to Windows programming with MinGW.

Also: you cannot have two entry points in an executable, so you probably can not do what you want to do.

Travis Gockel
Sorry that didn't work. I changed the code to<Code>printf("main\n");MessageBox (NULL, TEXT ("main"), TEXT ("main"), 0) ;return 0;</Code>in main and guess who showed up.I hope you're not correct that using both is not a possibility because I've seen it done with SDL and libraries like it, I'm just not sure what was different.
BT
@BT: No, you cannot have two mains. It doesn't make sense. Why do you think you need two mains?
GMan
@BT: You can *define* both main functions, but only one of them will ever be called.
Travis Gockel
@GMan: I was attempting to make a simple GUI Library which would hide away a good portion of the code from the user during initiation in WinMain and use the main the user provides after everything is initiated. This would just make the code cleaner and reduce confusion (trust me it does). As for two mains that's not what I'm attempting. I'm attempting to have the entry point WinMain (programmed by me) which will then call main (programmed by the library user - most like me).
BT
@Travis G: I know that. I can create as many main definitions as I want, but I need a way to control the entry point to the program. I was depending on the compiler to provide a method for that.
BT
@BT: It does. If you are compiling with `-mwindows`, it selects `WinMain`, otherwise it uses the classic `main` function. If you compile with UTF8, it will select the `main` function with `wchar_t`. But you only get one entry point per compiled binary.
Travis Gockel
@Travis G: It's not working for me. I'm using version 3.4.5. Gonna check to see if they have anything in their error logs.
BT
@BT: Is there a reason you are using such an old version? MinGW is up to version 4.4, although their automated installer is *way* out of date and unmaintained. I'd recommend getting the latest MinGW through Cygwin for less pain than compiling it yourself.
Travis Gockel
@BT: I think your test is faulty. If you link the program as a Windows program, it will not hook up stdout et al; thus printf will never give you anything. However, you can do this manually; see http://www.halcyon.com/~ast/dload/guicon.htm
Luke
@Luke: that's a cool article man, but as far as testing goes just dropping in a message box is by and far easier.
BT
+4  A: 

The compiler will choose one entry point or the other based on whether you're targeting the compiled output to the Windows subsystem or the Console subsystem. WinMain for the former, main for the latter.

sblom
A: 

Just found this work around and kind of feel dumb.

#define main USER_Main

This then takes main out of line for being the programs entry point while still hiding the fact that anything was messed with from the user.

BT