7

I would like to display an int value in a win32 MessageBox. I have read some different methods to perform this cast. Can someone provide me with a good implementation.

New to Win32 programming so go easy :)

update

So this is what i have so far. It works.. but the text looks like Chinese or some other double byte characters. I am not groking the Unicode vs. not Unicode types. Can someone help me understand where I am going wrong?

 int volumeLevel = 6; std::stringstream os; os<<volumeLevel; std::string intString = os.str(); MessageBox(plugin.hwndParent,(LPCTSTR)intString.c_str(), L"", MB_OK); 
10
  • 1
    Possible duplicate of Formatting an integer in C++. (Use wostringstream for Unicode.) Commented Aug 25, 2011 at 3:17
  • 1
    @In silico: Nick is asking about converting an int to LPCTSTR, you are guiding him to use the wchar_t based wostringstream. Which assumes his project is compiled in with UNICODE or _UNICODE enabled. Commented Aug 25, 2011 at 3:22
  • To account for both unicode and multi-byte encodings (as LPC*T*STR suggests), you'll have to use #ifdef UNICODE typedef wostringstream tstringstream #else typedef ostringstream tstringstream #endif Commented Aug 25, 2011 at 3:26
  • 3
    This has been asked so many times before: stackoverflow.com/questions/228005/… stackoverflow.com/questions/273908/… stackoverflow.com/questions/5555616/… stackoverflow.com/questions/5590381/… stackoverflow.com/questions/4668760/… just to name a few Commented Aug 25, 2011 at 3:26
  • 1
    @Nick: LPCTSTR is a typedef to const char* or const wchar_t* depending on whether UNICODE or _UNICODE has been defined. You can get a const char* from a std::string or a const wchar_t* from a std::wstring via the c_str() method of string/wstring. Commented Aug 25, 2011 at 4:03

5 Answers 5

6

Converting for MFC like belov :

int number = 1; CString t; t.Format(_T("%d"), number); AfxMessageBox(t); 

I used and it worked for me.

Sign up to request clarification or add additional context in comments.

1 Comment

Note that AfxMessageBox receives LPCTSTR, not CString. The above code works because CString (CSimpleStringT) has operator PCXSTR().
4
int OurVariable; LPCWSTR result=(to_string(OurVariable).c_str()); 

or

LPCWSTR result=LPCSTR(to_string(OurVariable).c_str()); 

or

LPCSTR result=(to_string(OurVariable).c_str()); 

it really works

Comments

3

LPCTSTR is defined like this:

#ifdef UNICODE typedef const wchar_t* LPCTSTR; #else typedef const char* LPCTSTR; #endif 

std::string::c_str() returns a const char* only. You can't convert a const char* directly to const wchar_t*. Normally the compiler will complain about it, but with the LPCTSTR cast you end up forcing the compiler to shut up about it. So of course it doesn't work as you expect at runtime. To build on what you have in your question, what you probably want is something like this:

// See Felix Dombek's comment under OP's question. #ifdef UNICODE typedef std::wostringstream tstringstream; #else typedef std::ostringstream tstringstream; #endif int volumeLevel = 6; tstringstream stros; stros << volumeLevel; ::MessageBox(plugin.hwndParent, stros.str().c_str(), L"", MB_OK); 

2 Comments

If you don't want to use the typedef, you could also use std::basic_ostringstream<TCHAR>.
This in conjunction with a good article on MS strings is good start :) Sorry for my ignorance with w_char_t vs char. I need to do some research. Thanks for all the help.
3

nA few ways:

int value = 42; TCHAR buf[32]; _itot(value, buf, 10); 

another more friendly way for your case:

int value = 42; const size_t buflen = 100; TCHAR buf[buflen]; _sntprintf(buf, buflen - 1, _T("the value is %d"), value); 

4 Comments

So _stprintf will put the formatted string the TCHAR buffer which can then be cast to LPCTSTR?
@Nick, LPCTSTR is just another name for const TCHAR *. When you use an array of TCHAR in a context that wants TCHAR * or const TCHAR * the compiler will automatically provide a pointer for you.
@Jack, you need to wrap that string literal in the _sntprintf call with a _T().
Since he's using TCHAR instead of _TCHAR, shouldn't the string literal be wrapped with TEXT() instead of _T()?
-1

Use _T() decorator for Unicode-aware code:

int number = 1; CString t; t.Format(_T("%d"), number); AfxMessageBox(t); 

ref: https://social.msdn.microsoft.com/Forums/vstudio/en-US/f202b3df-5849-4d59-b0d9-a4fa69046223/how-to-convert-int-to-lpctstr?forum=vclanguage

2 Comments

Your answer might be clearer if you used the same variable names as the original poster's question. Thanks.
Duplicated answer. Already a user answered. You just copied and pasted it here. Redundant work.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.