where is the memory corruption?

Jack jckj33 at gmail.com
Wed Dec 9 20:35:21 UTC 2020


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)
{
	import std.stdio : writeln;
	import std.conv : to;

	import core.runtime : rt_init, rt_term;
	rt_init();
	scope(exit) rt_term();

	writeln("+sayHello()");
	auto s2 = to!string(s);
	writeln("s2 = ", s2);
	writeln("-sayHello()");
}

build with dub, using "targetType": "dynamicLibrary" in dub.json.

and below the piece of C code where I call the lib's function, 
compiled with clang -std=c11 -m64 dll.c -ldl


   const char *libpath = "path/to/library.so";
   void *lh = dlopen(libpath, RTLD_LAZY);
   if(!lh) {
     fprintf(stderr, "dlopen error: %s\n", dlerror());
     return EXIT_FAILURE;
   }

   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?


More information about the Digitalmars-d-learn mailing list