www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Where to use "new" in a DLL?

reply Gilles G. <schaouette free.fr> writes:
Hello,
I am new to system programmation so, please, excuse my ignorance...
I am writing a DLL and I would like to have a class that would be a DllManager.
Then, I export some of the functions using extern(Windows) and a .def file.
The problem is that I don't now where I can create a new DllManager.
For example:
class DllManager
{
public:
        this(){}
        ~this(){}
        void sayHello(){writefln("Hello!");}
}
DllManager manager = new DllManager();
extern(Windows) void DllSayHello(){manager.sayHello();}

With this code I get the error:
Error: non-constant expression new DllManager

Thank you for your help.
--
Gilles
Jun 25 2007
parent reply "Chris P." <chrisp inventivedingo.com> writes:
Gilles G. wrote:
 Hello,
 I am new to system programmation so, please, excuse my ignorance...
 I am writing a DLL and I would like to have a class that would be a
DllManager. Then, I export some of the functions using extern(Windows) and a
.def file.
 The problem is that I don't now where I can create a new DllManager.
 For example:
 class DllManager
 {
 public:
         this(){}
         ~this(){}
         void sayHello(){writefln("Hello!");}
 }
 DllManager manager = new DllManager();
 extern(Windows) void DllSayHello(){manager.sayHello();}
 
 With this code I get the error:
 Error: non-constant expression new DllManager
 
 Thank you for your help.
 --
 Gilles
Hi Gilles, You have to use "new" from within a function; it won't work at global scope like you've tried. The DLL entry point (DllMain) is probably the most appropriate place in which to initialise the "manager" pointer. I imagine static this() would also work: DllManager manager; // static this() is called on module init. static this() { manager = new DllManager(); } --Chris P.
Jun 25 2007
parent Gilles G. <schaouette free.fr> writes:
Whao!
Thanks a lot, you really saved my day!
Regards.
--
Gilles

Chris P. Wrote:

 Gilles G. wrote:
 Hello,
 I am new to system programmation so, please, excuse my ignorance...
 I am writing a DLL and I would like to have a class that would be a
DllManager. Then, I export some of the functions using extern(Windows) and a
.def file.
 The problem is that I don't now where I can create a new DllManager.
 For example:
 class DllManager
 {
 public:
         this(){}
         ~this(){}
         void sayHello(){writefln("Hello!");}
 }
 DllManager manager = new DllManager();
 extern(Windows) void DllSayHello(){manager.sayHello();}
 
 With this code I get the error:
 Error: non-constant expression new DllManager
 
 Thank you for your help.
 --
 Gilles
Hi Gilles, You have to use "new" from within a function; it won't work at global scope like you've tried. The DLL entry point (DllMain) is probably the most appropriate place in which to initialise the "manager" pointer. I imagine static this() would also work: DllManager manager; // static this() is called on module init. static this() { manager = new DllManager(); } --Chris P.
Jun 25 2007