D2 string conversion

Sivo Schilling sivo.schilling at web.de
Sat May 9 12:20:27 PDT 2009


Sam Hu Wrote:

> Hello,
> 
> In D2 I tried to write a simple function which wrapps WIN32 API MessageBoxW,with which I can handle wide characters like Chinese .Below is my try:
> 
> import std.string;
> import std.conv;
> import std.c.windows.windows;
> 
> extern(Windows)int MessageBoxW(HWND,LPCSTR,LPCSTR,int);
> int showMessage(wchar[] msg,wchar[ ] caption=cast(wchar[])"Application",int style=MB_OK|MB_ICONINFORMATION)
> {
> return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(caption),style);//try one
> return MessageBoxW(null,toStringz(to!(const(char[])(msg)),toStringz(to!(const(char)[])(caption)),style);//try two
> }
> 
> But both *try one* and *try two* the above are wrong.Anyone can figure me out what's the problem and what's the right way to do would be appreciated.
> 
> Sam

Hi Sam,

here your code snippet with a few modifications to work:

---
module sam;

import std.string;
import std.conv;
import std.c.windows.windows;

extern(Windows)int MessageBoxW(HWND, LPCWSTR, LPCWSTR, int);

int showMessage(in wchar[] msg, in wchar[] caption = cast(wchar[])"Application", int style = MB_OK|MB_ICONINFORMATION)
{
    // return MessageBoxW(null,to!(const(char)[])(msg),to!(const(char)[])(caption),style);//try one
    return MessageBoxW(null, (msg ~ "\0").ptr, (caption ~ "\0").ptr, style); //try two
}

void main()
{
    showMessage("Message with embedded \u00E4\u00E5\u00F6 european unicode chars"w);
}
----

Hope this will help you.
Chears,
Sivo


More information about the Digitalmars-d-learn mailing list