digitalmars.D.learn - exporting function from betterc to windows dll
- Abby (24/24) Mar 14 2020 I would like to export some functions from my bettec dll for
- User (2/5) Mar 14 2020 It is the calling convention.
- Mike Parker (13/34) Mar 14 2020 For one thing, you're defining it with stdcall in dotnet, but
- Ahmet Sait (5/7) Mar 15 2020 You can check my library documentation to understand how to do
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
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
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
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