exporting function from betterc to windows dll

Mike Parker aldacron at gmail.com
Sun Mar 15 03:22:39 UTC 2020


On Saturday, 14 March 2020 at 20:53:45 UTC, Abby wrote:
> I would like to export some functions from my bettec dll for 
> dotnet core application in windows.
>
> Right now I have compiled dll using dmd v2.091.0-dirty simply 
> by ´dub build´
>
> this is the function I have
>
> extern(C) char* test_echo(const(char)* line, size_t len,  ref 
> size_t resLen)
> {
>     enum format = "{\"message\": \"%s\"}\n";
>
>     auto response = cast(char*)malloc(format.length + len);
>     resLen = sprintf(response, format, line);
>     return response;
> }
>
> and this is my dotnet core equivalent
>
> [DllImport(DllName, CallingConvention = 
> CallingConvention.StdCall)]
> static extern IntPtr 
> test_echo([MarshalAs(UnmanagedType.LPStr)]string line, ulong 
> len, out ulong resLen);
>
> This works for me in linux but does not in windows, any idea 
> what I'm doing wrong?

For one thing, you're defining it with stdcall in dotnet, but 
cdecl in the code. You should change the convention on the dotnet 
side to CallingConvention.Cdecl. Or, if you really want to use 
stdcall, use extern(System) in D -- the equivalent of 
extern(Windows) on Windows and extern(C) everywhere else.

Also, I would expect you'd have to use export [1] on the D side 
for the symbol to be loadable from the dll:

extern(C) export char* test_echo(...) { ... }

This should be the equivalent of __declspec(dllexport) for C and 
C++ on Windows.

[1] https://dlang.org/spec/attribute.html#visibility_attributes 
(Item #7)




More information about the Digitalmars-d-learn mailing list