12 Replies - 7543 Views - Last Post: 21 August 2008 - 06:51 PM Rate Topic: -----

#1 The Architect 2.0   User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

microsoft visual c++ - win32 programming

Posted 09 July 2008 - 11:53 PM

#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

Is This A Good Question/Topic? 0
  • +

Replies To: microsoft visual c++ - win32 programming

#2 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 12:00 AM

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.
Was This Post Helpful? 0
  • +
  • -

#3 born2c0de   User is offline

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

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: microsoft visual c++ - win32 programming

Posted 10 July 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.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil   User is offline

  • Professor Snuggly Pants
  • member icon

Reputation: 6698
  • View blog
  • Posts: 31,070
  • Joined: 10-May 07

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 12:39 AM

Well shoot, I was way off.
Was This Post Helpful? 0
  • +
  • -

#5 The Architect 2.0   User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 12:44 AM

View Postborn2c0de, on 10 Jul, 2008 - 12:37 AM, said:

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'?
Was This Post Helpful? 0
  • +
  • -

#6 polymath   User is offline

  • D.I.C Addict
  • member icon

Reputation: 54
  • View blog
  • Posts: 670
  • Joined: 04-April 08

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 01:24 PM

It should be.
Was This Post Helpful? 0
  • +
  • -

#7 Cerolobo   User is offline

  • D.I.C Regular
  • member icon

Reputation: 52
  • View blog
  • Posts: 450
  • Joined: 05-April 08

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 03:44 PM

Try
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.
Was This Post Helpful? 0
  • +
  • -

#8 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: microsoft visual c++ - win32 programming

Posted 10 July 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.

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

#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.
Was This Post Helpful? 0
  • +
  • -

#9 bodom658   User is offline

  • Villiage Idiom
  • member icon

Reputation: 114
  • View blog
  • Posts: 1,123
  • Joined: 22-February 08

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 08:31 PM

i suggest trying:

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



This works for me and is what microsoft recomended me to do.
Was This Post Helpful? 0
  • +
  • -

#10 born2c0de   User is offline

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

Reputation: 187
  • View blog
  • Posts: 4,673
  • Joined: 26-November 04

Re: microsoft visual c++ - win32 programming

Posted 10 July 2008 - 10:31 PM

Since you are using VC++ 8.0, prefix each string parameter with L like this:
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:
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.
Was This Post Helpful? 0
  • +
  • -

#11 The Architect 2.0   User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 351
  • Joined: 22-May 08

Re: microsoft visual c++ - win32 programming

Posted 10 August 2008 - 02:00 PM

View Postperfectly.insane, on 10 Jul, 2008 - 06:38 PM, said:

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.
Was This Post Helpful? 0
  • +
  • -

#12 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3202
  • View blog
  • Posts: 19,234
  • Joined: 14-September 07

Re: microsoft visual c++ - win32 programming

Posted 20 August 2008 - 08:15 PM

Project Options->Configuration Properties->General->Choose which character set you want to use
Was This Post Helpful? 0
  • +
  • -

#13 perfectly.insane   User is offline

  • D.I.C Addict
  • member icon

Reputation: 70
  • View blog
  • Posts: 644
  • Joined: 22-March 08

Re: microsoft visual c++ - win32 programming

Posted 21 August 2008 - 06:51 PM

I don't know much about newer versions of Visual C++, but you also might find an option that specified a list of preprocessed definitions, separated by commas (make sure UNICODE and _UNICODE are not there). But as KYA is indicating, there is also a specific option to select the character type to use (which is essentially the definition of TCHAR).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1