Where to use "new" in a DLL?

Chris P. chrisp at inventivedingo.com
Mon Jun 25 02:38:56 PDT 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.


More information about the Digitalmars-d-learn mailing list