MessageBox() in Visual C++?

This is a discussion on MessageBox() in Visual C++? within the Windows Programming forums, part of the Platform Specific Boards category; Hello. I recently started to learn Win32 programming through web tutorials , and since I changed from Dev-C++ to Visual ...

  1. #1
    Registered User
    Join Date
    Dec 2005
    Posts
    43

    MessageBox() in Visual C++?

    Hello.

    I recently started to learn Win32 programming through web tutorials, and since I changed from Dev-C++ to Visual Studio 2005, my code doesn't work anymore.

    This code compiles and runs fine in Dev-C++, but somehow it doesn't in Visual C++.

    Code:
    #include <windows.h>
    
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
    {
    	MessageBox ( NULL, "Hello World!", "Test", MB_OK );
    	return 0;
    }
    Here's the error message:

    Code:
    Error	1	error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [13]' to 'LPCWSTR'  5
    I tried other sources, including MSDN, but they all use the same implementation of MessageBox().

    Thanks.

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Is UNICODE defined?

    Try MessageBoxA().

  3. #3
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Ah thanks. MessageBoxA() works fine.

    How do I enable UNICODE though?

  4. #4
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Put
    Code:
    #define UNICODE
    before you include windows.

  5. #5
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342

  6. #6
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <windows.h>
    
    int WINAPI WinMain ( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd )
    {
    	MessageBox ( NULL, TEXT("Hello World!"), TEXT("Test"), MB_OK );
    	return 0;
    }
    Will work whether unicode is enabled or not. You can use the L prefix for string literals to specify wide chars (which is what the TEXT macro does if unicode is enabled), for example:
    Code:
    wchar_t str[] = L"Hello World";
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  7. #7
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Yeah ... but that tends to get old every time you want to use text in API functions.

  8. #8
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    _T() isn't usually too bad in terms of typing/readability:

    E.g. MessageBox(NULL, _T("Hello"), _T("Test"), MB_OK);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  9. #9
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    You can prefix strings with L as well which is a bit easier.

    Code:
    MessageBox(NULL,L"MyText",L"MyTitle",MB_OK);

  10. #10
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Only problem with prefixing L is that you can then only compile in Unicode.

    _T("string") or TEXT("string") are macros which become L"string" if UNICODE is defined, and "string" if UNICODE is not defined.

    Of course everyone should use Unicode wherever possible.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  11. #11
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Well I always use UNICODE but thanks for the clarification. BTW are you Cat from www.youngcoders.com?

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Nope, must be a different Cat. Originally used to use the handle Catskinner (I was a Wing Commander addict and that was my callsign there; if you played the game you'd understand). Got sick of trying to explain that/listening to the comments people would make -- and it certainly got me some odd looks at parties as friends used to call me that IRL -- so I shortened it
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  13. #13
    Registered User
    Join Date
    Dec 2005
    Posts
    43
    Defining UNICODE doesn't work... All it gives me is a warning:

    Code:
    Warning	1	warning C4005: 'UNICODE' : macro redefinition	c:\documents and settings\user1.cs.000\desktop\win32\win32\win32.cpp	1
    I also tried the alternatives and they all give me the same errors:

    Code:
    Error	1	error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup	MSVCRTD.lib	
    Error	2	fatal error LNK1120: 1 unresolved externals	C:\Documents and Settings\user1.CS.000\Desktop\win32\Debug\win32.exe

  14. #14
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    For msvc2005, UNICODE (and _UNICODE) are #defined by default - you don't need to define them. For other compilers this is not the case and you should, ideally, define them as a compiler setting/switch rather than as a set of bare #defines in your source code. In dev-cpp to set such preprocessor definitions, open up the project options dialog from the project menu, select the parameters tab and enter the defines in the c++ compiler field as
    Code:
    -DUNICODE
    -D_UNICODE
    Note that it is imperative that you define both.

    As Cat and others have already described, use the _T or TEXT macro to ensure that all string literals are properly converted to their wide(wchar_t) or narrow(char) types depending on whether you have defined the two macros as previously described; this enables you to write code once and build it for deployment on unicode(winnt, win2k,winxp etc) and non-unicode(win9x ie win95/98/me) windows systems. If you need to declare string types use TCHAR which will resolve to wchar_t if UNICODE is defined and char if not. If you are using c++ then you may prefer to use std::string and std::wstring; typically you might create an alias for wide and narrow strings as follows:
    Code:
    typedef std::basic_string<TCHAR> unicodestring;
    which will effectively resolve to std::wstring if unicode is defined, std::string if not.

    Note that the definition of UNICODE will also ensure that the proper wide(W, eg MessageBoxW) or narrow(A, eg MessageBoxA) winapi functions are invoked.

    If you know you are only going to be targetting, for example, winxp or later then you can and arguably should just use wide character strings, using the 'L' prefix as others have already described and defining both UNICODE and _UNICODE for your project.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 09:43 AM
  4. Errors with including winsock 2 lib
    By gamingdl'er in forum C++ Programming
    Replies: 3
    Last Post: 12-05-2005, 07:13 PM
  5. Learning OpenGL
    By HQSneaker in forum C++ Programming
    Replies: 7
    Last Post: 08-06-2004, 08:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21