On 09.12.20 21:35, Jack wrote:
I'm on linux/opensuse, trying to pass a wchar_* from C to D but I'm
getting only the first letter of that string. Could someone help figure
out why?
this is the piece of D code:
extern(C) export
void sayHello(const (wchar) *s)
[...]
and below the piece of C code where I call the lib's function, compiled
with clang -std=c11 -m64 dll.c -ldl
[...]
const wchar_t *s2 = L"hello!";
void (*fp)(const wchar_t*) = dlsym(lh, "sayHello");
char *de = dlerror();
if(de) {
fprintf(stderr, "slsym error:%s\n", de);
return EXIT_FAILURE;
}
fp(s2);
the output is "h" rather "hello". What am I missing?
D's wchar is not C's wchar_t. D's wchar is 16 bits wide. The width of
C's wchar_t is implementation-defined. In your case it's probably 32 bits.
Because of that size mismatch, sayHello sees your L"hello!" string as
"h\0e\0l\0l\0o\0!\0"w. And the conversion correctly stops at the first
null character.
My C isn't very good, but I think char_16t is the correct analog to D's
wchar. https://en.cppreference.com/w/c/string/multibyte/char16_t