Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke a C++ function after main() finishes

Tags:

c++

function

I am developing a C++ tool that should run transparent to main program. That is: if user simply links the tool to his program the tool will be activated. For that I need to invoke two functions, function a(), before main() gets control and function b() after main() finishes.

I can easily do the first by declaring a global variable in my program and have it initialize by return code of a(). i.e

int v = a() ;

but I cannot find a way to call b() after main() finishes?

Does any one can think of a way to do this?

The tool runs on windows, but I'd rather not use any OS specific calls.

Thank you, George

like image 612
George T. Avatar asked Dec 05 '22 01:12

George T.


2 Answers

Use RAII, with a and b called in constructor/destructor.

class MyObj {
MyObj()
  {
   a();
  };
~MyObj()
  {
    b();
  };
};

Then just have an instance of MyObj outside the scope of main()

MyObj obj;

main()
{
  ...
}

Some things to note.

  • This is bog-standard C++ and will work on any platform
  • You can use this without changing ANY existing source code, simply by having your instance of MyObj in a separate compilation unit.
  • While it will run before and after main(), any other objects constructed outside main will also run at this time. And you have little control of the order of your object's construction/destruction, among those others.
like image 79
Roddy Avatar answered Dec 26 '22 22:12

Roddy


SOLUTION IN C:

have a look at atexit:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void bye(void)
{
    printf("That was all, folks\n");
}
int main(void)
{
    long a;
    int i;
    a = sysconf(_SC_ATEXIT_MAX);
    printf("ATEXIT_MAX = %ld\n", a);
    i = atexit(bye);
    if (i != 0) {
        fprintf(stderr, "cannot set exit function\n");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}

http://linux.die.net/man/3/atexit

this still implies however that you have access to your main and you can add the atexit call. If you have no access to the main and you cannot add this function call I do not think there is any option.

EDIT:

SOLUTION IN C++:

as sudgested there is a c++ equivalent from std. I simply paste in here an example which i copied from the link available just below the code:

#include <iostream>
#include <cstdlib>

void atexit_handler_1() 
{
    std::cout << "at exit #1\n";
}

void atexit_handler_2() 
{
    std::cout << "at exit #2\n";
}

int main() 
{
    const int result_1 = std::atexit(atexit_handler_1);
    const int result_2 = std::atexit(atexit_handler_2);

    if ((result_1 != 0) or (result_2 != 0)) {
        std::cerr << "Registration failed\n";
        return EXIT_FAILURE;
    }

    std::cout << "returning from main\n";
    return EXIT_SUCCESS;
}

http://en.cppreference.com/w/cpp/utility/program/atexit

like image 40
Stefano Avatar answered Dec 26 '22 23:12

Stefano