February 20, 2013
On Wednesday, 20 February 2013 at 14:36:56 UTC, bearophile wrote:
> Chris:
>
>> extern (C) {export void printThis(char* str);}
>
> Maybe the D wiki should contain info to save some time and experiments to people.

I agree and I am glad that the people on this forum are always willing to help. I will soon be able to write a book about interfacing to D (via C) from various languages, ha ha!

> Possible alternative syntax:
>
> extern(C) export void printThis(char* str);
>
> Also, think if you want some const:
>
> extern(C) export void printThis(const(char)* str);

Yep, const is next on my list.

By the way, Python could call the functions in the DLL _without_ extern (C). The name would be mangled though, e.g.:

lib.D8abairDLL10printThisFxPaZv(...)

It wasn't until arguments were passed to D that the problems kicked in.

February 20, 2013
On Wednesday, 20 February 2013 at 14:43:06 UTC, John Colvin wrote:
> On Wednesday, 20 February 2013 at 14:28:06 UTC, Chris wrote:
>> On Wednesday, 20 February 2013 at 14:05:40 UTC, Mike Parker wrote:
>>> Your function is being called from Python, correct? Then in addition to the extern(C), the argument needs to be a char*, not a D array or a reference to one.
>>
>> Correct, and it works _now_*! The lines
>>
>> printf("Incoming printf: %s\n", str);
>> writefln("writefln %s", to!string(str));
>>
>> Print now:
>>
>> Incoming printf: Hello from Python
>> writefln Hello from Python
>>
>>
>> So the correct signature is
>>
>> extern (C) {export void printThis(char* str);}
>>
>> Thanks you guys! I'm so glad I don't have to write a C-wrapper!
>>
>> *("_now_" because I tried char* with extern (C) before as I would in my other Python modules, but it didn't work for some reason. Must have overlooked something. Mea culpa!).
>
> yeah, although extern(C) doesn't have to have any braces.

You are right of course!

>
> I've never had to use export before, but then that could be a .so/.dylib vs .dll thing

"export" is used in the DLL how-to. I never had to use it for my .so/.dylib modules either. I am new to Windows and it is a very strange beast. Like cooking without pots and pans.
1 2
Next ›   Last »