• Advertisement
Sign in to follow this  

MessageBox

This topic is 4349 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

1)If I have a variable and it's of type Dword and I want to print it out in a messagebox like DWORD myone; I can't do MessageBox(NULL, myone, "name", MB_OK); because it requires a type LPCTSTR, so how do i convert it to LPCTSTR?

Share this post


Link to post
Share on other sites
Advertisement
Try something like so:

char buffer[MAX_PATH] = "";
DWORD myone;

sprintf(buffer, "%d", myone);
MessageBox(NULL, buffer, "title", MB_OK);

Basically what sprintf does is it writes variables (like integers, doubles, floats, characters, etc.) into a string buffer, which can then be passed into the MessageBox function. %d in the sprintf function stands for an integer value, while %f would be float, %s is a string, and then there are a few others I forget. Lookup sprintf in the MSDN library; should find all the info you need there.

There's also the itoa() function, but with sprintf you can format the output to be something more appealing.

Share this post


Link to post
Share on other sites
IF you're using C++, you can use strings and this simple useful template function I've found somewhere(can't remember where):

template <class T> string ToString(T val)
{
std::stringstream ss;
ss<<val;
return ss.str();
}

Now you can just do:

MessageBox(NULL,ToString(myone).c_str(),"name",MB_OK);

Of course, you can use the ToString function with any type that can be converted into a string.

Share this post


Link to post
Share on other sites
A more robust method than mikeman's would be to use Boost's lexical_cast function, which will also let you convert strings back into primitives. A link to Boost can be found in my signature.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement