9 Replies - 4417 Views - Last Post: 26 August 2008 - 10:21 AM Rate Topic: -----

#1 Viviaci  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-August 08

Using MessageBox in WinMain

Post icon  Posted 25 August 2008 - 09:57 PM

I'm pretty new at this and have a programming book, that i copied some code out of line for line, but it doesn't work.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>


int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)

{
	MessageBox(NULL, "There one", "There can only be one", MB_OK | MB_ICONEXCLAMATION);
	return(0);
}



and i get this error:

error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char [10]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast



I can't figure out why the example in this game programming book won't work on my machine. Granted the Game programming book is a few years old, but shouldn't this work?

Thanks for anyones help on this

Is This A Good Question/Topic? 0
  • +

Replies To: Using MessageBox in WinMain

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,603
  • Joined: 23-August 08

Re: Using MessageBox in WinMain

Posted 25 August 2008 - 10:05 PM

You have UNICODE defined in your project. Either remove that define, or use one of the text conversion macros available, like _T("There one")

See also here.
Was This Post Helpful? 0
  • +
  • -

#3 Viviaci  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-August 08

Re: Using MessageBox in WinMain

Posted 25 August 2008 - 10:25 PM

So apparently im doing this wrong. I originally changed the project properties, but changed them back to the initial settings. I opted to use the _T macro method instead, but still can't get it to work. Is there something im doing wrong in using it?

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)

{
	MessageBox(NULL, _T("There one"), _T("There can only be one"), MB_OK | MB_ICONEXCLAMATION);
	return(0);
}


Here is the error code i get

error C3861: '_T': identifier not found

which leads me to believe im using the macro wrong, but not sure how to use it.

This post has been edited by Viviaci: 25 August 2008 - 10:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,603
  • Joined: 23-August 08

Re: Using MessageBox in WinMain

Posted 25 August 2008 - 10:33 PM

There's more info here. Think you need to include <tchar.h>.
Was This Post Helpful? 1
  • +
  • -

#5 Viviaci  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-August 08

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 07:26 AM

Still having errors when building. I can't believe this book code won't work. Here is my new updated code - that still does not work.

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <windowsx.h>
#include <Tchar.h>



int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)

{
	MessageBox(NULL, _T("There one"), _T("There can only be one"), MB_OK | MB_ICONEXCLAMATION);
	return(0);
}

This post has been edited by Viviaci: 26 August 2008 - 07:55 AM

Was This Post Helpful? 0
  • +
  • -

#6 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 07:28 AM

try

 MessageBox(NULL, L"There one", L"There can only be one", MB_OK | MB_ICONEXCLAMATION);


Was This Post Helpful? 0
  • +
  • -

#7 perfectly.insane  Icon User is offline

  • D.I.C Addict
  • member icon

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

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 08:47 AM

Usually when UNICODE is set, the application entry point is wWinMain. There's a special WinMain definition also:

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, UINT nShowCmd)

Note the T in LPTSTR.

Also, I doubt that this is the problem as filesystems in Windows are not case sensitive (but case preserving), but your Tchar.h should be tchar.h.
Was This Post Helpful? 0
  • +
  • -

#8 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 09:13 AM

I have unicode set on and I can use a regular WinMain().
Was This Post Helpful? 0
  • +
  • -

#9 Viviaci  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 9
  • Joined: 25-August 08

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 09:29 AM

Very wierd. I just installed the Visual C++ 2008 Express edition on my work computer and my last code piece worked just fine, but didn't work on my home computer, which has Visual C++ 2005. Not sure why that is, but nonetheless very confusing as to why it works on one machine and not another. Thanks for everyone's help
Was This Post Helpful? 0
  • +
  • -

#10 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6091
  • View blog
  • Posts: 23,603
  • Joined: 23-August 08

Re: Using MessageBox in WinMain

Posted 26 August 2008 - 10:21 AM

Glad you got it sorted :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1