Hello ladies and gents,

I just wanted to see what this small programs does so I tried it in Dev-C++ and in VC++ 2005, thing was, it worked in Dev-C++ but not in VC++ 2005 in which I got an error stating the following:

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [22]' to 'LPCWSTR'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

The program I tried was one which Ancient Dragon gave a link to in another thread:

// WinApi.cpp : Defines the entry point for the console application.

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
    return 0;
}

Could anyone tell me why it works in the one, but not in the other :?:

Thanks.

Recommended Answers

All 19 Replies

LPCWSTR is a wide string type and a string literal is a narrow string type. The two aren't compatible. You can fix it in a number of ways. First, you can make the string wide by prefixing it with L, or you can use the _TEXT macro provided by the Win32 API for selecting the right string width to match your settings. Or you can use MessageBoxA directly instead of using MessageBox as a "smart" entry point.

>Could anyone tell me why it works in the one, but not in the other
MessageBox calls either MessageBoxA or MessageBoxW depending on the need to use 1 or 2 byte character sizes. I'm inclined to think that your Visual Studio setup is conflicting with your needs somewhere while Dev-C++'s setup isn't.

I'm inclined to think that your Visual Studio setup is conflicting with your needs somewhere...

Think that could be it, when installing the SDK, I had to add three directories:

$(ProgramFiles)\Microsoft Platform SDK\lib
$(ProgramFiles)\Microsoft Platform SDK\bin
$(ProgramFiles)\Microsoft Platform SDK\include

The way it was showed on the video, it seemed that these three had to be added to the executable files, but, when I did that, I got an error message saying that <windows.h> could not be found.

When I added the "$(ProgramFiles)\Microsoft Platform SDK\include" directory to the include files, that problem was gone, however, I'm now getting the mentioned error.

Do I have to include all three directories to the executable-include- and library files?

It was showed that I had to add them by pressing tools->options->Projects and Solutions->VC++Directories->there I found the several files, but as said, it seemed as if I had to add the three directories only into the executable file, but this gave me the error that <window.h> couldn't be found???

Visual Studio has enabled Unicode support by default. So even though you have written MessageBox, as Narue said, it calls MessageBoxW because of this setting.
Try setting

Project--->Propoerties-->Configuration Properties-->General-->Character Set

to "Use Multi-Byte Character Set" or "Not Set", and compile. It should work.

The usual way programmers tell the compiler to use the Wide String format or not is by setting the #define UNICODE preprocessor switch.

As for the second problem, there is a drop down box to select the type you are adding the directory. See the attached bitmap.

$(ProgramFiles)\Microsoft Platform SDK\lib ---- To Library Files
$(ProgramFiles)\Microsoft Platform SDK\bin ---- To executable Files
$(ProgramFiles)\Microsoft Platform SDK\include ----- To Include Files

Byte Character Set" or "Not Set", and compile. It should work.

Neither worked WolfPack, I get three errors eachtime:

WinApi.obj : error LNK2019: unresolved external symbol __imp__MessageBoxA@16 referenced in function _WinMain@16
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Visual Studio 2005\Projects\WinApi\Debug\WinApi.exe : fatal error LNK1120: 2 unresolved externals

I did add them into the separate include, lib and executable files and changed the character set to those two options you said, it didn't help.

Beats me, it ought to work. Anyway change the Character set to what it was ( Unicode ) and run this code and tell me what happens.

// WinApi.cpp : Defines the entry point for the console application.

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, L"Goodbye, cruel world!", L"Note", MB_OK);
    return 0;
}

>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?

... and run this code and tell me what happens.

I get the following error message:

WinApi.obj : error LNK2019: unresolved external symbol __imp__MessageBoxW@16 referenced in function _WinMain@16
MSVCRTD.lib(crtexe.obj) : error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
C:\Visual Studio 2005\Projects\WinApi\Debug\WinApi.exe : fatal error LNK1120: 2 unresolved externals

Just the same as before :o

>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?

Duh. Yeah. Seems like it.

[Edit]
Okay I think I know what your problem is. Building Win32 Applications is disabled in the Visual Studio 2005 by default. You can build only Console Programs as it is. To create Win32 Applications look at this article by Brian Johnson.

>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?

Euhm, I'm using Win32 Console Application :o

I'm presuming that isn't right :-|

If that's not correct, which one do I have to use then, and also, how do I create this with an empty proj. ????

Options are:
- Class Library
- CLR Console Application
- CLR Empty Project
- Makefile Project
- Win32 Console Application
- Windows Forms Application
- Empty Project

Euhm, I'm using Win32 Console Application :o

I'm presuming that isn't right :-|

If that's not correct, which one do I have to use then, and also, how do I create this with an empty proj. ????

Options are:
- Class Library
- CLR Console Application
- CLR Empty Project
- Makefile Project
- Win32 Console Application
- Windows Forms Application
- Empty Project

After Selecting Win32 Console Application, you are taken to the Application Wizard. Click Application Settings in it, and you should see 4 options, in which the option "Windows Application" is disabled. It is this option you should choose to build a GUI application. To enable it follow the steps in the link I provided in the previous post.

To enable it follow the steps in the link I provided in the previous post.

Yep, that did the trick WolfPack, I can now compile it without getting any errors.

There's only one problem now, when I want to execute with F5, I get a message that says the following:

This exe file can't be run because the MSVCR80D.dll couldn't be found. Reinstalling this will solve the problem.

Found it on my computer:
C:\WINDOWS\WinSxS\x86_Microsoft.VC80.DebugCRT_1fc8b3b9a1e18e3b_8.0.50727.42_x-ww_f75eb16c

Question is, where do I have to put it????
Thanks for all the assistance allready :!:

I have had the same question about how to disable UNICODE settings -- thanks WolfPack for the help. But even after that, I also get the unresolved external MessageBox error. Can't it be used in console applications?? I use it in console apps with VC++ 6.0 compiler without any problems. Maybe we just can't use in console apps with that free Express edition compiler. Otherwise, the other win32 api functions I use (FindFirstFile(), FindNextFile() etc) compile and link ok.

Exactly the same problems I went through while I installed Visual Studio 2005 for the first time. Try setting Embed Manifest to No. See attachment.

I have had the same question about how to disable UNICODE settings -- thanks WolfPack for the help. But even after that, I also get the unresolved external MessageBox error. Can't it be used in console applications?? I use it in console apps with VC++ 6.0 compiler without any problems. Maybe we just can't use in console apps with that free Express edition compiler. Otherwise, the other win32 api functions I use (FindFirstFile(), FindNextFile() etc) compile and link ok.

I find no problem in doing it. Have you done the modifications I told JoBe in a previous reply ( Reply Number 10 in this thread )? Can you make GUI Applications in Visual Studio 2005?

Exactly the same problems I went through while I installed Visual Studio 2005 for the first time. Try setting Embed Manifest to No. See attachment.

Pffffffff, man oh man, finally, it's working, Narue and especially WolfPack, thanks for your patience, really appreciated :!: :!:

Can I ask WolfPack, do you have a manual from Visual C++ 2005? If not, how do you know these solutions, or is it experience with VC++ 2005?

I find no problem in doing it. Have you done the modifications I told JoBe in a previous reply ( Reply Number 10 in this thread )? Can you make GUI Applications in Visual Studio 2005?

I don't have a problem described in reply #10, but the one in previous reply #9. And yes, I did follow the steps in that M$ link about how to set up the compiler for windows applications -- that's not my problem. But thanks anyway.

Pffffffff, man oh man, finally, it's working, Narue and especially WolfPack, thanks for your patience, really appreciated :!: :!:

No big deal. Narue made a big breakthrough noticing the _main part in the linker errors. I only saw the first linker error with _Winmain@16.

Can I ask WolfPack, do you have a manual from Visual C++ 2005? If not, how do you know these solutions, or is it experience with VC++ 2005?

Pure Experience. I have faced all of your problems some time ago. So it's all coming back to me now.

I don't have a problem described in reply #10, but the one in previous reply #9. And yes, I did follow the steps in that M$ link about how to set up the compiler for windows applications -- that's not my problem. But thanks anyway.

I was just checking your settings just in case. I have done only those modifications and I can run this code without any problem.

#include <windows.h>

int main()
{
    MessageBox(NULL, L"Goodbye, cruel world!", L"Note", MB_OK);
    return 0;
}

Therefore a solution doesnot come to me right now. Sorry about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.