B
babak
Hi everyone
I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn't seem to work.
Here is an example sample of what i try to do:
char msg[120];
string my_name = "John";
sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
And this is the error I get:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char
[120]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
I know that MessageBox wants a LPCTSTR as its second parameter. If I
use that instead (see below)...
LPCTSTR msg;
string my_name = "John";
sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
....I get the following error:
error C2664: 'sprintf' : cannot convert parameter 1 from 'const
unsigned short *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Seems like what ever I try to use either sprintf or MessageBox is not
happy. If I try to cast 'msg' in either sprintf or MessageBox I just
get junk as my output.
Does anyone have a suggestion how I can solve this? Please note that I
don't want to use CString since my platform does not seem to support
that. An example code of how I can solve this problem would be much
appreciated.
Thanks a lot for your help.
/Babak
I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn't seem to work.
Here is an example sample of what i try to do:
char msg[120];
string my_name = "John";
sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
And this is the error I get:
error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'char
[120]' to 'const unsigned short *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
I know that MessageBox wants a LPCTSTR as its second parameter. If I
use that instead (see below)...
LPCTSTR msg;
string my_name = "John";
sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);
....I get the following error:
error C2664: 'sprintf' : cannot convert parameter 1 from 'const
unsigned short *' to 'char *'
Types pointed to are unrelated; conversion requires reinterpret_cast,
C-style cast or function-style cast
Seems like what ever I try to use either sprintf or MessageBox is not
happy. If I try to cast 'msg' in either sprintf or MessageBox I just
get junk as my output.
Does anyone have a suggestion how I can solve this? Please note that I
don't want to use CString since my platform does not seem to support
that. An example code of how I can solve this problem would be much
appreciated.
Thanks a lot for your help.
/Babak