
Signature
Doug Harrison
Visual C++ MVP
> On Thu, 13 Oct 2005 11:49:30 +0200, "Egbert Nierop \(MVP for IIS\)"
> <egbert_nierop@nospam.invalid> wrote:
[quoted text clipped - 6 lines]
>
> Can you demonstrate this in a little console program?
ps: I established this bug. It is not windows but VC 8 which behaves
different with VC 7.
I have mailed this to Michael M (Microsoft).
BSTR __cdecl Format(PCWSTR pszFormat, ...) throw()
{
va_list args;
HRESULT hr = S_OK;
BSTR retVal = NULL;
va_start( args, pszFormat );
int len = _vscwprintf( pszFormat, args );
retVal = SysAllocStringLen(NULL, len);
if(retval != NULL)
vswprintf( retVal, (SIZE_T)len, pszFormat, args );
va_end(args);
return retVal;
}
INT main()
{
CoInitialize(NULL);
{
BSTR test = Format(L"%s, %d,%s", L"one", 2, L"three");
wprintf(test); //"output one, 2, thre" <-- BUG this used to have the
correct length in VC 7?
SysFreeString(test);
}
CoUninitialize();
return 0;
}
Jochen Kalmbach [MVP] - 14 Oct 2005 08:26 GMT
Hi Egbert!
> ps: I established this bug. It is not windows but VC 8 which behaves
> different with VC 7.
[quoted text clipped - 12 lines]
> return retVal;
> }
If it had worked in VC7(.1) then it was a bug in VC7(.1) and is now
corrected in VC8 :-)
The following line is wrong:
> int len = _vscwprintf(pszFormat, args);
You should change it to
int len = _vscwprintf(pszFormat, args) + 1;
The reason is:
_vscprintf doesn't count the terminating '\0'!
but vswprintf requires the terminating NUL in in the count.

Signature
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Egbert Nierop (MVP for IIS) - 14 Oct 2005 08:44 GMT
> Hi Egbert!
>
[quoted text clipped - 17 lines]
> If it had worked in VC7(.1) then it was a bug in VC7(.1) and is now
> corrected in VC8 :-)
Great! :<
Counting on bugs helps one to improve his skills :)