converting D's string to use with C API with unicode

IGotD- nise at nise.com
Sat Dec 5 20:16:55 UTC 2020


On Saturday, 5 December 2020 at 20:12:52 UTC, IGotD- wrote:
> On Saturday, 5 December 2020 at 19:51:14 UTC, Jack wrote:
>> So in D I have a struct like this:
>>
>>>struct ProcessResult
>>>{
>>>	string[] output;
>>>	bool ok;
>>>}
>>
>> in order to use output from C WINAPI with unicode, I need to 
>> convert each string to wchar* so that i can acess it from C 
>> with wchar_t*. Is that right or am I missing anything?
>>
>>
>>>struct ProcessResult
>>>{
>>>	string[] output;
>>>	bool ok;
>>>
>>>	C_ProcessResult toCResult()
>>>	{
>>>		auto r = C_ProcessResult();
>>>		r.ok = this.ok; // just copy, no conversion needed
>>>		foreach(s; this.output)
>>>			r.output ~= cast(wchar*)s.ptr;
>>>		return r;
>>>	}
>>>}
>>
>>>version(Windows) extern(C) export
>>>struct C_ProcessResult
>>>{
>>>	wchar*[] output;
>>>	bool ok;
>>>}
>
> I would just use std.encoding
>
> https://dlang.org/phobos/std_encoding.html
>
> and use transcode
>
> https://dlang.org/phobos/std_encoding.html#transcode

Forget previous post, I didn't see the arrays.

extern(C) has no knowledge of D arrays, I think you need to use 
wchar** instead of []. Keep in mind you need to store the lengths 
as well unless you use zero terminated strings.



More information about the Digitalmars-d-learn mailing list