Can't Even Get Hello World To Work
I am new to Win32 programming and I found a tutorial online that gave code for a Hello World program. I have Visual C++ 2005 Express Edition and after many hours of frustrating work I was able to get the Platform SDK installed and working (I tried a sample program and it worked).
Here is the code the site gave for the Hello World program:
#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow){
MessageBox( NULL , "Hello World!", "Win32 Tutorial" , MB_OK );
return 0;
}
I get this error when I attempt to build:
Build started: Project: testhw, Configuration: Debug Win32
Compiling...
hw.cpp
c:\documents and settings\owner\my documents\visual studio 2005\projects\testhw\testhw\hw.cpp(5) : 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
Build log was saved at "file://c:\Documents and Settings\Owner\My Documents\Visual Studio 2005\Projects\testhw\testhw\Debug\BuildLog.htm"
testhw - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
If anyone could help me with this I would greatly appreciate it.
Hi,
Try either way.
#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow){
// MessageBox( NULL , (LPCWSTR)"Hello World!", (LPCWSTR)"Win32 Tutorial" , MB_OK );
// MessageBox( NULL , L"Hello World!", L"Win32 Tutorial" , MB_OK );
MessageBox( NULL , TEXT("Hello World!"), TEXT("Win32 Tutorial") , MB_OK );
return 0;
}
Hope this helps.
Hi,
purplenite wrote: |
The second two of the three lines work great (the first one still gives me Chinese). |
|
This is my fault! Very sorry for it.
Just for your reference;
#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance,HINSTANCE hPrevInstance ,LPSTR lpCmdLine ,int nCmdShow){
// LPCWSTR hw = L"Hello World!";
// LPCWSTR cp = L"Win32 Tutorial";
wchar_t* hw = TEXT("Hello World!");
wchar_t* cp = TEXT("Win32 Tutorial");
MessageBox( NULL , hw, cp, MB_OK );
return 0;
}
Thanks Takashi I found this useful too. I also was trying to get similar code to work with a function-style cast with the LPCTSTR type.
My question is - how would you work out that you needed to typecast with TEXT, if you didn't happen to know already?
Also what does LP mean in LPCTSTR, LPCWSTR, etc?
Thanks!
scdlewis wrote: |
| My question is - how would you work out that you needed to typecast with TEXT, if you didn't happen to know already? Also what does LP mean in LPCTSTR, LPCWSTR, etc? Thanks! |
|
LP is short for Long Pointer. This is a hang-over from older-model versions of Windows (e.g., 16-bit Windows) where pointer size varied depending on the execution and addressing model being used in the application. These symbols are automatically defined correctly for the machine being compiled for when you use windows.h in implementation of your program.
The problem of type mismatches is related to the default use of Unicode in some Visual C++ projects. There is more on the incompatible types situation on http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=432748&SiteID=1 The mystery that you encounter is that MessageBox is defined by a Macro (hence the name with the W in it that you may see in the error message) based on certain settings for compiler options. That invisible-to-you feature results in an incompatibility between the pointer made from "your text of any kind in double-quotes" and the kind of string being expected by the version of MessageBox being supplied.
- Dennis
PS: I don't know how Takashi worked it out, but my experience is that it is a matter of experience and cues from such things as the types named in the error messages, the functions named in messages, and understanding the pointer types that result from using character arrays, etc. The TEXT macro is a special case having to know how VC++ handles writing a single program that can be compiled for either single-byte, double-byte, and wide characters. There is more about that in the MSDN and Help materials, although it may take a little more experimentation and practice before it makes much sense.