February 16, 2007
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. 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.

I am only interested in the more popular languages like C, C++, Java, C#, PHP.

Greetings and thank you.
-- Mn
February 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.
> 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
>
> I am only interested in the more popular languages like C, C++, Java, C#, PHP.
>
> Greetings and thank you.
> -- Mn

No 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.


February 16, 2007
Jarrett Billingsley Wrote:

> "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.
> > 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
> >
> > I am only interested in the more popular languages like C, C++, Java, C#, PHP.
> >
> > Greetings and thank you.
> > -- Mn
> 
> No 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.
> 
> 

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.
-- Mn
February 16, 2007
Jarrett Billingsley wrote:
> "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.
>> 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
>>
>> I am only interested in the more popular languages like C, C++, Java, C#, PHP.
>>
>> Greetings and thank you.
>> -- Mn
> 
> No 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. 
> 

I tried this very thing about a year ago, creating a DLL in D with extern(C) functions which was imported and called by a C# program.  It passed data back and forth via simple structs, defined on both the C# and D side.  It worked great when called via a D host, but would segfault every time I tried it from the C# host.  I never did figure out what was going on (and since it was a school project I just gave up and ported the code to C#).  I suspected it was the fault of the garbage 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.
February 16, 2007
David Gileadi Wrote:

> Jarrett Billingsley wrote:
> > "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.
> >> 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
> >>
> >> I am only interested in the more popular languages like C, C++, Java, C#, PHP.
> >>
> >> Greetings and thank you.
> >> -- Mn
> > 
> > No 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.
> > 
> 
> I tried this very thing about a year ago, creating a DLL in D with extern(C) functions which was imported and called by a C# program.  It passed data back and forth via simple structs, defined on both the C# and D side.  It worked great when called via a D host, but would segfault every time I tried it from the C# host.  I never did figure out what was going on (and since it was a school project I just gave up and ported the code to C#).  I suspected it was the fault of the garbage 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.

You said you wrote a DLL in D and tried to access it via C#, ok. But what do you mean with "D host" and "C# host" then?? Accessing a .NET Assembly is unlikely to be possible that easy from D, although C# offers COM support. I 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.
February 16, 2007
Jarrett Billingsley Wrote:

> "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.
> > 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
> >
> > I am only interested in the more popular languages like C, C++, Java, C#, PHP.
> >
> > Greetings and thank you.
> > -- Mn
> 
> No 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.
> 
> 

Is it possible to use the COM with D?
February 16, 2007
Mn wrote:
> 
> 
> Is it possible to use the COM with D?

http://www.digitalmars.com/d/interface.html

at the bottom
February 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.
---
February 16, 2007
Mn wrote:
> David Gileadi Wrote:
> 
>> Jarrett Billingsley wrote:
>>> "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.
>>>> 2. The Un-OOP way: use a function of a lib, its called P/Invoke in C#.
>>>>
>>>> I am only interested in the more popular languages like C, C++, Java, C#, PHP.
>>>>
>>>> Greetings and thank you.
>>>> -- Mn
>>> No 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. 
>>>
>> I tried this very thing about a year ago, creating a DLL in D with extern(C) functions which was imported and called by a C# program.  It passed data back and forth via simple structs, defined on both the C# and D side.  It worked great when called via a D host, but would segfault every time I tried it from the C# host.  I never did figure out what was going on (and since it was a school project I just gave up and ported the code to C#).  I suspected it was the fault of the garbage 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.
> 
> You said you wrote a DLL in D and tried to access it via C#, ok. But what do you mean with "D host" and "C# host" then?? Accessing a .NET Assembly is unlikely to be possible that easy from D, although C# offers COM support. I 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.

Maybe "host" wasn't the best choice of words.  I meant the .exe which loads the DLL and calls the code--i.e. in this case the C# program was the "host".  I also had a program written in D to call the code in the DLL.  I never tried to load a DLL written in C# from D code--as you say, this is not likely possible, at least not without some massive effort.
Top | Discussion index | About this forum | D home