Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » [WIN32] 'WinMain' : function cannot be overloaded

This thread is locked; no one can reply to it. rss feed Print
[WIN32] 'WinMain' : function cannot be overloaded
Steve++
Member #1,816
January 2002

Using Visual C++ Express, I created an empty win32 project (not console) and created the following as main.cpp:

#include <windows.h>

int main()
{
  return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
  return main();
}

Compiler said:

main.cpp(9) : error C2731: 'WinMain' : function cannot be overloaded
main.cpp(8) : see declaration of 'WinMain'

Google didn't help in this case. Needless to say, neither did MSDN. This error occurs with both 2005 and 2008 versions.

Roy Underthump
Member #10,398
November 2008
avatar

Get rid of the main() function. That's for console, WinMain() is for gui app.

I love America -- I love the rights we used to have
George Carlin

Steve++
Member #1,816
January 2002

Roy, I'll dumb it down for you. The following code also produces a C2731:

#include <windows.h>

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
  return 0;
}

Roy Underthump
Member #10,398
November 2008
avatar

When I tried the IDE in the Express versions, I couldn't create a gui app at all. I thought it was to avoid cutting into the sales of the $$$ versions. I just use makefiles.

I love America -- I love the rights we used to have
George Carlin

Steve++
Member #1,816
January 2002

It's not that hard to make a GUI app in Win32. Even easier with .NET WinForms.

I found the problem... I was using LPTSTR instead of LPSTR.

The reason I have a main() method called by WinMain is because I'm making a "magic main" system similar to that of Allegro, except simplified for my purposes. I'm actually using it in Ogre and Irrlicht, both of which I'm currently evaluating.

axilmar
Member #1,204
April 2001

The only reason Microsoft did not use the regular main as the entry symbol of a C application is marketing: they feared that their code will be easily portable to other platforms.

This generated hundreds of 'magic main' implementations and lots of boilerplate code to set things straight.

It was one of the dumbest things ever to be done by a software company.

Steve++
Member #1,816
January 2002

Hey, profit isn't dumb!

Roy Underthump
Member #10,398
November 2008
avatar

Remember IBM's PS/2 and the proprietary bus interfaces? That was an attempt to lock in customers also.

I love America -- I love the rights we used to have
George Carlin

torhu
Member #2,727
September 2002
avatar

You don't need winmain. You can just set SubSystem to Windows in msvc. Then you have to set the entry point to mainCRTStartup. Both are in the linker setting in project properties.

Neil Walker
Member #210
April 2000
avatar

Quote:

I found the problem... I was using LPTSTR instead of LPSTR.

When I create a windows app I always use LPTSTR and not LPSTR. Mind you, I always let MSVC set up my startup code...

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Steve++ said:

I found the problem... I was using LPTSTR instead of LPSTR.

Neil Walker said:

When I create a windows app I always use LPTSTR and not LPSTR. Mind you, I always let MSVC set up my startup code...

I happened across this from a thread on the cplusplus.com forums :

guestgulkan's reply said:

A bit more about windows string pointers.
I have counted 11 different type of string pointers in windows as follows;

...

LPTSTR //LPWSTR if UNICODE defined, LPSTR if UNICODE not defined

I don't know if that's accurate or not, but maybe it has something to do with it.

Neil Walker
Member #210
April 2000
avatar

Thank god .net has a common type system ;)

Neil.
MAME Cabinet Blog / AXL LIBRARY (a games framework) / AXL Documentation and Tutorial

wii:0356-1384-6687-2022, kart:3308-4806-6002. XBOX:chucklepie

Tobias Dammers
Member #2,604
August 2002
avatar

Thank god for .NET anyway, if only so we don't have to deal with winapi calls anymore.

---
Me make music: Triofobie
---
"We need Tobias and his awesome trombone, too." - Johan Halmén

Don Freeman
Member #5,110
October 2004
avatar

There is a way to get Visual Studio Express to create Win32 Gui apps. Google it, or use the 2008 version that has it enabled by default.:P

--
"Everyone tells me I should forget about you, you don’t deserve me. They’re right, you don’t deserve me, but I deserve you."
"It’s so simple to be wise. Just think of something stupid to say and then don’t say it."

Go to: