D-DLLs & Python

Chris wendlec at tcd.ie
Tue Feb 19 08:23:44 PST 2013


I have written a DLL that I load into a Python program. 
Everything works fine (DLL is loaded via ctypes, functions can be 
called and are executed). Only the string handling is giving me a 
bit of a headache. The string in D is always complete garbage. I 
have written Python modules in D (with C wrappers) before and got 
them to work. But the DLL in D seems to be a completely different 
beast. Does anyone have experience with it?

Python uses ctypes, e.g.

myDLL = CDLL("myDLL") / WinDLL("myDLL")
myDLL.printThis(c_char_p("Hello world")) / 
myDLL.printThis(create_string_buffer("Hello world"))

The D side looks like like this:

export void printThis(ref char[] str) {
    printf("%s\n", str); // prints "Hello World"
    writeln(str); // prints garbage
}

OR

export void printThis(char* str) {
     printf("%s\n", str); // prints garbage
     writeln(str); // prints garbage
}

What am I doing wrong / missing here? I guess it has something to 
do with the pointers.

Thanks!

prin


More information about the Digitalmars-d-learn mailing list