Thread overview |
---|
June 25, 2007 Where to use "new" in a DLL? | ||||
---|---|---|---|---|
| ||||
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 Re: Where to use "new" in a DLL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Gilles G. | 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 Re: Where to use "new" in a DLL? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Chris P. | 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.
|
Copyright © 1999-2021 by the D Language Foundation