Is there an equivalent to toStringz for wide strings?

Jonathan M Davis jmdavisProg at gmx.com
Mon Apr 4 16:29:59 PDT 2011


On 2011-04-04 16:17, Andrej Mitrovic wrote:
> I think I'd need a toWStringz function, or maybe toStringz can be made more
> clever and figure out that I'm passing a wstring and return a
> null-terminated wchar*.
> 
> Currently I'm using wstrings and appending the null terminator by hand,
> e.g.:
> 
> wstring appName  = "DMapp" ~ "\0";
> 
> Some WinAPI functions in unicode form, such as CreateWindowExW, expect a
> wchar*. So in the above case I just pass `appName.ptr`. But it would be
> convenient not having to append the null terminator prematurely.

Almost all of the string functions are string-specific and don't deal with 
wstring or dstring. Some of them work with multiple string types, but most 
don't - though that may change. In this case, I'd just append the null 
character and be done with it. Not to mention, in the case above, a null 
character should automatically be appended onto the end of the string, since 
it's as string literal (unless wstring or dstring literals are treated 
differently for some reason - you might want to check that). All string 
literals have a null character at one past their end so that they can be 
passed to C functions directly.

Regardless, you should watch out for cases where the function being called 
keeps the pointer that it's passed without copying it but you didn't keep the 
string around, which could cause it to be garbage collected. I've screwed up 
with that before.

In any case, there is no toWStringz in Phobos. You can open up an enhancement 
request for one though.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list