Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,613 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 950 people online right now. Registration is fast and FREE... Join Now!




microsoft visual c++ - win32 programming

2 Pages V  1 2 >  
Reply to this topicStart new topic

microsoft visual c++ - win32 programming

The Architect 2.0
post 9 Jul, 2008 - 10:53 PM
Post #1


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

cpp

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{

MessageBox(NULL, "Hello World!", "Hello World", MB_OK);

return 0;
}


basically, that code, copy-pasted from my win32 tutorial doesn't work.

i get this error message:

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

Go to the top of the page

no2pencil
post 9 Jul, 2008 - 11:00 PM
Post #2


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


I believe (& this is going back a ways) that what I've done in the past to force this to work is create a char array & pass that in the argument, rather than the string/text. I think... it's been a while since I've done Windows coding.
User is online!Profile CardPM

Go to the top of the page

born2c0de
post 9 Jul, 2008 - 11:37 PM
Post #3


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


When you simply copy-paste code and execute it in VC++ 6.0 IDE, it creates a default workspace for a console project.
Console projects expect a main() function instead of WinMain() and hence it won't compile.

Create a New Project and select the Win32 Application option and then compile the code.
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 9 Jul, 2008 - 11:39 PM
Post #4


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


Well shoot, I was way off.
User is online!Profile CardPM

Go to the top of the page

The Architect 2.0
post 9 Jul, 2008 - 11:44 PM
Post #5


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

QUOTE(born2c0de @ 10 Jul, 2008 - 12:37 AM) *

When you simply copy-paste code and execute it in VC++ 6.0 IDE, it creates a default workspace for a console project.
Console projects expect a main() function instead of WinMain() and hence it won't compile.

Create a New Project and select the Win32 Application option and then compile the code.


i'm using microsoft visual c++ 2008 express edition and it has a 'win32 project.' is that 'good enough'?
User is offlineProfile CardPM

Go to the top of the page

polymath
post 10 Jul, 2008 - 12:24 PM
Post #6


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


It should be.
User is offlineProfile CardPM

Go to the top of the page

Cerolobo
post 10 Jul, 2008 - 02:44 PM
Post #7


D.I.C Regular

Group Icon
Joined: 5 Apr, 2008
Posts: 440



Thanked 31 times
My Contributions


Try
CODE
MessageBox(NULL, L"Hello World!", L"Hello World", MB_OK);  


Microsoft has two variations to a lot of their functions, a ASCII and a Unicode version. If you see a function with a "W" at the end of the name (As your error code says), then you are running in Unicode come. If there is a "A" at the end, then it's ASCII.

The L in front of a string tells the compiler to convert the ASCII string to Unicode. This will only work for string literals though.
User is offlineProfile CardPM

Go to the top of the page

perfectly.insane
post 10 Jul, 2008 - 05:38 PM
Post #8


D.I.C Addict

Group Icon
Joined: 22 Mar, 2008
Posts: 557



Thanked 46 times

Dream Kudos: 25

Expert In: C/C++

My Contributions


If you want to use the ASCII versions by default, then make sure that _UNICODE and UNICODE are *NOT* defined. If you're using Visual C++, there should be an option for preprocessor definitions in the compilation options.

The best way to do this IMHO is to write your code so that it can be compiled either way:

cpp

#include <windows.h>

// Note this:
#include <tchar.h>

// Note: _tWinMain doesn't work in MinGW. In this case, there is a workaround.
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, _T("Hello World"), _T("Hello World"), MB_OK);
return 0;
}


In this case, _T("string") is expanded to L"string" when the *W functions are to be used, or just "string" when the ASCII version are to be used.

In the API documentation, you will find many references to LPTSTR, LPCTSTR, TCHAR[], etc. For these strings, just use _T(...) around it to have it automatically adapted to the compilation environment.
User is offlineProfile CardPM

Go to the top of the page

bodom658
post 10 Jul, 2008 - 07:31 PM
Post #9


D.I.C Head

**
Joined: 22 Feb, 2008
Posts: 115



Thanked 4 times
My Contributions


i suggest trying:

cpp

MessageBox(NULL, (LPCWSTR)L"Hello World!", (LPCWSTR)L"Hello World", MB_OK);


This works for me and is what microsoft recomended me to do.
User is offlineProfile CardPM

Go to the top of the page

born2c0de
post 10 Jul, 2008 - 09:31 PM
Post #10


printf("I'm a %XR",195936478);

Group Icon
Joined: 26 Nov, 2004
Posts: 3,905



Thanked 34 times

Dream Kudos: 2800

Expert In: 80x86 Assembly, C/C++, VB6, VB.NET, C#, J2SE, Win32 API, Reversing

My Contributions


Since you are using VC++ 8.0, prefix each string parameter with L like this:
cpp
MessageBox(NULL, L"Hello World!", L"Hello World", MB_OK);


L converts the ASCII string into unicode format.

There is something else worth mentioning in this case:
cpp
MessageBox(NULL, (LPCWSTR)"Hello World!", (LPCWSTR)"Hello World", MB_OK);


Don't typecast the strings to LPCWSTR inspite of the compiler asking you to do so.
Because by doing so, an ASCII string is considered as Unicode and as a result, the MessageBox displays the text in the Chinese font (because the characters match the unicode string) like this:
Attached Image

Typecasting does not convert ASCII to Unicode, it merely considers an ASCII string as a Unicode string.
User is offlineProfile CardPM

Go to the top of the page

The Architect 2.0
post 10 Aug, 2008 - 01:00 PM
Post #11


D.I.C Head

**
Joined: 22 May, 2008
Posts: 56

QUOTE(perfectly.insane @ 10 Jul, 2008 - 06:38 PM) *

If you want to use the ASCII versions by default, then make sure that _UNICODE and UNICODE are *NOT* defined. If you're using Visual C++, there should be an option for preprocessor definitions in the compilation options.


um...where exactly ARE these compilation options? i am very tired and can't find them.
User is offlineProfile CardPM

Go to the top of the page

KYA
post 20 Aug, 2008 - 07:15 PM
Post #12


#include <nerd.h>

Group Icon
Joined: 14 Sep, 2007
Posts: 4,211



Thanked 50 times

Dream Kudos: 1150
My Contributions


Project Options->Configuration Properties->General->Choose which character set you want to use
User is offlineProfile CardPM

Go to the top of the page

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 11/23/08 02:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month