Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > the initialization of the main program and its loading DLL

Reply
Thread Tools

the initialization of the main program and its loading DLL

 
 
blackbiscuit
Guest
Posts: n/a
 
      11-12-2008
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
 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      11-12-2008
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-lit...t.html#faq-5.9
 
Reply With Quote
 
 
 
 
blackbiscuit
Guest
Posts: n/a
 
      11-12-2008
On 11$B7n(B12$BF|(B, $B2<8a(B2$B;~(B19$BJ,(B, 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-lit...t.html#faq-5.9


 
Reply With Quote
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      11-12-2008
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: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: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:pen(Args const& args)
{
return std::auto_ptr<Library>(new LibraryImpl(args));
}

--
Max

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      11-12-2008
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:
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 14 04-03-2010 10:08 AM
Its a bird, its a plane, its.. um, an Attribute based System? thunk Ruby 0 04-01-2010 10:25 PM
Its a bird, its a plane, no ummm, its a Ruide thunk Ruby 1 03-30-2010 11:10 AM
msvcrt.dll, msvcirt.dll, msvcrt20.dll and msvcrt40.dll, explanation please! Snoopy NZ Computing 16 08-25-2003 12:34 PM



Advertisments