BSTR in sprintf
-
Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks
itkid wrote:
...however it is showing only first charactor.
What is it? How are you verifying this? This works fine for me:
void main( void )
{
BSTR name(_T("David"));
TCHAR buff[32];
_stprintf(buff, _T("%s"), name);
}
"Take only what you need and leave the land as you found it." - Native American Proverb
-
Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks
That will happen if you try to treat a wide (UNICODE/BSTR) string as an narrow (ANSI) string. If using the ANSI version of
sprintf(...)
,%s
means ANSI string and%S
(uppercase 'S') means UNICODE string. For the UNICODE version, the meaning of the format specifiers is reversed. Additionally, if you pass a wide version of a string as the format specification to the (ANSI version of)sprintf(...)
, only the first character will be formatted into the buffer ('N' in your example above). Peace! -=- James
If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
Tip for new SUV drivers: Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
DeleteFXPFiles & CheckFavorites (Please rate this post!) -
Hi, I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name); Thanks
itkid wrote:
I am trying to put BSTR variable in buffer, using sprintf however it is showing only first charactor.Can you please let me know how to store it in buffer.I am trying fun (BSRT name,...) sprint(buff,L"Name =%s", name);
What about this
CString str;
str.Format("Name =%s", name);
BSTR buff= str.AllocSysString()"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV
-
itkid wrote:
...however it is showing only first charactor.
What is it? How are you verifying this? This works fine for me:
void main( void )
{
BSTR name(_T("David"));
TCHAR buff[32];
_stprintf(buff, _T("%s"), name);
}
"Take only what you need and leave the land as you found it." - Native American Proverb
DavidCrow wrote:
BSTR name(_T("David"));
This is dodgy. A
BSTR
is not just a wide string - It has a length prefixed before the first character (before where the pointer points). Code like this means that this length prefix is not initialized. This is a serious error and bloody hard to track down. Here are a few examples which show some of the problems:BSTR name(L"David"); // BAD CODE!!!!! UINT Len = SysStringLen(name); // Wrong length will be returned. SysFreeString(name); // Crash if you're lucky!
Steve