www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Had not Dllimport in D?

reply xky <mozirikan gmail.com> writes:
First, I really sorry my bad english.

I just want to using Ruby dll file(msvcrt-ruby220.dll).
In the case of C#,
================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using INT = System.Int32;

namespace RubyTest01
{
   static class Ruby
   {
     public const string RubyDll = "msvcrt-ruby18";

     [DllImport(RubyDll)]
     public static extern void ruby_init();

     [DllImport(RubyDll)]
     public static extern INT rb_eval_string_protect(byte[] 
script, ref INT state);

      public static INT rb_eval_string_protect(string script, ref 
INT state)
      {
         return 
rb_eval_string_protect(Encoding.UTF8.GetBytes(script + '\0'), ref 
state);
     }

}

class Program
{
   static void Main(string[] args)
   {
     INT state = 0;
     Ruby.ruby_init();
     Ruby.rb_eval_string_protect("open('test.txt', 'w') {| fp| 
fp.write(\"Hello World!\\n\")}", ref state);

   }
}
================================================================================

I guess D had something like it, but i don't know how to do it.
Thx!
Mar 06 2016
next sibling parent reply WebFreak001 <janju007 web.de> writes:
On Sunday, 6 March 2016 at 14:12:35 UTC, xky wrote:
 First, I really sorry my bad english.

 I just want to using Ruby dll file(msvcrt-ruby220.dll).
D can import and use functions from a DLL but you are using a C# library, which is probably not going to work. D can only call functions from DLLs with a C or D interface. If you want to make your own shared libraries or use some C DLLs check these links: http://wiki.dlang.org/Win32_DLLs_in_D (native .dll -> windows) https://dlang.org/dll-linux.html (shared object .so -> linux) https://github.com/DerelictOrg/DerelictUtil (library for loading dll functions on multiple platforms)
Mar 06 2016
parent xky <mozirikan gmail.com> writes:
On Sunday, 6 March 2016 at 14:20:34 UTC, WebFreak001 wrote:
 On Sunday, 6 March 2016 at 14:12:35 UTC, xky wrote:
 First, I really sorry my bad english.

 I just want to using Ruby dll file(msvcrt-ruby220.dll).
D can import and use functions from a DLL but you are using a C# library, which is probably not going to work. D can only call functions from DLLs with a C or D interface. If you want to make your own shared libraries or use some C DLLs check these links: http://wiki.dlang.org/Win32_DLLs_in_D (native .dll -> windows) https://dlang.org/dll-linux.html (shared object .so -> linux) https://github.com/DerelictOrg/DerelictUtil (library for loading dll functions on multiple platforms)
DerelictUtil looks like useful for me. Thank you. :-)
Mar 06 2016
prev sibling parent reply Rikki Cattermole <alphaglosined gmail.com> writes:
Okay, since you clearly have not worked in a native language before, 
lets start from scratch.

You want to make some bindings to a shared library called "msvcrt-ruby18".
 From this I know that it is using the Microsoft Visual C runtime. That 
means you must build D using this as well.
This is done via the -ms32coff switch for 32bit and -m64 for 64bit 
(which is easier all up).
You will need Visual studio with all of the c/c++ stuff installed.
Keep in mind you must match the version of Visual C runtime up with the 
shared libraries one (if you fail enjoy the segfaults!).

Lastly you want some bindings. I won't cover that here. But this should 
help ya out http://dlang.org/spec/interfaceToC.html

After this you should be able to pass the shared library directly to dmd 
when in -ms32coff or -m64 mode and it'll all work.
Mar 06 2016
parent reply xky <mozirikan gmail.com> writes:
On Sunday, 6 March 2016 at 14:21:55 UTC, Rikki Cattermole wrote:
 Okay, since you clearly have not worked in a native language 
 before, lets start from scratch.

 [...]
Sorry for my idiot question. Thanks.
Mar 06 2016
parent Rikki Cattermole <alphaglosined gmail.com> writes:
On 07/03/16 4:19 AM, xky wrote:
 On Sunday, 6 March 2016 at 14:21:55 UTC, Rikki Cattermole wrote:
 Okay, since you clearly have not worked in a native language before,
 lets start from scratch.

 [...]
Sorry for my idiot question. Thanks.
Not idiot, its just where you are at and that's ok :) Sorry for making you feel like that though.
Mar 06 2016