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 seen to support
that. An example code of how I can solve this problem would be much
appreciated.
Thanks a lot for your help.
/Babak
Rogas69 - 26 Aug 2005 11:42 GMT
Hi,
AFAIR you have defined __UNICODE in your project. you should either use
'wide' version of sprintf and use wchar or call directly MessageBoxA,
because due to this UNICODE definition linker links unicode versions of
libraries.
Peter
Ilya Tumanov [MS] - 26 Aug 2005 19:50 GMT

Signature
Best regards,
Ilya
This posting is provided "AS IS" with no warranties, and confers no rights.
*** Want to find answers instantly? Here's how... ***
1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactfra
mework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
> Hi everyone
> I want to format a string (using sprintf) and put it in a messagebox
[quoted text clipped - 40 lines]
> Thanks a lot for your help.
> /Babak
Ilya Tumanov [MS] - 26 Aug 2005 20:27 GMT
Oops, hit wrong button...
Consider using Unicode versions of all functions and always declare strings
as Unicode.
That would save you a lot of time should you need to localize your
application.
Also, consider using Safe CRT:
http://blogs.msdn.com/shawnfa/archive/2004/04/08/110097.aspx
Best regards,
Ilya
This posting is provided "AS IS" with no warranties, and confers no rights.
*** Want to find answers instantly? Here's how... ***
1. Go to
http://groups-beta.google.com/group/microsoft.public.dotnet.framework.compactfra
mework?hl=en
2. Type your question in the text box near "Search this group" button.
3. Hit "Search this group" button.
4. Read answer(s).
> Hi everyone
> I want to format a string (using sprintf) and put it in a messagebox
[quoted text clipped - 40 lines]
> Thanks a lot for your help.
> /Babak