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.

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>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 on other sites
Try prepending "::" to MessageBox and see what happens. That is, try ::MessageBox.

Share on other sites
Nope same errors...
Thanks for trying

Share on other sites
Have you double checked the project properties to see if Windows is the chosen subsystem?

Share on other sites
How exactly do I check that? Right now it says Win32 is the platform I dunno if thats it of what.

Share on other sites
Sorry same errors...

Share on other sites
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 on other sites
Yes I installed the Platform SDK and I did everything on the website. What exactly did you change in the code?

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 on other sites
I looked through the project settings and a Unicode build was intended. How do I use text Macros?

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 on other sites
Does any one know what this error means?

Share on other sites
2nd Error

I can't tell for sure what's going on with the first error without seeing your code. So... post your new updated code with all the changes you made.

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 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:
 QuestionI'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?ProblemThis 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.ResolutionYou 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.VariationsAnother 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 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>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 on other sites
Does anyone know my problem?