944,089 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 17994
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 4th, 2006
0

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

Expand Post »
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:

Quote ...
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:

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 4th, 2006
0

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

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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 4th, 2006
0

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

Quote 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???
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 4th, 2006
0

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

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
C++ Syntax (Toggle Plain Text)
  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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 4th, 2006
0

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

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
Click image for larger version

Name:	vcprop.bmp
Views:	132
Size:	707.6 KB
ID:	1726  
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 4th, 2006
0

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

Quote originally posted by WolfPack ...
Byte Character Set" or "Not Set", and compile. It should work.
Neither worked WolfPack, I get three errors eachtime:
Quote ...
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.
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 4th, 2006
0

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

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.
C++ Syntax (Toggle Plain Text)
  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. }
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005
Mar 4th, 2006
0

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

>error LNK2019: unresolved external symbol _main referenced in function
You're using a console application?
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 4th, 2006
0

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

Quote originally posted by WolfPack ...
... and run this code and tell me what happens.
I get the following error message:
Quote ...
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
Reputation Points: 51
Solved Threads: 4
Posting Pro in Training
JoBe is offline Offline
420 posts
since Sep 2004
Mar 4th, 2006
0

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

Quote 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.
Moderator
Reputation Points: 572
Solved Threads: 115
Mentally Challenged Mod.
WolfPack is offline Offline
1,559 posts
since Jun 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: (Noob) need some help w/ Prime Numbers
Next Thread in C++ Forum Timeline: Avoid PROGRAM CRASH





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC