digitalmars.D - Writing a library
- Mn (7/7) Feb 16 2007 Hello World!
- Jarrett Billingsley (9/20) Feb 16 2007 No other languages understand D calling or mangling conventions, but D c...
- Mn (10/39) Feb 16 2007 Thanks for your reply.
- David Gileadi (12/40) Feb 16 2007 I tried this very thing about a year ago, creating a DLL in D with
- Mn (4/46) Feb 16 2007 You said you wrote a DLL in D and tried to access it via C#, ok. But wha...
- David Gileadi (6/54) Feb 16 2007 Maybe "host" wasn't the best choice of words. I meant the .exe which
- Mn (2/31) Feb 16 2007 Is it possible to use the COM with D?
- BCS (3/6) Feb 16 2007 http://www.digitalmars.com/d/interface.html
- Frits van Bommel (18/19) Feb 16 2007 Yes. The last section of http://www.digitalmars.com/d/interface.html :
- Mn (5/34) Feb 16 2007 So if I want to create an instance of a class, the instance being create...
- Bill Baxter (39/76) Feb 16 2007 I think what you need to do is basically to mimic the type of interface
- Jarrett Billingsley (22/28) Feb 16 2007 The GC should work normally, but one thing you have to do is to make sur...
- Bill Baxter (6/39) Feb 16 2007 Ok, I wasn't sure about that. But it makes sense now. D memory
- Sean Kelly (9/16) Feb 16 2007 And please be aware that the necessary sequence of function calls is
- Mn (2/4) Feb 17 2007 *complain*
- Mn (3/17) Feb 17 2007 The function wrapper goes inside of the class too, doesnt it?
- Mn (4/22) Feb 17 2007 But now if I somehow call a create()-function (or however i call it) it ...
- Frits van Bommel (4/27) Feb 17 2007 It's a static variable in a DLL, so it stays in memory from the time the...
- Mn (4/15) Feb 17 2007 Just a pointer for myself:
Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. Greetings and thank you. -- Mn
Feb 16 2007
"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Jarrett Billingsley Wrote:"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...Thanks for your reply. But if I now want - for example - to have three functions: openSomething(...) { connection c = pointer to an opened file } doSomething(...) { do something with c } closeSomething(...) { close connection c } How do I pass the c between these three functions? How do I ensure that c stays alive until I call closeSomething(...), and then how do I remove it? Next problem (?) is that it has to be possible to call openSomething multiple times, use doSomething with the right c multiple times and then close all the c. Sure i can just return a pointer from openSomething and pass it over to doSomething and closeSomething - but to what shall this pointer point? And where to store that? I need something persistant in the lib. How to do this? Thanks for your help. -- MnHello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Jarrett Billingsley wrote:"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...I tried this very thing about a year ago, creating a DLL in D with and D side. It worked great when called via a D host, but would what was going on (and since it was a school project I just gave up and collector running in a separate thread, and at the time the calls to disable/enable the GC didn't do anything, so I didn't get to test if my suspicion was true. I'd be very interested to hear of your success with this.Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
David Gileadi Wrote:Jarrett Billingsley wrote:will post a seperate reply for this topic. As far as I know there is a way to disable GC for parts of or the whole program. I will of course continue to look into this, but as of now I just started with D. Although it looks wonderful and its steep learning curve should be conquered in zero time."Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...I tried this very thing about a year ago, creating a DLL in D with and D side. It worked great when called via a D host, but would what was going on (and since it was a school project I just gave up and collector running in a separate thread, and at the time the calls to disable/enable the GC didn't do anything, so I didn't get to test if my suspicion was true. I'd be very interested to hear of your success with this.Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Mn wrote:David Gileadi Wrote:Maybe "host" wasn't the best choice of words. I meant the .exe which the "host". I also had a program written in D to call the code in the this is not likely possible, at least not without some massive effort.Jarrett Billingsley wrote:will post a seperate reply for this topic. As far as I know there is a way to disable GC for parts of or the whole program. I will of course continue to look into this, but as of now I just started with D. Although it looks wonderful and its steep learning curve should be conquered in zero time."Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...I tried this very thing about a year ago, creating a DLL in D with and D side. It worked great when called via a D host, but would what was going on (and since it was a school project I just gave up and collector running in a separate thread, and at the time the calls to disable/enable the GC didn't do anything, so I didn't get to test if my suspicion was true. I'd be very interested to hear of your success with this.Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Jarrett Billingsley Wrote:"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...Is it possible to use the COM with D?Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Mn wrote:Is it possible to use the COM with D?http://www.digitalmars.com/d/interface.html at the bottom
Feb 16 2007
Mn wrote:Is it possible to use the COM with D?Yes. The last section of http://www.digitalmars.com/d/interface.html : --- COM Interfaces A variant on interfaces is the COM interface. A COM interface is designed to map directly onto a Windows COM object. Any COM object can be represented by a COM interface, and any D object with a COM interface can be used by external COM clients. A COM interface is defined as one that derives from the interface std.c.windows.com.IUnknown. A COM interface differs from a regular D interface in that: * It derives from the interface std.c.windows.com.IUnknown. * It cannot be the argument of a DeleteExpression. * References cannot be upcast to the enclosing class object, nor can they be downcast to a derived interface. To accomplish this, an appropriate QueryInterface() would have to be implemented for that interface in standard COM fashion. ---
Feb 16 2007
Jarrett Billingsley Wrote:"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...So if I want to create an instance of a class, the instance being created outside but the class being inside the lib, and then the class staying alive until I somehow get rid of it (calling a function of the class) - is this possible? Short: is it possible to export a class, not only its functions? If yes, how? Thanks. -- MnHello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
Mn wrote:Jarrett Billingsley Wrote:I think what you need to do is basically to mimic the type of interface used by C FILE operations, if you are familiar with that. So you will need a create() call that returns a MY_CLASS, which is actually just a void pointer. In fact it will be a pointer to your class object, but from C land all they will know is that it's a void pointer. So it will look something like: alias void* MY_CLASS; extern(C) MY_CLASS createMyClass() { return new MyClass; } Every method of your class like void myMethod(float arg); will need a plain function wrapper like extern(C) void myClassMyMethod(MY_CLASS self, float arg); And they'll all be implemented mostly like extern(C) void myClassMyMethod(MY_CLASS self, float arg) { MyClass real_self = cast(MyClass)self; // [check that it really is a MyClass] real_self.myMethod(arg); } And you will need a destroy call that takes a MY_CLASS, too, and calls the destructor. You also have to do something about any arguments or return values that are not C types and convert them accordingly. For instance if you have a method that returns a 'real' the C wrapper needs to cast it to double. I'm not sure if the GC will work normally or not. But I'm pretty sure you'll need to do something to startup and initialize the GC on the D side. Check out WinMain in http://www.dsource.org/projects/minwin/browser/trunk/minwin/app.d at line 144. In particular the calls to gc_init(), _minit(), _moduleCtor() and _moduleUnitTests(). Calling D from C/C++ is not something I have not done either, but will probably want to do at some point. So I'm also very interested in hearing about whether you succeed. And the above is what I was planning to try when I get to that point. If you're making a DLL out of the D code, then all the functions should be marked 'export' also so that they get marked as callable in the DLL. --bb"Mn" <mn mailinator.com> wrote in message news:er492f$1sti$1 digitalmars.com...So if I want to create an instance of a class, the instance being created outside but the class being inside the lib, and then the class staying alive until I somehow get rid of it (calling a function of the class) - is this possible? Short: is it possible to export a class, not only its functions? If yes, how? Thanks. -- MnHello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. PHP. Greetings and thank you. -- MnNo other languages understand D calling or mangling conventions, but D can make functions with C, Windows, and Pascal calling conventions. If you just do something like: extern(C) export void func(int x) { ... } You can then, maybe, make a DLL or something out of it which can be called from virtually any other mainstream language, since most things understand the C calling convention.
Feb 16 2007
"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:er58l2$i21$1 digitalmars.com...I'm not sure if the GC will work normally or not. But I'm pretty sure you'll need to do something to startup and initialize the GC on the D side. Check out WinMain in http://www.dsource.org/projects/minwin/browser/trunk/minwin/app.d at line 144. In particular the calls to gc_init(), _minit(), _moduleCtor() and _moduleUnitTests().The GC should work normally, but one thing you have to do is to make sure you keep references to any GC memory (i.e. anything allocated with 'new' in your D code) in the DLL. One way of doing this for classes is to keep a static list or AA of all instances of the class in the class: class ExportedClass { static bool[ExportedClass] instances; this() { .... instances[this] = true; } ~this() { instances.remove(this); } } This way, there is at least one reference to each class instance held in the DLL, so that the instances won't get collected. When the instances are deleted, they are removed from the list.
Feb 16 2007
Jarrett Billingsley wrote:"Bill Baxter" <dnewsgroup billbaxter.com> wrote in message news:er58l2$i21$1 digitalmars.com...Ok, I wasn't sure about that. But it makes sense now. D memory allocated with 'new' is in some big D gc pool, and that's the only memory the GC scans for outstanding references. Well, that and stuff on the D stack. Anything in C land won't be noticed by the GC. Makes sense. --bbI'm not sure if the GC will work normally or not. But I'm pretty sure you'll need to do something to startup and initialize the GC on the D side. Check out WinMain in http://www.dsource.org/projects/minwin/browser/trunk/minwin/app.d at line 144. In particular the calls to gc_init(), _minit(), _moduleCtor() and _moduleUnitTests().The GC should work normally, but one thing you have to do is to make sure you keep references to any GC memory (i.e. anything allocated with 'new' in your D code) in the DLL. One way of doing this for classes is to keep a static list or AA of all instances of the class in the class: class ExportedClass { static bool[ExportedClass] instances; this() { .... instances[this] = true; } ~this() { instances.remove(this); } } This way, there is at least one reference to each class instance held in the DLL, so that the instances won't get collected. When the instances are deleted, they are removed from the list.
Feb 16 2007
Bill Baxter wrote:I'm not sure if the GC will work normally or not. But I'm pretty sure you'll need to do something to startup and initialize the GC on the D side. Check out WinMain in http://www.dsource.org/projects/minwin/browser/trunk/minwin/app.d at line 144. In particular the calls to gc_init(), _minit(), _moduleCtor() and _moduleUnitTests().And please be aware that the necessary sequence of function calls is different for GDC. Fixing this has been on my "to do" list for Tango since the project began, and it really all just comes down to finding an appropriate pair of function names. They will most likely be cr_init() and cr_term(), and will call the normal runtime initialization and termination process. I think I haven't gotten around to it yet simply because no one has complained about it :-) Sean
Feb 16 2007
Sean Kelly Wrote:I think I haven't gotten around to it yet simply because no one has complained about it :-)*complain*
Feb 17 2007
Bill Baxter Wrote:Every method of your class like void myMethod(float arg); will need a plain function wrapper like extern(C) void myClassMyMethod(MY_CLASS self, float arg); And they'll all be implemented mostly like extern(C) void myClassMyMethod(MY_CLASS self, float arg) { MyClass real_self = cast(MyClass)self; // [check that it really is a MyClass] real_self.myMethod(arg); } And you will need a destroy call that takes a MY_CLASS, too, and calls the destructor.The function wrapper goes inside of the class too, doesnt it? --Mn
Feb 17 2007
Jarrett Billingsley Wrote: One way of doing this for classes is to keep astatic list or AA of all instances of the class in the class: class ExportedClass { static bool[ExportedClass] instances; this() { .... instances[this] = true; } ~this() { instances.remove(this); } }But now if I somehow call a create()-function (or however i call it) it creates a class instance and returns it. The class adds a reference to that instance to its instances list. But then we return from that create() function to the program outside of the lib/dll - and the instances list? Doesnt it get removed by GC? How long does it (the instances list) stay in memory (or wherever it is)? From first call to OS shutdown? Or to host app shutdown? Or to ________ ? --Mn
Feb 17 2007
Mn wrote:Jarrett Billingsley Wrote: One way of doing this for classes is to keep aIt's a static variable in a DLL, so it stays in memory from the time the DLL is loaded to when it's unloaded (which may be at application shutdown if it's never explicitly unloaded).static list or AA of all instances of the class in the class: class ExportedClass { static bool[ExportedClass] instances; this() { .... instances[this] = true; } ~this() { instances.remove(this); } }But now if I somehow call a create()-function (or however i call it) it creates a class instance and returns it. The class adds a reference to that instance to its instances list. But then we return from that create() function to the program outside of the lib/dll - and the instances list? Doesnt it get removed by GC? How long does it (the instances list) stay in memory (or wherever it is)? From first call to OS shutdown? Or to host app shutdown? Or to ________ ?
Feb 17 2007
Mn Wrote:Hello World! Is it possible to write a library in D that can be used by other programming languages? And if yes, how to do it? I can think of two ways of "using" a lib in general: 1. The OOP way: use a class of the lib, then its functions, dunno how its called. Greetings and thank you. -- MnJust a pointer for myself: http://www.digitalmars.com/d/dll.html DLLs are for Windows afaik, what do I use under Linux?
Feb 17 2007