www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Extern function inside a class?

reply Gilles G. <schaouette free.fr> writes:
Hello,
I am writing a DLL using D. Iwould like to group the functions that I export
together in a class which would also keep the data.
For example:
class DllManager
{
        this(){}
        ~this(){}
        invariant int dllVersion 1;
        MyObject[] objectList;
        extern(Windows) int GetDllVersion() { return dllVersion;}
        extern(Windows) int NewObject()
        {
                 objectList.length = objectList.length+1;
                 return objectList.length;
        }
}
But I wonder if this is possible, because there is no instance of the class
DllManager available. What happens when the function NewObject is called by the
main program?

I guess the good way to do this is:
class DllManager
{
        this(){}
        ~this(){}
        invariant int dllVersion 1;
        MyObject[] objectList;
        int GetDllVersion() { return dllVersion;}
        int NewObject()
        {
                 objectList.length = objectList.length+1;
                 return objectList.length;
        }
}
DllManager manager;
extern(Windows) int GetDllVersion() {return manager.getDllVersion(); }
extern(Windows) int NewObject() {return manager.NewObject();}

But it looks like I am forced to write all the external calls twice...

Anybody for a "cleaner" solution?
Thanks!
Jun 23 2007
parent Daniel Keep <daniel.keep.lists gmail.com> writes:
Gilles G. wrote:
 Hello,
 I am writing a DLL using D. Iwould like to group the functions that I export
together in a class which would also keep the data.
 For example:
 class DllManager
 {
         this(){}
         ~this(){}
         invariant int dllVersion 1;
         MyObject[] objectList;
         extern(Windows) int GetDllVersion() { return dllVersion;}
         extern(Windows) int NewObject()
         {
                  objectList.length = objectList.length+1;
                  return objectList.length;
         }
 }
 But I wonder if this is possible, because there is no instance of the class
DllManager available. What happens when the function NewObject is called by the
main program?
 
 I guess the good way to do this is:
 class DllManager
 {
         this(){}
         ~this(){}
         invariant int dllVersion 1;
         MyObject[] objectList;
         int GetDllVersion() { return dllVersion;}
         int NewObject()
         {
                  objectList.length = objectList.length+1;
                  return objectList.length;
         }
 }
 DllManager manager;
 extern(Windows) int GetDllVersion() {return manager.getDllVersion(); }
 extern(Windows) int NewObject() {return manager.NewObject();}
 
 But it looks like I am forced to write all the external calls twice...
 
 Anybody for a "cleaner" solution?
 Thanks!
The problem with your second example is that it doesn't work, either. "DllManager manager;" does *NOT* create an instance of DllManager: it simply defines a variable that can point to one. The question is: why do you want to use a class? I mean, classes aren't required in D, and you can't export classes through DLLs, so using a class doesn't gain you anything. Judging from how you're using it, you could easily just not use a class at all, and use globals. Then you wouldn't need to wrap all the calls. It's not like using globals will cause the world to explode, or anything :) -- Daniel
Jun 23 2007