Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a int to LPCTSTR? Win32

Tags:

c++

winapi

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);
like image 277
Nick Avatar asked Dec 09 '22 06:12

Nick


2 Answers

Converting for MFC like belov :

int number = 1;

CString t;

t.Format(_T("%d"), number);

AfxMessageBox(t);

I used and it worked for me.

like image 93
Mahmut EFE Avatar answered Jan 31 '23 03:01

Mahmut EFE


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);  
like image 35
In silico Avatar answered Jan 31 '23 05:01

In silico