467,134 Members | 1,035 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,134 developers. It's quick & easy.

Execution of a function on startup

Hello NG,

If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the mechanism
to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)

Thank you!

Dave
Jul 22 '05 #1
  • viewed: 1921
Share:
11 Replies
Dave wrote:
If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the mechanism
to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)


The more _sure_ alternative to call that function _upon_loading_ of that
dynamic library would be to call it from the dynamic library _loading_
routine, which is OS-specific.

As for elegance, your method is fine. Of course, if you could make your
function to return an int, then you could make it a bit simpler:

int func() { /* blah */ return 0; }
int global_var = func();

V
Jul 22 '05 #2
Dave wrote:
Hello NG,

If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the
mechanism to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)


It is worse than that (being subjective). As the standard C++ language (the
one discussed here) has nothing to say about dynamic load libraries (yet),
this is not the best place for asking this question. Or running the risk of
being politically incorrect I may say, this is not the place to ask this
question. ;-) If you need another way to do what you have asked, you will
need to ask it in a newsgroup or other forum dedicated to your
platform/compiler. Most probably there is a way to do that.

--
WW aka Attila
:::
Cole's Axiom: The sum of the intelligence on the planet is a constant. The
population is growing.
Jul 22 '05 #3

"White Wolf" <wo***@freemail.hu> wrote in message
news:cs**********@phys-news1.kolumbus.fi...
Dave wrote:
Hello NG,

If a particular dynamic library gets loaded, I need to ensure that a
function in that library gets executed on startup (and I want the
mechanism to use only Standard C++). Here's what I've got:

void func()
{
// This is the function that must be executed
}

struct foo
{
foo() {func();}
} global_var;

I would like to solicit suggestions on more elegant alternatives. (I
realize that elegance is subjective!)
It is worse than that (being subjective). As the standard C++ language

(the one discussed here) has nothing to say about dynamic load libraries (yet),
this is not the best place for asking this question. Or running the risk of being politically incorrect I may say, this is not the place to ask this
question. ;-) If you need another way to do what you have asked, you will
need to ask it in a newsgroup or other forum dedicated to your
platform/compiler. Most probably there is a way to do that.

--
WW aka Attila
:::
Cole's Axiom: The sum of the intelligence on the planet is a constant. The
population is growing.

I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite. So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main(). I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.

I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining; 2) Expanding my knowledge of
C++.

Thanks again,
Dave
Jul 22 '05 #4
Dave wrote:
[...]
I specifically want to stay within the realm of Standard C++. I don't want
to do anything compiler-specific. I probably should not have mentioned the
words "dynamic library" since their presence naturally tends to cause people
to think I'm looking for something platform-specific even though my stated
intent was the opposite.
But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before
control is transferred to main().
Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.
That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of 1)
Possibly improving the code I'm maintaining;
Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
2) Expanding my knowledge of
C++.


That never hurts, of course.

V
Jul 22 '05 #5

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Xd*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
[...]
I specifically want to stay within the realm of Standard C++. I don't want to do anything compiler-specific. I probably should not have mentioned the words "dynamic library" since their presence naturally tends to cause people to think I'm looking for something platform-specific even though my stated intent was the opposite.
But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
> So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before control is transferred to main().


Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
> I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.
That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of

1) Possibly improving the code I'm maintaining;


Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
> 2) Expanding my knowledge of
C++.


That never hurts, of course.

V


Please folks, forget the dynamic libs. They don't matter. They might as
well have never existed. The program I'm maintaining already works as it
is. I don't *need* another technique. I just *want* to identify creative
ways to invoke a function upon program startup, before main() is called,
because this is 99% an academic exercise to expand knowledge. If I happen
to find a technique that I like better than what's been done, I will use it,
but it is completely beside the point. To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!
Jul 22 '05 #6

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Xd*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
[...]
I specifically want to stay within the realm of Standard C++. I don't want to do anything compiler-specific. I probably should not have mentioned the words "dynamic library" since their presence naturally tends to cause people to think I'm looking for something platform-specific even though my stated intent was the opposite.
But you'd not be getting the best advice on a platform-specific feature,
if you try to look for non-platform-specific mechanism to do something
with a platform-specific element (dynamic libraries in this case). No,
I am not encouraging you to seek platform-specific advice here. Just
trying to indicate the issues with your approach.

Don't get us wrong, please.
> So, let's pose the problem this way:

I need to ensure that a given function is called upon program startup before control is transferred to main().


Just to be totally generic, loading of a dynamic library doesn't
necessarily happen before 'main' is called. See 'dlopen' or 'LoadLibrary'
or whatever it is on your platform.

And, yes, as a very beginner-level C++ test question, "how to call
a function before 'main' is given control" is something we can answer,
and, besides, you had answered it in your original post.
> I would like to solicit suggestions on
ways I may accomplish this that fall strictly within the standard language.
That's the problem WW was trying to indicate: there are no dlls in the
standard language. So, now you're switching from a particular problem to
a generic problem, and a solution to the latter is not necessarily
an acceptable solution to the former.
I'm looking for creative / varied ways to accomplish this as a means of

1) Possibly improving the code I'm maintaining;


Staying within confines of the standard language and library may not be
an improvement in that case. Using proper OS-specific mechanisms might.
Consider yourself warned.
> 2) Expanding my knowledge of
C++.


That never hurts, of course.

V


And by the way, thank you for the alternative you have already offered!
Jul 22 '05 #7
Dave wrote:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly
challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!


Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...
Jul 22 '05 #8

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:pK*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!


Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...


Oh for God's sake, I give up. This is clearly not the place to get an
answer to a Standard C++ question. I obviously won't be missed (nor will I
miss the sarcasm).
Jul 22 '05 #9
Dave wrote:
[...]
Oh for God's sake, I give up.


Come on, what is this, your first day on Usenet? Don't give up
simply because you've received a sarcastic remark in response to
your own. OTOH, who cares?... Bye!
Jul 22 '05 #10
Dave wrote:
[SNIP]
I specifically want to stay within the realm of Standard C++. I don't
want to do anything compiler-specific. I probably should not have
mentioned the words "dynamic library" since their presence naturally
tends to cause people to think I'm looking for something
platform-specific even though my stated intent was the opposite. So,
let's pose the problem this way:

I need to ensure that a given function is called upon program startup
before control is transferred to main(). I would like to solicit
suggestions on ways I may accomplish this that fall strictly within the
standard language.

I'm looking for creative / varied ways to accomplish this as a means of
1) Possibly improving the code I'm maintaining; 2) Expanding my
knowledge of C++.

The reason why I have told you that you are asking the question in the wrong
place is because *nothing* whatsoever guarantees that *any* advice I give
you here based on my knowledge of Standard C++ will work once you try to use
in in your dynamic load libraries. Got it? When I say Standard C++ has
nothing to say (as of today, and I would say unfortunately) about dynamic
load libraries it also means, that any Standard solution I (or anyone else)
gives you here has about the same chance to work in the presence of dynamic
loading as any non-standard one. I know it sounds like I am unreasonable,
but this is really how it is. Once you say your platform, there is more
chance to give you useful ideas. But once you did that, we are off-topic
here. :-)

--
WW aka Attila
:::
Only dead fish go with the flow.
Jul 22 '05 #11

"Dave" <be***********@yahoo.com> wrote in message
news:10*************@news.supernews.com...

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:pK*******************@newsread1.mlpsca01.us.t o.verio.net...
Dave wrote:
[..] To those so inclined towards
thinking of creative ways to use the language, I am giving you a friendly challenge to exercise your C++ muscles and dazzle the NG with your
creativity / elegance!
Thanks, Dave. It's just what we need. I can really see now how we've
stagnated here over the years. How many readers here have written a C++
book? How many have patented a template technique? Embarrassing...


Oh for God's sake, I give up. This is clearly not the place to get an
answer to a Standard C++ question. I obviously won't be missed (nor will

I miss the sarcasm).


dave:
please dont let this stuff bother you - just ignore it. its somewhat common
on the newsgroups (not that that excuses it) and victor has a long history
here of the sarcasm you mention as well as being a general ass with an
arrogant know it all attitude. the c++ newsgroups are apparently his life;
what more can I say? its really kind of sad.

keep posting and learning!

best,
dan

Jul 22 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Andreas Zita | last post: by
75 posts views Thread by Beni | last post: by
17 posts views Thread by romixnews@googlemail.com | last post: by
5 posts views Thread by Selva Chinnasamy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.