Problem with creating an window

Sep 22, 2012 at 6:51pm
closed account (ozUkoG1T)
hey c++ i have copied and pasted an code from MSDN,yet the code does not work.
how is it me or Microsoft i doubt it is them but any way :
1
2
3
4
5
6
#include<Windows.h>
int  APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine, int       nCmdShow)
{
MessageBox(NULL, L"MessageBox Text", L"MessageBox caption", MB_OK);
     return FALSE;
}

what is the error please...
p.s
If you know an easier way to make an simple window please post it thanks
Sep 22, 2012 at 7:57pm
Just wondering, did you put that code into a Win32 project? Or Win32 Console Application?
Sep 22, 2012 at 9:04pm
closed account (ozUkoG1T)
win32 project
Sep 22, 2012 at 10:37pm
Just get rid of the '_t' in behind WinMain, it worked fine for me once I did that
Sep 23, 2012 at 3:56am
Or include tchar.h :)
Sep 23, 2012 at 9:48am
closed account (ozUkoG1T)
hey all but when i did both i still got an error "WinMain" function cannot be overloaded

please help!
Sep 23, 2012 at 11:22am
It shouldn't be saying it overloaded with that code, and including tchar.h like modoran said it still works fine.

Which compiler are you using? And post the source and error message too.
Sep 23, 2012 at 11:56am
closed account (ozUkoG1T)
i am using visual studio 2010 ultimate the error code is :

source
1
2
3
4
5
6
#include<Windows.h>
int  APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine, int       nCmdShow)
{
MessageBox(NULL, L"MessageBox Text", L"MessageBox caption", MB_OK);
     return TRUE;
}
Error	1	error C2731: 'WinMain' : function cannot be overloaded	c:\users\Cyberwarfare\desktop\c programs & project\networking\networking\server.cpp	      3  	1	Networking
Sep 23, 2012 at 4:42pm
Alright, where it saids LPTSTR, I changed that to LPSTR and it worked fine for me.

If you used _tWinMain and include tchar.h it will work too.
Sep 23, 2012 at 5:07pm
closed account (ozUkoG1T)
still no progress not an single success run
1
2
3
4
5
6
7
#include<Windows.h>
#include<tchar.h>
int  APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine, int       nCmdShow)
{
MessageBox(NULL, L"MessageBox Text",L"MessageBox caption", MB_OK);
     return TRUE;
}

please write the correct code for me to actually run please
Error	1	error C2731: 'wWinMain' : function cannot be overloaded	c:\users\****\desktop\c programs & project\networking\networking\server.cpp	4	1	Networking
Last edited on Jan 9, 2021 at 2:55pm by admin
Sep 23, 2012 at 5:15pm
1) The entry point is WinMain, not _tWinMain

2) WinMain takes LPSTR, not LPTSTR

3) No need to #include <tchar.h>

1
2
3
4
5
6
7
8
9
#include <Windows.h>

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
{
   //...

   return 0;  // <- returning 0 indicates success, just like with the normal main().
    // return TRUE is the same as return 1, which indicates failure.
}
Last edited on Sep 23, 2012 at 5:16pm
Sep 23, 2012 at 5:29pm
Yeah, looking at the code I said one of the other, you done both!

It can either be like

1
2
3
4
5
6
7
#include <Windows.h>

int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR    lpCmdLine, int       nCmdShow)
{
	MessageBox(NULL, L"MessageBox Text", L"MessageBox caption", MB_OK);
        return 0;
}


Or

1
2
3
4
5
6
7
8
#include <Windows.h>
#include <tchar.h>

int APIENTRY _tWinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine, int       nCmdShow)
{
	MessageBox(NULL, L"MessageBox Text", L"MessageBox caption", MB_OK);
        return 0;
}


And what Disch said, returning 1 or TRUE is failure, as TRUE is just a macro for 1.
Last edited on Sep 23, 2012 at 5:30pm
Sep 23, 2012 at 5:31pm
closed account (ozUkoG1T)
hey none of them seem to work what IDE or compiler you using please.
Sep 23, 2012 at 5:50pm
I'm using Microsoft Visual Studios 2010 Ultimate, the same as you. Just wondering did you crack your version? Because that might be why it not working but it should be fine.

Did Disch's code also not work?
Sep 23, 2012 at 5:59pm
If you are using MinGW try...
1
2
3
4
5
6
7
#include <Windows.h>
#include <tchar.h>
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPTSTR    lpCmdLine, int       nCmdShow)
{
	MessageBoxW(NULL, L"MessageBox Text", L"MessageBox caption", MB_OK);
	return 0;
}
Last edited on Sep 23, 2012 at 6:00pm
Sep 23, 2012 at 6:08pm
closed account (ozUkoG1T)
Hey all again it does not work i did not crack it i have the Open gl lib and everything which came with it.again you're code or naraku or Disch code did not work at all.
Sep 23, 2012 at 6:12pm
closed account (ozUkoG1T)
anyone know but i copied some window code about an week ago and that one works but do not know why any of these dont work
Sep 23, 2012 at 6:14pm
Then I'm really not sure, OpenGL got nothing to do with the code so it won't be anything to do with that.

Just try making a new Win32 Project and make sure it empty then try the code again.

Or try that code below.

1
2
3
4
5
6
7
#include <Windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	MessageBox(NULL, L"Text", L"Caption", MB_OK);
	return 0;
}
Sep 23, 2012 at 6:24pm
closed account (ozUkoG1T)
Yes finnaly it is working thanks Callum !!!!
Sep 23, 2012 at 9:44pm
I feel I must interject with a technical correction.

MessageBox(NULL, L"Text", L"Caption", MB_OK);

This is wrong. MessageBox takes TCHAR strings, you are giving it WCHAR strings.

1
2
3
MessageBoxA(NULL, "Text", "Caption", MB_OK);  // <- Correct.  ANSI ver of the function with char strings
MessageBoxW(NULL, L"Text", L"Caption", MB_OK);  // <- Correct.  Wide ver of the function with wide strings
MessageBox(NULL, TEXT("Text"), TEXT("Caption"), MB_OK); // <- Correct. TCHAR ver of the function with TCHAR strings 


Whether or not passing wide strings to a TCHAR version of the function will work depends on whether or not UNICODE is defined (depends on your compiler settings or whether you are manually #defining it).

Best practice: pick one and run with it. Don't mix and match.

I personally try to use the 'W' version of all WinAPI functions whenever practical.
Last edited on Sep 23, 2012 at 9:45pm
Topic archived. No new replies allowed.