[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

the initialization of the main program and its loading DLL

blackbiscuit

11/12/2008 5:06:00 AM

Dear All,

Now in my current developing project, I have defined a global object
in the main program . In dlls, there are also some static object whose
constructor will use the global object in the main program. But the
problem is always the initialization of the dll is prior to the main
program. How can I do if I wanna the main program be initialized
first?

Thank you very much!

Best,
Tony
5 Answers

red floyd

11/12/2008 6:20:00 AM

0

blackbiscuit wrote:
> Dear All,
>
> Now in my current developing project, I have defined a global object
> in the main program . In dlls, there are also some static object whose
> constructor will use the global object in the main program. But the
> problem is always the initialization of the dll is prior to the main
> program. How can I do if I wanna the main program be initialized
> first?
>

DLL initialization order is off-topic for this group. This group
discusses the C++ language as defined by ISO/IEC 14882:2003.

Please ask your question in a group with "microsoft" or "windows" in
its name. See FAQ 5.9 for a list of suggested newsgroups,
http://www.parashift.com/c++-faq-lite/how-to-post.ht...

blackbiscuit

11/12/2008 6:29:00 AM

0

On 11?12?, ??2?19?, red floyd <no.spam.h...@example.com> wrote:
> blackbiscuit wrote:
I am sorry. Thanks for your suggestion.

> > Dear All,
>
> > Now in my current developing project, I have defined a global object
> > in the main program . In dlls, there are also some static object whose
> > constructor will use the global object in the main program. But the
> > problem is always the initialization of the dll is prior to the main
> > program. How can I do if I wanna the main program be initialized
> > first?
>
> DLL initialization order is off-topic for this group. This group
> discusses the C++ language as defined by ISO/IEC 14882:2003.
>
> Please ask your question in a group with "microsoft" or "windows" in
> its name. See FAQ 5.9 for a list of suggested newsgroups,http://www.parashift.com/c++-faq-lite/how-to-post.ht...

Paavo Helde

11/12/2008 6:47:00 AM

0

blackbiscuit <infozyzhang@gmail.com> kirjutas:

> Dear All,
>
> Now in my current developing project, I have defined a global object
> in the main program . In dlls, there are also some static object whose
> constructor will use the global object in the main program. But the
> problem is always the initialization of the dll is prior to the main
> program. How can I do if I wanna the main program be initialized
> first?

The initialization order of global statics is badly defined in C++, there
are many threads in this NG discussing this. You can try to wrap the
static object in a function, then it should be initialized in the first
function call:

class A { ... };

A& GetAInstance() {
static A the_a;
return the_a;
}

As red floyd said the dll-s are not covered by the standard, so anything
related to them is implementation-defined. Typically the main exe depends
on a number of additional dll-s, and not vice versa as it appears to be
in your case. I am not even sure how you managed to compile it this way.
Yes you can easily build an extra DLL that depends on the exe, but then
it would need to be loaded explicitly by a LoadLibrary() call, and by
that time all statics in the exe and implicitly loaded dll-s should be
already initialized.

Paavo

Maxim Yegorushkin

11/12/2008 9:42:00 AM

0

On Nov 12, 5:06 am, blackbiscuit <infozyzh...@gmail.com> wrote:

> Now in my current developing project, I have defined a global object
> in the main program . In dlls, there are also some static object whose
> constructor will use the global object in the main program. But the
> problem is always the initialization of the dll is prior to the main
> program. How can I do if I wanna the main program be initialized
> first?

A standard portable solution for this problem is to have an explicit
initialisation (and often deinitialisation) function for your .dll
or .so. This function initialises the global state of the shared
library. Following this approach you don't depend on any platform
specific initialisation routines like DllMain or gcc constructor
functions.

Example:

// library.h
#include <memory>
#include <iosfwd>

struct Library
{
virtual ~Library() = 0;

virtual void foo() = 0;
virtual void bar() = 0;

// initialisation arguments
struct Args {
// arguments as needed, for example:
std::ostream* log;
};
// the initialisation routine / factory function
// only this functions needs to be exported
// (for .dll it should be __declspec(dllexport/import)
static std::auto_ptr<Library> open(Args const&);
};

// main application / library client
#include "library.h"
#include <iostream>

int main()
{
// open / initialise library
Library::Args lib_args = { &std::cout };
std::auto_ptr<Library> lib(Library::open(lib_args));
// use the library
lib->foo();
lib->bar();
// done with the library
// std::auto_ptr destructor calls Library::~Library
// which deinitialises the library
}

// library.cpp
#include <ostream>
#include "library.h"

namespace {

struct LibraryImpl : Library
{
Args args; // library global state

LibraryImpl(Args a) : args(a)
{
// do required initialisation here
*args.log << "Library has been initialised\n";
}

~LibraryImpl()
{
// do required deinitialisation here
*args.log << "Library has been deinitialised\n";
}

void foo() { *args.log << "Library::foo()\n"; }
void bar() { *args.log << "Library::bar()\n"; }
};

}

Library::~Library() {}

std::auto_ptr<Library> Library::open(Args const& args)
{
return std::auto_ptr<Library>(new LibraryImpl(args));
}

--
Max

James Kanze

11/12/2008 3:10:00 PM

0

On Nov 12, 6:06 am, blackbiscuit <infozyzh...@gmail.com> wrote:

> Now in my current developing project, I have defined a global
> object in the main program . In dlls, there are also some
> static object whose constructor will use the global object in
> the main program. But the problem is always the initialization
> of the dll is prior to the main program. How can I do if I
> wanna the main program be initialized first?

I'm not sure I understand. Are the DLL's being loaded
explicitly, or implicitly? (It's generally a good idea to avoid
DLL's except when necessary. For user written DLL's, this
really only occurs when the DLL's are loaded explicitly.)

The initializers in a DLL won't be called until the DLL is
loaded, so the simple answer is not to load it until after
you've entered main. If for some reason, explicit loading isn't
an option, or it must occur before entering main, then the
problem is basically the same as with static linking; some
variant of the singleton pattern is the usual solution.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34