473,386 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Formating a string using sprintf and showing it in MessageBox!

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

Nov 17 '05 #1
12 15174
Hi 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 *'


You compile a UNICODE program but you heare are dealing with ANSI strings...

Change it to

<code>
ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

TCHAR msg[120];
tstring my_name = _T("John");
__stprintf(msg, _T("My name is: %s "), (LPCTSTR) my_name);
MessageBox(NULL, msg, NULL, MB_OK);
</code>

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #2
> 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 Looks like the applicaiton is Unicode.
LPCTSTR msg;
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK); What is "string" ?
Anyway, try:

TCHAR msg[120];
TCHAR *my_name = _T("John");

_stprintf (msg, _T("My name is: %s "), my_name);
MessageBox(NULL, msg, NULL, MB_OK);

Notes:
----------------
LPCTSTR msg;
will crash you application, because it is a non-initialized pointer.
It expands to
LPCTSTR:
LP = Long Pointer (the long is obsolete, from the old days when
Intel procesors used segmented memory)
C = const
T = generic text type
STR = string
So, this becomes:
if Non-Unicode: const char * msg;
if Unicode: const wchar_t * msg;
Generic: const TCHAR * msg;
----------------
Note _stprintf insted of sprintf and _T(".....")
If you read the doc for sprintf, you will see a table named
"Generic-Text Routine Mappings". "TCHAR.H routine" is what you
allways want.
----------------
Read about the the "Generic-Text Routine Mappings" in MSDN.
----------------
Read this blurb: http://www.mihainita.net/20050306b.shtml
Please note that I don't want to use CString since my platform does not
seem to support that.

CString is part of MFC (MFC/ATL in VC.NET). If you configure the
project to use MFC, then you will get it.

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #3
><code>
ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

TCHAR msg[120];
tstring my_name = _T("John");
__stprintf(msg, _T("My name is: %s "), (LPCTSTR) my_name);
MessageBox(NULL, msg, NULL, MB_OK);
</code>

I did not think about std::string :-)
Jochen is right, that is what you should do.

But he is not right in double-underscore in __stprintf :-)
Correct that, and you have a running thing.

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #4
Hi Jochen
Thanks for your help. I tried what you suggested and got the following
error:

error C2440: 'type cast' : cannot convert from 'class
std::basic_string<unsigned short,struct std::char_traits<unsigned
short>,class std::allocator<unsigned short> >' to 'const unsigned short
*'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

This doesn't seem like the right approach to me because to my
understanding the variable that the compiler is not happy with is 'msg'
(i.e the first parameter in sprintf and second parameter in MessageBox)
and not 'string' (which you refer to as 'tstring' in your code) that
you changed in your code. Have I missunderstood this totally?

/Babak

Nov 17 '05 #5
Hi Mihai!
__stprintf(msg, _T("My name is: %s "), (LPCTSTR) my_name);


But he is not right in double-underscore in __stprintf :-)
Correct that, and you have a running thing.


Upps... it was a copy-and-past error...

But to be even better, he should use "_sntprintf" instead of "_stprintf"!

<code>
ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

TCHAR msg[120];
tstring my_name = _T("John");
_sntprintf(msg, 120, _T("My name is: %s "), (LPCTSTR) my_name);
MessageBox(NULL, msg, NULL, MB_OK);
</code>
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #6
Thanks for explanations in the notes to your first message, Mihai. It
is all now a little bit more clear to me. However, as I wrote in my
last message, this still does not seem to work (I used single
underscore in _sprintf). Any suggestions?

Thanks for your help.
/Babak

Nov 17 '05 #7
Hi babak!
error C2440: 'type cast' : cannot convert from 'class
std::basic_string<unsigned short,struct std::char_traits<unsigned
short>,class std::allocator<unsigned short> >' to 'const unsigned short
*'
No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called


Upps.. I forgot that the STL-(w)string class has not const-operator...

Here is a complete working example:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

#include <string>

#ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

int _tmain()
{
TCHAR msg[120];
tstring my_name = _T("John");
_sntprintf(msg, 120, _T("My name is: %s "), my_name.c_str());
MessageBox(NULL, msg, NULL, MB_OK);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #8
Here is a complete working example:

Again a really, really complete working example:

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#pragma comment(lib, "user32.lib")

#include <string>

#ifdef _UNICODE
#define tstring std::wstring
#else
#define tstring std::string
#endif

int _tmain()
{
TCHAR msg[120+1];
tstring my_name = _T("John");
_sntprintf(msg, 120, _T("My name is: %s "), my_name.c_str());
msg[120] = 0;
MessageBox(NULL, msg, NULL, MB_OK);
}

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #9
Thanks a lot for your help. Not it seems to be working properly!

/Babak

Mihai N. skrev:
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

Looks like the applicaiton is Unicode.
LPCTSTR msg;
string my_name = "John";

sprintf (msg, "My name is: %s ", my_name);
MessageBox(NULL, msg, NULL, MB_OK);

What is "string" ?
Anyway, try:

TCHAR msg[120];
TCHAR *my_name = _T("John");

_stprintf (msg, _T("My name is: %s "), my_name);
MessageBox(NULL, msg, NULL, MB_OK);

Notes:
----------------
LPCTSTR msg;
will crash you application, because it is a non-initialized pointer.
It expands to
LPCTSTR:
LP = Long Pointer (the long is obsolete, from the old days when
Intel procesors used segmented memory)
C = const
T = generic text type
STR = string
So, this becomes:
if Non-Unicode: const char * msg;
if Unicode: const wchar_t * msg;
Generic: const TCHAR * msg;
----------------
Note _stprintf insted of sprintf and _T(".....")
If you read the doc for sprintf, you will see a table named
"Generic-Text Routine Mappings". "TCHAR.H routine" is what you
allways want.
----------------
Read about the the "Generic-Text Routine Mappings" in MSDN.
----------------
Read this blurb: http://www.mihainita.net/20050306b.shtml
Please note that I don't want to use CString since my platform does not
seem to support that.

CString is part of MFC (MFC/ATL in VC.NET). If you configure the
project to use MFC, then you will get it.

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email


Nov 17 '05 #10
> But to be even better, he should use "_sntprintf" instead of "_stprintf"!
....
TCHAR msg[120];
tstring my_name = _T("John");
_sntprintf(msg, 120, _T("My name is: %s "), (LPCTSTR) my_name);


True. What I normaly do is this:

#define DIM(a) (sizeof(a)/sizeof(a[0]))
_sntprintf( msg, DIM(msg), ... );

This way you don't have to manualy keep in sync the
_sntprintf parameter with the buffer size.
You just have to be careful to use it only on pointers,
not on arrays.
I also have something in C++, using templates, that gives
compile error if used on pointers.
But is a bit long to post here. And VC2005 will have something anyway :-)

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #11
Hi Mihai!
But to be even better, he should use "_sntprintf" instead of "_stprintf"!

TCHAR msg[120];
tstring my_name = _T("John");
_sntprintf(msg, 120, _T("My name is: %s "), (LPCTSTR) my_name);


True. What I normaly do is this:

#define DIM(a) (sizeof(a)/sizeof(a[0]))
_sntprintf( msg, DIM(msg), ... );


But this is wrong... (as I also did in my first most... ;-) )

If the resulting string is exactly the size of the passed buffer, then
*no* NUl characater will be appended...

So you need to do:
#define DIM(a) (sizeof(a)/sizeof(a[0]))
_snprintf(msg, DIM(msg)-1, ...);
msg[DIM(msg)-1] = 0;

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Nov 17 '05 #12
> So you need to do:
#define DIM(a) (sizeof(a)/sizeof(a[0]))
_snprintf(msg, DIM(msg)-1, ...);
msg[DIM(msg)-1] = 0;


In fact, if you do msg[DIM(msg)-1] = 0;
you can do _snprintf(msg, DIM(msg), ...); without problem.
It is just about "what's your style."

Ok, now we move from internationalization to security.
I know, snprintf is unsafe. You should use the safe string API from MS.

But I kust did not feel like taking any piece of code from newsgroups and
rising it to production code level.
This is what I do with my production code, true.
But on newsgroups, sometimes I do feel like baby-sitting, sometimes not.

For instance, I would not start here a discution about portability.
Not today. Some other day, maybe :-)

--
Mihai Nita [Microsoft MVP, Windows - SDK]
------------------------------------------
Replace _year_ with _ to get the real email
Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Andrew Connell | last post by:
Having fits transforming an XML string using an XSL file. In the 1.1 version of the framework, I see that the XmlResolver is heavily used in the XslTransform class. However, that looks like I am...
2
by: Maziar Aflatoun | last post by:
Hi guys, I'm using Windows authentication to connect to SQL Server 2000. On my computer the connection is fine. Now if I move it to a remote server, how to I hard code my Username/Password in...
2
by: Bruno | last post by:
Hello friends I'm creating one application that will send content to another app using HTTP connection. To authenticate my connection, I need to send a string with the username and password...
3
by: minguskhan | last post by:
Does anyone know how to reverse a string using a loop?
2
by: aurora | last post by:
I have some unicode string with some characters encode using python notation like '\n' for LF. I need to convert that to the actual LF character. There is a 'unicode_escape' codec that seems to...
11
by: wreckingcru | last post by:
I'm trying to tokenize a C++ string with the following code: #include <iostream> #include <stdio> #include <string> using namespace std; int main(int argc, char* argv)
7
by: wenmang | last post by:
what is format for sprintf to convert long long integer (64 bits) to string?
8
by: Nkhosinathie | last post by:
i'm comparing two string using the function stryncmp,but my problem is when i execute the program it doesn't give me the correct values.actually it gives me same results as the function strycmp ...
2
by: powerfulperl | last post by:
I want to locate a string 'Local=IN' from a file and I am sure that this string is located within 100 lines(assumption) from the beginning of the file out of 5000 lines. The 100th line start with the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.