Why program works in Dev-C++ and not in VC++ 2005?
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 :?:
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.
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 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 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.
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.
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
>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.
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.
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 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?
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.