the initialization of the main program and its loading DLL

B

blackbiscuit

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
 
R

red floyd

blackbiscuit said:
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.html#faq-5.9
 
M

Maxim Yegorushkin

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::eek:stream* 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::eek:pen(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::eek:pen(Args const& args)
{
return std::auto_ptr<Library>(new LibraryImpl(args));
}
 
J

James Kanze

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top