0

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.

Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
BT.
  • 229
  • 1
  • 9
  • 3
    Your use of "before" makes me believe you think one main will be called, then the other. But there is only one main. (It doesn't make sense to have two main's.) – GManNickG Apr 12 '10 at 05:33
  • sorry, poor language. I'm not planning on using them at one time (that kind of implies a certain view on my intelligence level which I'm not to fond of, oh well). I was going to inject the main into the WinMain later. – BT. Apr 12 '10 at 06:01

3 Answers3

5

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
  • 26,877
  • 14
  • 89
  • 116
  • Sorry that didn't work. I changed the code to printf("main\n"); MessageBox (NULL, TEXT ("main"), TEXT ("main"), 0) ; return 0; 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. Apr 12 '10 at 05:40
  • @BT: No, you cannot have two mains. It doesn't make sense. Why do you think you need two mains? – GManNickG Apr 12 '10 at 05:43
  • 3
    @BT: You can *define* both main functions, but only one of them will ever be called. – Travis Gockel Apr 12 '10 at 05:54
  • @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. Apr 12 '10 at 05:54
  • @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. Apr 12 '10 at 05:57
  • 1
    @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 Apr 12 '10 at 06:03
  • @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. Apr 12 '10 at 06:12
  • @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 Apr 12 '10 at 06:20
  • @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 Apr 12 '10 at 14:47
  • @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. Apr 12 '10 at 20:44
4

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
  • 26,911
  • 4
  • 71
  • 95
1

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.
  • 229
  • 1
  • 9