• Advertisement
Sign in to follow this  

Errors

This topic is 4041 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using some code from Direct Tutorial on Visual C++.
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    // create a "Hello World" message box using MessageBox()
    MessageBox(NULL,
               L"Hello World!",
               L"Just another Hello World program!",
               MB_ICONEXCLAMATION | MB_OK);

    // return 0 to Windows
    return 0;
}
My error is:
1>------ Build started: Project: new, Configuration: Debug Win32 ------
1>Linking...
1>tutorial.obj : error LNK2028: unresolved token (0A000029) "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)
1>tutorial.obj : error LNK2019: unresolved external symbol "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)
1>C:\Documents and Settings\Ryan\My Documents\Visual Studio 2005\Projects\new\Debug\new.exe : fatal error LNK1120: 2 unresolved externals
1>Build log was saved at "file://c:\Documents and Settings\Ryan\My Documents\Visual Studio 2005\Projects\new\new\Debug\BuildLog.htm"
1>new - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Any Ideas what this means?

Share this post


Link to post
Share on other sites
Advertisement
Hi...

I tried to run your code in Visual C++ 6.0 and it worked just great except for the L before the strings. He just refused to compile it in that way. So, the only way I could get it to work was:


#include <windows.h> // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// create a "Hello World" message box using MessageBox()
MessageBox(NULL,
"Hello World!",
"Just another Hello World program!",
MB_ICONEXCLAMATION | MB_OK);

// return 0 to Windows
return 0;
}




EDIT: Noticed you are using Visual 2005. Is it the express edition? If it is, did you installed Platform SDK? I used to have errors like those while I didn't install it.

Share this post


Link to post
Share on other sites
He probably didn't select a UNICODE build. The L prepend tells the compiler to deal with the string literal as a UNICODE string rather than an ANSI string. A more flexible approach is to wrap string literals with the TEXT macro. That macro prepends the L as needed depending on whether the UNICODE macro is defined or not.

I didn't think that was the problem though because those error messages indicate that a UNICODE build was intended. For example the "wchar_t const *" in the function prototypes.

Share this post


Link to post
Share on other sites
Ahh i figured out what Sagito meant and it worked but changed the error message.

new error message:
1>------ Build started: Project: new, Configuration: Debug Win32 ------
1>Compiling...
1>tutorial.cpp
1>.\tutorial.cpp(8) : error C2731: 'WinMain' : function cannot be overloaded
1> .\tutorial.cpp(4) : see declaration of 'WinMain'
1>.\tutorial.cpp(13) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'const char [13]' to 'LPCTSTR'
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:\Documents and Settings\Ryan\My Documents\Visual Studio 2005\Projects\new\new\Debug\BuildLog.htm"
1>new - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
Heres the new code and error messages:
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// create a "Hello World" message box using MessageBox()
::MessageBox(NULL,
"Hello World!",
"Just another Hello World program!",
MB_ICONEXCLAMATION | MB_OK);

// return 0 to Windows
return 0;
}



1>------ Build started: Project: new, Configuration: Debug Win32 ------
1>Compiling...
1>tutorial.cpp
1>.\tutorial.cpp(13) : error C2664: 'MessageBox' : cannot convert parameter 2 from 'const char [13]' to 'LPCTSTR'
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:\Documents and Settings\Ryan\My Documents\Visual Studio 2005\Projects\new\new\Debug\BuildLog.htm"
1>new - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Share this post


Link to post
Share on other sites
1) Your new code doesn't have the first error that the last one had, so I won't deal with that again.

2) It DOES have the same error as the last one and the link I posted tells you how to fix it. Here is what you would have read if you actually clicked that link:
Quote:
Question

I'm trying to compile a piece of code such as:

MessageBox("Hello world!");

... when I compile the project, the compiler yields:

error C2664: 'CWnd::MessageBoxW' : cannot convert parameter 1 from 'const char [12]' to 'LPCTSTR'

What am I doing wrong?

Problem

This error message means that you are trying to pass a multi-byte string (const char [12]) to a function which expects a unicode string (LPCTSTR). The LPCTSTR type extends to const TCHAR*, where TCHAR is char when you compile for multi-byte and wchar_t for unicode. Since the compiler doesn't accept the char array, we can safely assume that the actual type of TCHAR, in this compilation, is wchar_t.

Resolution

You will have to do one of two things:

1. Change your project configuration to use multibyte strings. Press ALT+F7 to open the properties, and navigate to Configuration Properties > General. Switch Character Set to "Use Multi-Byte Character Set".
2. Indicate that the string literal, in this case "Hello world!" is of a specific encoding. This can be done through either prefixing it with L, such as L"Hello world!", or surrounding it with the generic _T("Hello world!") macro. The latter will expand to the L prefix if you are compiling for unicode (see #1), and nothing (indicating multi-byte) otherwise.

Variations

Another error message, indicating the same problem, would be:

cannot convert parameter 1 from 'const char [12]' to 'LPCWSTR'

Where LPCWSTR maps to a wchar_t pointer, regardless of your build configuration. This problem can be resolved primarily by using solution #2, but in some cases also #1. A lot of the Microsoft provided libraries, such as the Platform SDK, have got two variations of each function which takes strings as parameters. In case of a unicode build, the actual functions are postfixed W, such as the MessageBoxW seen above. In case of multi-byte, the function would be MessageBoxA (ASCII). Which of these functions is actually used when you compile your application, depends on the setting described in resolution #1 above.

Share this post


Link to post
Share on other sites
Alright, I changed my Character set to Multi_Byte and changed my code to the following...
#include <windows.h>    // include the basic windows header file

// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nShowCmd)
{
// create a "Hello World" message box using MessageBox()
MessageBox(NULL,
_T("Hello World!"),
_T("Just another Hello World program!"),
MB_ICONEXCLAMATION | MB_OK);

// return 0 to Windows
return 0;
}


Now I get this error...
1>.\tutorial.cpp(11) : error C3861: '_T': identifier not found
1>.\tutorial.cpp(12) : error C3861: '_T': identifier not found
1>Build log was saved at "file://c:\Documents and Settings\Ryan\My Documents\Visual Studio 2005\Projects\new\new\Debug\BuildLog.htm"
1>new - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Seems like a never ending string of errors :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement