www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - wchar[] and wchar*

reply novice2 <sorry noem.ail> writes:
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!
Apr 06 2009
next sibling parent reply Sergey Gromov <snake.scaly gmail.com> writes:
Tue, 07 Apr 2009 01:32:59 -0400, novice2 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?
Since most of FuncW require not only the pointer but length as well, you usually call them like this: wchar[] foo = "some string"; FuncW(foo.ptr, foo.length); To get a string from a function you need a buffer: auto buf = new wchar[MAX_PATH]; buf.length = GetModuleFileNameW(h, buf.ptr, buf.length);
Apr 07 2009
parent reply novice2 <sorry noem.ail> writes:
thank you Sergey

but sometime wchar* is zero-terminated strings (LPWSTR)
i feel lack of toStringz(wchar[]) and toString(wchar*) functions in phobos

or i missed something?
Apr 07 2009
next sibling parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Apr 7, 2009 at 3:09 PM, novice2 <sorry noem.ail> wrote:
 thank you Sergey

 but sometime wchar* is zero-terminated strings (LPWSTR)
 i feel lack of toStringz(wchar[]) and toString(wchar*) functions in phobos
Yeah, that does suck, doesn't it? I wouldn't want to mention the 'T' word for fear of Downs accusing me of proselytizing.
Apr 07 2009
parent downs <default_357-line yahoo.de> writes:
Jarrett Billingsley wrote:
 On Tue, Apr 7, 2009 at 3:09 PM, novice2 <sorry noem.ail> wrote:
 thank you Sergey

 but sometime wchar* is zero-terminated strings (LPWSTR)
 i feel lack of toStringz(wchar[]) and toString(wchar*) functions in phobos
Yeah, that does suck, doesn't it? I wouldn't want to mention the 'T' word for fear of Downs accusing me of proselytizing.
:snort: I do agree in this case. I just don't like it when Tango is proposed as an alternative and the existence of a perfectly valid Phobos solution is ignored.
Apr 09 2009
prev sibling parent reply Kagamin <spam here.lot> writes:
novice2 Wrote:

 but sometime wchar* is zero-terminated strings (LPWSTR)
when?
Apr 09 2009
parent reply novice2 <sorry noem.ail> writes:
Kagamin Wrote:

 novice2 Wrote:
 
 but sometime wchar* is zero-terminated strings (LPWSTR)
when?
everytime, when you see function with LPWSTR without size passing for example: SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath); GetShortPathNameW( IN LPCWSTR lpszLongPath, OUT LPWSTR lpszShortPath, IN DWORD cchBuffer); GetLongPathNameW(IN LPCWSTR lpszShortPath, OUT LPWSTR lpszLongPath, IN DWORD cchBuffer); etc etc etc
Apr 09 2009
parent reply Kagamin <spam here.lot> writes:
novice2 Wrote:

 Kagamin Wrote:
 
 novice2 Wrote:
 
 but sometime wchar* is zero-terminated strings (LPWSTR)
when?
everytime, when you see function with LPWSTR without size passing for example: SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR pszPath); GetShortPathNameW( IN LPCWSTR lpszLongPath, OUT LPWSTR lpszShortPath, IN DWORD cchBuffer); GetLongPathNameW(IN LPCWSTR lpszShortPath, OUT LPWSTR lpszLongPath, IN DWORD cchBuffer); etc etc etc
these functions don't require output buffer to be null-terminated, because it's an *out* buffer, not inout, see example in msdn.
Apr 10 2009
parent reply novice2 <sorry noem.ail> writes:
Kagamin Wrote:

 
 SHGetFolderPathW(HWND hwnd, int csidl, HANDLE hToken, DWORD dwFlags, LPWSTR
pszPath);
 
these functions don't require output buffer to be null-terminated, because it's an *out* buffer, not inout, see example in msdn.
http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx citate: pszPath [out] A pointer to a null-terminated string of length MAX_PATH which will receive the path. How i can determine the length of returned path string? I know only one way - look for 0, because it is 0-terminated For example all registry functions... RegCreateKeyW(IN HKEY hKey, IN LPCWSTR lpSubKey,... in VC 7 folder in PlatformSDK\Include i found 24 *.h files contain "IN LPWSTR" 36 *.h files contain "OUT LPWSTR" we shoiuld multiply this by function count in one file...
Apr 10 2009
parent reply Kagamin <spam here.lot> writes:
novice2 Wrote:

 http://msdn.microsoft.com/en-us/library/bb762181(VS.85).aspx
 
 citate:
 pszPath
     [out] A pointer to a null-terminated string of length MAX_PATH which will
receive the path.
 
 How i can determine the length of returned path string?
 I know only one way - look for 0, because it is 0-terminated
This happens in your code only *after* function call, before function call pszPath may be not initialized. You don't have to create that buffer by transforming some meaningful string to null terminated string. See example on that page. It's a out buffer initially filled with garbage. So you don't need toStringz to create it. It's meaningless.
 For example all registry functions...
 RegCreateKeyW(IN HKEY hKey, IN LPCWSTR lpSubKey,...
this is readonly string. You can make it by simply appending "\0".
 in VC 7 folder in PlatformSDK\Include i found
 24 *.h files contain "IN LPWSTR"
 36 *.h files contain "OUT LPWSTR"
 we shoiuld multiply this by function count in one file...
Can you mention at least one of them?
Apr 11 2009
parent reply novice2 <sorry noem.ail> writes:
Kagamin Wrote:

 
 this is readonly string. You can make it by simply appending "\0".
sorry, but i not understand, what you wan to say to me :( if you want - just say. my original post was: thank you Sergey
but sometime wchar* is zero-terminated strings (LPWSTR)
i feel lack of toStringz(wchar[]) and toString(wchar*) functions in phobos
what wrong? wich assertion is false? for char[] <--> char* interoperability in windows i used before the functions pair std.windows.charset.toMBSz and fromMBSz. and i just wanted to know, how i should act in the case wchar[] <--> wchar* are you want to say that thereis no problem at all? wchar[] --> wchar* just append \0 wchar* --> wchar[] jusr ...? bu i can fantasy long about what you want to say. please, just advice me, how to write program in D with many D <--> C (include Windows) interaction to keep D code simple. some one in this forum advice me use wchar[] and no problem with Windows API. i just novice.
Apr 11 2009
parent Kagamin <spam here.lot> writes:
novice2 Wrote:

 please, just advice me, how to write program in D with many D <--> C (include
Windows) interaction to keep D code simple.
 some one in this forum advice me use wchar[] and no problem with Windows API.
i just novice.
I want to say, you do unneded thing converting output buffers from something else. Don't do unneded things and your code will be simple.
Apr 11 2009
prev sibling parent reply "Denis Koroskin" <2korden gmail.com> writes:
(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 <sorry noem.ail> 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.
Apr 07 2009
parent novice2 <sorry noem.ail> writes:
thank you, Denis
Apr 08 2009