(I wonder why this message wasn't sent hours ago and stuck in my mailbox, but 
here it goes:)

On Tue, 07 Apr 2009 09:32:59 +0400, novice2 <[email protected]> wrote:

hi!
could you advice me, please, what techniques should be used
while working with D wchar[] and C wchar* (e.g. Windows unicode API named ...W()).

how to pass wchar[] to FuncW(wchar*) and back?

thanks!

The easiest way to go is as follows:

import std.string;

// helper functions
wchar* toWptr(wchar[] str, wchar[] buffer = void) {
  size_t len = str.length;
  buffer.length = len + 1;
  buffer[0..len] = str[];
  buffer[len-1] = 0;

  return buffer.ptr;
}

wchar[] fromWptr(wchar* ptr) {
  return ptr[0..wcslen(ptr)];
}

wchar[] s = "Hello, World"w;
wchar[] h = s[0..5];

// usage
FuncW(toWptr(s), toWptr(h));

// how to use to avoid heap activity:
wchar[256] tmp;
wchar* wptr = toWptr(s, tmp);
...


Hope that helps.


Reply via email to