Article View: borland.public.cppbuilder.nativeapi
Article #2858Re: Problem converting WideString to AnsiString
From: Remy Lebeau (TeamB)
Date: Mon, 11 Dec 2006 19:18
Date: Mon, 11 Dec 2006 19:18
77 lines
3501 bytes
3501 bytes
"Mark Guerrieri" <mguerrieri@nospam_cmsgrp.com> wrote in message
news:457da01d@newsgroups.borland.com...
> The first call to ::WideCharToMultiByte to get the size of the buffer
> ALWAYS returns 0 with GetLastError returning 87- "The Parameter
> is Incorrect"
One thing I notice is that you are not taking into account that
WideString::c_bstr() will return a NULL pointer when the Length() is 0.
Unlike AnsiString::c_str(), which returns a non-NULL pointer instead. The
lpWideCharStr parameter of WideCharToMultiByte() cannot be NULL.
Try this instead:
int __fastcall TCCSMsgObjectEx::BuildMessageFromString(const
WideString& cMessage)
{
int nSize;
if( cMessage.Length() > 0 )
{
nSize = ::WideCharToMultiByte(CP_ACP, 0, cMessage.c_bstr(),
cMessage.Length(), NULL, 0, NULL, NULL);
if( nSize <= 0 )
RaiseLastOSError();
}
else
nSize = 0;
if( (!FMessageBuffer) || (nSize > (int)FMessageSize) )
{
if(FMessageBuffer)
free(FMessageBuffer);
if( nSize > 0 )
{
FMessageBuffer = (BYTE*) malloc(nSize);
if( !FMessageBuffer )
OutOfMemoryError();
}
}
FMessageSize = nSize;
if( nSize > 0 )
{
nSize = ::WideCharToMultiByte(CP_ACP, 0, cMessage.c_bstr(),
cMessage.Length(), FMessageBuffer, nSize, NULL, NULL);
if( nSize <= 0 )
RaiseLastOSError();
}
return FMessageSize;
}
> If I do the following:
>
> AnsiString cValue = AnsiString(wValue);
> BuildMessageFromString(cValue);
>
> then everything works as expected.
As it should be, because the WideString->AnsiString conversion handles the
condition where the WideString may be empty.
> The string stored in wValue may get quite large at times and I would like
> to avoid the overhead of converting to AnsiString first then storing the
> results in the buffer; ::WideCharToMultiByte should just store directly to
> the buffer for me, it can't seem to handle my NULL-infested string...
Yes, it can, as the VCL uses WideCharToMultiByte() internally for its own
conversion. If the NULL characters were tripping up WideCharToMultiByte()
in general, then the VCL would be effected as well, which is obviously not
the case. So the more likely cause is that your use of the function is
simply wrong. Which is evident by the 87 error code.
Gambit
Technical Details
Message-ID:
<457daeaf@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailReferences:
<457da01d@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6651002f727c431 X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Remy Lebeau \(TeamB\)" <no.spam@no.spam.com> Newsgroups: borland.public.cppbuilder.nativeapi References: <457da01d@newsgroups.borland.com> Subject: Re: Problem converting WideString to AnsiString Date: Mon, 11 Dec 2006 11:18:09 -0800 Lines: 79 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1409 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1409 NNTP-Posting-Host: 75.83.34.171 Message-ID: <457daeaf@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1165864623 75.83.34.171 (11 Dec 2006 11:17:03 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news4.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail