473,657 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
11 2092
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.********@com Acast.net> wrote in message
news:Xd******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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.********@com Acast.net> wrote in message
news:Xd******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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.********@com Acast.net> wrote in message
news:pK******** ***********@new sread1.mlpsca01 .us.to.verio.ne t...
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1524
by: Andreas Zita | last post by:
Hi I'm trying to figure out why my .net app take ages to start for the first time. Well over 10 seconds. I'm not doing anything particullary cpu intense at startup so I'm a bit confused. I have read that it takes "a while" for the .net framwork to load the first time but this long?? Does anyone have any suggestion that could boost the execution of .net apps? If I would split my app into different dll:s, could that boost the execution...
75
9851
by: Beni | last post by:
I have been programming in C for about a year now. It sounds silly, but I never took the time to question why a C(or C++ or Java) program execution begins only at the main(). Is it a convention or is there some deeper underlying reason?
17
5066
by: romixnews | last post by:
Hi, I'm facing the problem of analyzing a memory allocation dynamic and object creation dynamics of a very big C++ application with a goal of optimizing its performance and eventually also identifying memory leaks. The application in question is the Mozilla Web Browser. I also have had similar tasks before in the compiler construction area. And it is easy to come up with many more examples, where such kind of statistics can be very...
3
1578
by: steveeisen | last post by:
I'm a long-time VB6 programmer in a shop that is mostly moving to VB ..NET 2005. And I'm confused about coding the start of solutions for unattended operations. Much of what I write is old-time batch. Such programs are started from a scheduler and run on a server, cycling through a mass of data, without human intervention. No form should show on the server monitor, although an invisible one may be needed to support the Winsock or...
5
1513
by: Selva Chinnasamy | last post by:
Hi I have a Windows application written in VB which has many functions. In addition it also reads a database and generates a output file. Currently this function is called when ever user clicks the button from UI. But I like to call the function from command line too. app.exe database.mdb should call the function to generate the output file. I have modified my Main() to do this. but I wasn't able to send output to console. I have tried...
2
1612
beaver
by: beaver | last post by:
Hello! I want to detect the way of program startup/execution.. There are several possibilities: After logon/at startup After the user clicked at the program icon (Desktop, Quick Launch, Start Menu)
0
8402
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8315
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8829
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8508
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6172
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5633
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4164
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4323
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.