Mourning the loss of our friend, WhitPhil.
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
 
Software Development
Tag Cloud
access audio blue screen boot bsod connection crash dell desktop driver drivers dvd email error excel excel 2003 firefox hard drive hardware hdmi hijackthis internet keyboard laptop malware monitor motherboard network networking outlook problem ram recovery router screen slow sound spyware tdlwsp.dll trojan upgrade video virus vista vundo windows windows 7 windows vista windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Software Development >
MessageBox / General text c++ problem

Tip: Click here to scan for System Errors and Optimize PC performance
[ Sponsored Link ]

 
Thread Tools
andythepandy's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
01-Nov-2009, 11:03 AM #1
MessageBox / General text c++ problem
Hi again,

I I am having problems putting values into a MessageBox and in fact all text in my project.

Code:
LPSTR test = "Hi";
	MessageBox(NULL, (LPCWSTR)test, L"FPS", MB_OK);
The MessageBox that appears contains one, what look like Chinese, character.

I get the same behaviour when drawing text using D3DXDrawTextA()

Thanks for any help
midders's Avatar
Account Closed with 654 posts.
 
Join Date: Dec 1969
01-Nov-2009, 02:59 PM #2
Modern C/C++ projects tend to use wide characters and character strings by default. In your call to Messagebox you are casting a narrow string (LPCSTR) to a wide string (LPCWSTR) which tends to produce garbage. Try using the TCHAR macros which compile to wide or narrow depending on the compiler options.

Code:
TCHAR *test=_T("Hi there!");
... (LPCWSTR)test
Slainte

midders
andythepandy's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
01-Nov-2009, 03:03 PM #3
Ah! Excellent, it works!

Thanks very much
andythepandy's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
01-Nov-2009, 04:01 PM #4
One more thing...
Another question.
My original code was:
Code:
LPSTR str;
str = "";
sprintf(str, "%d", m_FPS);
MessageBox(NULL, (LPCWSTR)str, L"FPS", MB_OK);
When this started return Chinese characters I changed it to:
Code:
LPSTR test = "Hi";
MessageBox(NULL, (LPCWSTR)test, L"FPS", MB_OK);
to test it. How do I implement TCHAR* into sprintf, because it only takes char* as parameter 1?

Thanks
midders's Avatar
Account Closed with 654 posts.
 
Join Date: Dec 1969
02-Nov-2009, 02:14 PM #5
When you run into this sort of problem, I would suggest using wide strings throughout your entire project, to avoid confusion. There are wide versions of all the standard C functions such as sprintf (_stprintf) etc.

If you do need to convert between wide and narrow strings then it is as simple as:
Code:
// TCHAR helper funcs
// note that these funcs each use a single internal buffer, so the string is only valid until the next call to
// the function; for this reason you should not pass the return value to other user functions unless you
// are absolutely sure that they don't use the function themsleves
// recommended usage: strcpy(szDest,T2c(tszSrc));
// _tcscpy(tszDest,c2T(szSrc));
TCHAR *c2T(char *cstr)
{
    int i=0;
    static TCHAR tstr[MAX_BUF];
    while (tstr[i]=(TCHAR)cstr[i++]);
    return tstr;
} // c2T
char *T2c(TCHAR *tstr)
{
    int i=0;
    static char cstr[MAX_BUF];
    while (cstr[i]=(char)tstr[i++]);
    return cstr;
} // T2c
andythepandy's Avatar
Member with 66 posts.
 
Join Date: Jul 2006
02-Nov-2009, 03:09 PM #6
Ok, I'm having some annoying problems...

I have a function to get the FPS, GetFPS(), which returns a float value;

I currently have:
Code:
char Text[40] = "";
float Num = GameTimer->GetFPS();
sprintf(Text, "%d", Num);
int i=0;
static TCHAR tstr[40];
while (tstr[i]=(TCHAR)Text[i++]);
MessageBox(NULL, (LPCWSTR)tstr, L"HI", MB_OK);
The MessageBox displays 0 where the actual value is 80.9.

I tried _stprintf also:
Code:
wchar_t* Text;
Text = _T("");
float Num = GameTimer->GetFPS();
_stprintf(Text, _T("%d"), Num);
MessageBox(NULL, (LPCWSTR)Text, L"HI", MB_OK);
which kicks out the error:

Unhandled exception at 0x5a283abe (msvcr90d.dll) in CompleteGameEngine.exe: 0xC0000005: Access violation writing location 0x00f6999c.

Arghhh!!

Thanks
Reply Bookmark and Share

Smart Search

Find your solution!



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.

Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -5. The time now is 09:27 PM.
Copyright © 1996 - 2009 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.
Powered by Cermak Technologies, Inc.