Why program works in Dev-C++ and not in VC++ 2005?

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #1
Mar 4th, 2006
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:

  1. // WinApi.cpp : Defines the entry point for the console application.
  2.  
  3. #include <windows.h>
  4.  
  5. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  6. LPSTR lpCmdLine, int nCmdShow)
  7. {
  8. MessageBox(NULL, "Goodbye, cruel world!", "Note", MB_OK);
  9. return 0;
  10. }

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

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #2
Mar 4th, 2006
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 here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #3
Mar 4th, 2006
Originally Posted by Narue
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???
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #4
Mar 4th, 2006
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
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #5
Mar 4th, 2006
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
Attached Thumbnails
vcprop.bmp  
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #6
Mar 4th, 2006
Originally Posted by WolfPack
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #7
Mar 4th, 2006
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.
  1. // WinApi.cpp : Defines the entry point for the console application.
  2.  
  3. #include <windows.h>
  4.  
  5. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  6. LPSTR lpCmdLine, int nCmdShow)
  7. {
  8. MessageBox(NULL, L"Goodbye, cruel world!", L"Note", MB_OK);
  9. return 0;
  10. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,580
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 709
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #8
Mar 4th, 2006
>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 421
Reputation: JoBe is on a distinguished road 
Solved Threads: 4
JoBe's Avatar
JoBe JoBe is offline Offline
Posting Pro in Training

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #9
Mar 4th, 2006
Originally Posted by WolfPack
... 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
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Why program works in Dev-C++ and not in VC++ 2005?

 
0
  #10
Mar 4th, 2006
Originally Posted by Narue
>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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC