www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - exporting function from betterc to windows dll

reply Abby <loniro7678 twit-mail.com> writes:
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?

This is the output from dotnet core

Unhandled exception. System.EntryPointNotFoundException: Unable 
to find an entry point named 'test_echo' in DLL 'test.dll'
Mar 14 2020
next sibling parent User <user user.com> writes:
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.

 [...]
It is the calling convention.
Mar 14 2020
prev sibling next sibling parent Mike Parker <aldacron gmail.com> writes:
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
Mar 14 2020
prev sibling parent Ahmet Sait <nightmarex1337 hotmail.com> writes:
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.
You can check my library documentation to understand how to do https://github.com/ahmetsait/IDL/wiki Should probably write a detailed blog post about that some time :/
Mar 15 2020