🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #213

Re: converting wchar_t * to const wchar_t *

#213
From: Thomas Maeder [TeamB]
Date: Sun, 06 Jul 2008 17:17
49 lines
2809 bytes
"Sean Farrow" <sean.farrow@seanfarrow.co.uk> writes:

> How can I convert wchar_t * to const wchar_t *?

Conversion from 'pointer to non-const' to 'pointer to const' are done
by the compiler does implicitly.


> I'm trying to use the MultiByteToWideChar function with an std::wstring as 
> the output string.
> Any help aprediated.
> The line of code is:
> MultiByteToWideChar(CP_ACP, 0, __FUNC__, -1,DragonInfo.FunctionName.c_str(), 
> DragonInfo.FunctionName.size());

Hmm. This asks for the conversion opposite to the one you write in the
Subject: header and further above, i.e. from 'pointer to const' to
'pointer to non-const'.

If safe, this can be done using the const_cast operator, but in this
case, the conversion would be very unsafe.

The docs of MultiByteToWideChar() indicate to first call it with 0
passed as 5th and 6th parameter. The return value is the required size
of the wide character buffer to hold the result of the conversion. You
can then use that value to create a wide character buffer of that size
and call MultiByteToWideChar() again, this time with non-0 values
passed as 5th and 6th parameter.

E.g.:

int const bufferSize(MultiByteToWideChar(CP_ACP,
                                         0,
                                         __FUNC__,-1,
                                         0,0));
if (bufferSize==0)
  ; // error handling
else
{
  std::vector<wchar_t> wchars(bufferSize);
  int const nrConverted(MultiByteToWideChar(CP_ACP,
                                            0,
                                            __FUNC__,-1,
                                            &wchars[0],nrOfWideChars);
  if (nrConverted==0)
    ; // error handling
  else
    ; // use wchars
}
Technical Details
Message-ID: <m2prprlynt.fsf@glue.ch>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail
References: <4870f768$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,42b54d6e10712e77
X-Google-Attributes: gid10cc67,domainid0,public,usenet
X-Google-Language: ENGLISH,ASCII-7-bit
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: converting wchar_t * to const wchar_t *
References: <4870f768$1@newsgroups.borland.com>
From: maeder@glue.ch (Thomas Maeder [TeamB])
Organization: TeamB
X-Face: ":yn4zt^A_dn>6~{YJmu^B\'4w)e:apPM\VC=Dvf&*tASxX9|BYczkFd$xr.$vNSZ[3Uobr p]c4\+\l){!WQI.$]veP({HLd;CDYH?iN
Date: Sun, 06 Jul 2008 19:17:58 +0200
Message-ID: <m2prprlynt.fsf@glue.ch>
User-Agent: Gnus/5.1006 (Gnus v5.10.6) XEmacs/21.4 (Jumbo Shrimp, linux)
Cancel-Lock: sha1:eSY3Lpb7nsyhKZ+pw0dCMDU1Amg=
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
NNTP-Posting-Host: 80.219.169.10
X-Trace: newsgroups.borland.com 1215364678 80.219.169.10 (6 Jul 2008 10:17:58 -0700)
Lines: 49
X-Authenticated-User: tmaeder
Path: arc1-ca.pugleaf.net!g2news1.google.com!news3.google.com!proxad.net!feeder1-2.proxad.net!feeder.erje.net!news.isoc.lu!newsgroups.borland.com!not-for-mail