Writing a library

Mn mn at mailinator.com
Fri Feb 16 06:03:11 PST 2007


Jarrett Billingsley Wrote:

> "Mn" <mn at mailinator.com> wrote in message 
> news:er492f$1sti$1 at 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



More information about the Digitalmars-d mailing list