Complete beginner to win32 programming

Mar 29, 2010 at 12:18am
1
2
3
4
5
6
7
8
9
10
11
12
13
#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)

{
	MessageBox(NULL, "This program", "Important windows message", MB_OK);
	
	return(0);
}


it's so simple...

It compiles ok if I exclude the messagebox function, but with it, it doesn't pass the compiler, and I get this error message

1>c:\main2\programming\lean\demo.cpp(9) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


the book said that the function is expecting a parameter, two strings, and a parameter to change the window's buttons. I don't see what I doing wrong, but then again, this book is from 1999, and I'm running MVC++ 2008 on vista 64 bit, so it could be out of date.
Mar 29, 2010 at 12:27am
Change it to MessageBoxA, not MessageBox. Unicode is supported by default, which causes it to be replaced with MessageBoxW, which requests a string using wide characters. So, you can either replace it with MessageBoxA, or you can put an L to the left of each of your strings:

A: MessageBoxA(NULL, "This program", "Important windows message", MB_OK);
or B: MessageBox(NULL, "This program"L, "Important windows message"L, MB_OK);
Mar 29, 2010 at 12:35am
Unicode?

You're the first person to tell me that, but I've seen things all over the internet telling me to just use "MessageBox" without 'L's, and had the same problem every time. The MessageBoxA trick worked perfectly.

Is this a MVC++ only feature, or is this just something that's been accepted recently?

what's a wide character, by the way? I thought a character was just one symbol, like 'a' or '1'.


Does anyone know of any good books to learn windows programming? I only have a gaming book, and I think I'd like to do more than that.

Last edited on Mar 29, 2010 at 12:36am
Mar 29, 2010 at 1:21am
Unicode is a character set the encompasses more than just basic latin characters. It's important for internationalization.

IMO, you should always try to support Unicode unless it's impractical (in widgetry programs, it's almost never impractical since it's so widely supported -- so try to do it)

Here's an article I whipped up a while back regarding Unicode in Win32. It explains what the deal with the 'L' is, what TCHARs are, what LPSTRs are, what the deal with MessageBoxA is., and other stuff you should know that most tutorials don't cover (and/or are oblivious to):

http://cplusplus.com/forum/articles/16820/
Last edited on Mar 29, 2010 at 1:22am
Mar 29, 2010 at 2:36pm
Unicode?


You can switch that off in your projects settings under "General". The option is "Character Set" and you can switch that to "Not Set".

can put an L to the left of each of your strings:
...
or B: MessageBox(NULL, "This program"L, "Important windows message"L, MB_OK);


o.O

Left is where the thumb is right, dude. ;-)

Ciao, Imi.
Last edited on Mar 29, 2010 at 2:36pm
Mar 29, 2010 at 3:53pm
It's also possible to write code that behaves well regardless of whether UNICODE is defined.
 
MessageBox(NULL, TEXT("This program"), TEXT("Important windows message"), MB_OK);
Mar 29, 2010 at 3:56pm
closed account (z05DSL3A)
+1 helios
Mar 30, 2010 at 1:09am
Does anyone know of a good site that has documentation on Windows programming?

I wish cpp.com had something, most of the things I've read online are a nightmare to follow, and I learned a lot from this site about console C++

Just wondering, why don't you guys have windows C++ documentation? Just never got around to it?
Mar 30, 2010 at 1:12am
I think msdn has pretty good documentation on the win32 api

http://msdn.microsoft.com/en-us/library/aa383749(VS.85).aspx
Last edited on Mar 30, 2010 at 1:15am
Mar 30, 2010 at 3:41am
Just wondering, why don't you guys have windows C++ documentation? Just never got around to it?
Because platform specifics are beyond the scope of a C++ reference, what with them, you know, not being part of the language and all.
Besides, the MSDN already is an extensive reference of the Windows API and its cousins.
Mar 31, 2010 at 1:28am
I see, that makes sense. Thanks!
Topic archived. No new replies allowed.