WinApi first lesson...

Hello friends!!
I just started learning WinApi.
In the first lesson i need run this cod:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Hello, Win32 world!", "Hello from Message Box", MB_OK);
return 0;
}


But the compiling field.
the error is:

1>------ Build started: Project: winapi, Configuration: Debug Win32 ------
1>Compiling...
1>first+lesson.cpp
1>c:\users\boris\desktop\c++\winapi\winapi\first+lesson.cpp(6) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [20]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\Users\boris\Desktop\c++\winapi\winapi\Debug\BuildLog.htm"
1>winapi - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


How i can fix it?
Can some one help me?
closed account (1vRz3TCk)
MessageBox(NULL, TEXT("Hello, Win32 world!"), TEXT("Hello from Message Box"), MB_OK); should do the trick.
thank you.
now it is work
closed account (1vRz3TCk)
It is to do with UNICODE and ANSI. A lot of Windows API calls have two versions with an 'general' (macro) version, for example MessageBoxW() and MessageBoxA() has the MessageBox(). Your project is set to use UNICODE so the general version is replaced with a call to MessageBoxW() this required an 'L' in front of the string literal (i.e. L"Hello, Win32 world!"). If you just put the L in front in the code above and later changed your project to non unicode it would fail again because it would require the plain string literal. The TEXT macro basically uses the correct format for the string literal based on how your project is configured.

Edit:
See: http://www.cplusplus.com/articles/2w6AC542/
NB: I have only skimmed it but Disch does seem to have his head screwed on right.
Last edited on
Topic archived. No new replies allowed.