Thread overview
Where to use "new" in a DLL?
Jun 25, 2007
Gilles G.
Jun 25, 2007
Chris P.
Jun 25, 2007
Gilles G.
June 25, 2007
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
June 25, 2007
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.
June 25, 2007
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.