Article View: borland.public.cppbuilder.nativeapi
Article #2857Problem converting WideString to AnsiString
From: Mark Guerrieri
Date: Mon, 11 Dec 2006 18:15
Date: Mon, 11 Dec 2006 18:15
88 lines
3583 bytes
3583 bytes
I have the following functions that are called from within the context of a
thread:
//--------------------------------------------------------------------------
-
int __fastcall TCCSMsgObjectEx::BuildMessageFromString(const AnsiString&
cMessage)
{
int nSize = cMessage.Length();
if(FMessageBuffer == NULL || nSize > (int)FMessageSize)
{
if(FMessageBuffer)
free(FMessageBuffer);
FMessageBuffer = (BYTE*)malloc(nSize);
if(FMessageBuffer == NULL)
OutOfMemoryError();
}
FMessageSize = nSize;
memcpy(FMessageBuffer, cMessage.c_str(), nSize);
return FMessageSize;
}
//--------------------------------------------------------------------------
-
int __fastcall TCCSMsgObjectEx::BuildMessageFromString(const WideString&
cMessage)
{
int nSize = ::WideCharToMultiByte(CP_ACP, 0, cMessage.c_bstr(),
cMessage.Length(), 0, 0, NULL, NULL);
if(nSize <= 0)
RaiseLastOSError();
if(FMessageBuffer == NULL || nSize > (int)FMessageSize)
{
if(FMessageBuffer)
free(FMessageBuffer);
FMessageBuffer = (BYTE*)malloc(nSize);
if(FMessageBuffer == NULL)
OutOfMemoryError();
}
FMessageSize = nSize;
nSize = ::WideCharToMultiByte(CP_ACP, 0, cMessage.c_bstr(),
cMessage.Length(), FMessageBuffer, nSize, NULL, NULL);
if(nSize <= 0)
RaiseLastOSError();
return FMessageSize;
}
//--------------------------------------------------------------------------
-
FMessageBuffer (BYTE*) and FMessageSize (int) are internal to the class.
I retrieve a WideString value wValue, and call:
BuildMessageFromString(wValue);
The first call to ::WideCharToMultiByte to get the size of the buffer ALWAYS
returns 0 with GetLastError returning 87- "The Parameter is Incorrect"
If I do the following:
AnsiString cValue = AnsiString(wValue);
BuildMessageFromString(cValue);
then everything works as expected.
wValue is a 54 character string that begins with 12 NULL characters and has
other NULL characters in other positions as well. The AnsiString cValue is
correct in that it has a length of 54 and scanning the strings yields the
correctly converted characters (which all happen to be ansi anyway.)
So why does ::WideCharToMultiByte fail every time? If I pass -1 for the
input buffer length, then the fuction suceeds, but sees this as a NULL
string since the first character is a NULL.
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...
Any ideas?
Mark
Mark
Technical Details
Message-ID:
<457da01d@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,6651002f727c431,start X-Google-Attributes: gid10cc67,public X-Google-Language: ENGLISH,ASCII-7-bit From: "Mark Guerrieri" <mguerrieri@nospam_cmsgrp.com> Newsgroups: borland.public.cppbuilder.nativeapi Subject: Problem converting WideString to AnsiString Date: Mon, 11 Dec 2006 13:15:52 -0500 Lines: 90 Organization: Creative Marketing Strategies X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2800.1158 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165 NNTP-Posting-Host: 209.50.174.82 Message-ID: <457da01d@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1165860893 209.50.174.82 (11 Dec 2006 10:14:53 -0700) Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news.germany.com!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail