Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. C++ - code executed upon definite program termination

C++ - code executed upon definite program termination

Scheduled Pinned Locked Moved C / C++ / MFC
c++
11 Posts 6 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    liquid_
    wrote on last edited by
    #1

    I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

    C Richard Andrew x64R P H D 5 Replies Last reply
    0
    • L liquid_

      I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      You could execute your program via, for instance CreateProcess[^] (see also "CreateProcess() and wait for result"[^]), then wait for the program termination and eventually execute the 'piece of code'.

      Veni, vidi, vici.

      1 Reply Last reply
      0
      • L liquid_

        I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Keep in mind that if your program is terminated by Task Manager, it won't have the chance to run any code at all.

        The difficult we do right away... ...the impossible takes slightly longer.

        1 Reply Last reply
        0
        • L liquid_

          I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

          P Offline
          P Offline
          pasztorpisti
          wrote on last edited by
          #4

          Just do what CPallini recommended. If you would do the same on linux then you could easily do the same trick with fork() without using 2 binaries.

          1 Reply Last reply
          0
          • L liquid_

            I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

            H Offline
            H Offline
            Heng Xiangzhong
            wrote on last edited by
            #5

            CPallian gave a good idea. Maybe you can do this in other way. You can complete a dll for this. when a dll is detached, the branch named "DLL_PROCESS_DETACH" of Dllmain. yeah, it's your wanted place. :laugh: Nice to make friends with you !

            1 Reply Last reply
            0
            • L liquid_

              I'd like to have a piece of code executed upon definite end of a program. atexit() is not appropriate because there may be some code executed when destructing static objects and this function does things beforehand.

              D Offline
              D Offline
              David Crow
              wrote on last edited by
              #6

              What about something like:

              #include <...>
              #include "..."

              class dummy
              {
              public:
              dummy::dummy()
              {
              ...code here is executed before main()
              }

              dummy::~dummy()
              {
                  ...code here is executed after main()
              }
              

              };

              void main( void )
              {
              ...do some stuff
              }

              "One man's wage rise is another man's price increase." - Harold Wilson

              "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

              "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

              H 1 Reply Last reply
              0
              • D David Crow

                What about something like:

                #include <...>
                #include "..."

                class dummy
                {
                public:
                dummy::dummy()
                {
                ...code here is executed before main()
                }

                dummy::~dummy()
                {
                    ...code here is executed after main()
                }
                

                };

                void main( void )
                {
                ...do some stuff
                }

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                H Offline
                H Offline
                Heng Xiangzhong
                wrote on last edited by
                #7

                Perfect ideas, using the order of constructor of a object. But the question is how can you control this object is destructed behind the any other one? Nice to chat with you !

                C 1 Reply Last reply
                0
                • H Heng Xiangzhong

                  Perfect ideas, using the order of constructor of a object. But the question is how can you control this object is destructed behind the any other one? Nice to chat with you !

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Heng Xiangzhong wrote:

                  But the question is how can you control this object is destructed behind the any other one?

                  You can control it is constructed before main execution (and destructed after main execution) making it global, e.g.

                  #include <iostream>
                  using namespace std;

                  class A
                  {
                  public:
                  A(){cout << "A ctor" << endl;}
                  ~A(){cout << "A dtor" << endl;}
                  };

                  A a;

                  int main()
                  {
                  cout << "main" << endl;
                  }

                  Veni, vidi, vici.

                  H 1 Reply Last reply
                  0
                  • C CPallini

                    Heng Xiangzhong wrote:

                    But the question is how can you control this object is destructed behind the any other one?

                    You can control it is constructed before main execution (and destructed after main execution) making it global, e.g.

                    #include <iostream>
                    using namespace std;

                    class A
                    {
                    public:
                    A(){cout << "A ctor" << endl;}
                    ~A(){cout << "A dtor" << endl;}
                    };

                    A a;

                    int main()
                    {
                    cout << "main" << endl;
                    }

                    Veni, vidi, vici.

                    H Offline
                    H Offline
                    Heng Xiangzhong
                    wrote on last edited by
                    #9

                    Yeah, you are right. It is all they can do. But it is too simple to complete the most situations. for example, i want execute a piece of code behind the all of objects' destruct function is invoked. if this, dll is a good way.

                    L 1 Reply Last reply
                    0
                    • H Heng Xiangzhong

                      Yeah, you are right. It is all they can do. But it is too simple to complete the most situations. for example, i want execute a piece of code behind the all of objects' destruct function is invoked. if this, dll is a good way.

                      L Offline
                      L Offline
                      liquid_
                      wrote on last edited by
                      #10

                      Thanks for all replies. Anyway, the solution requires quite tricky programming.

                      Heng Xiangzhong wrote:

                      for example, i want execute a piece of code behind the all of objects' destruct function is invoked.

                      ...and is right. I would say it looks much more difficult than using simple atexit() function and I wonder why. Probably it belonged to early C standard libraries and no one invented anything better at C++ times. I'm not sure whether dll is a solution because the same question remains: if I'd like to execute some code after all static objects in dll are disposed, I'd come to the same point of interest.

                      H 1 Reply Last reply
                      0
                      • L liquid_

                        Thanks for all replies. Anyway, the solution requires quite tricky programming.

                        Heng Xiangzhong wrote:

                        for example, i want execute a piece of code behind the all of objects' destruct function is invoked.

                        ...and is right. I would say it looks much more difficult than using simple atexit() function and I wonder why. Probably it belonged to early C standard libraries and no one invented anything better at C++ times. I'm not sure whether dll is a solution because the same question remains: if I'd like to execute some code after all static objects in dll are disposed, I'd come to the same point of interest.

                        H Offline
                        H Offline
                        Heng Xiangzhong
                        wrote on last edited by
                        #11

                        Let met think. If you don't think it is complex, hook is a good solution for this. You can hook the ExitProcess API in win32 by inline, IAT or something you can do. Because a process exits in windows through invoke the ExitProcess by itself, so after CRT lib destructs all the objects, it will invoke ExitProcess, at that time, the control flow will give you a chance to execute your code. If you don't know how to complete it, I can give a piece of code that i wrote before. Nice to make friends with you ! My email, Henzox.7@gmail.com

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups