Trigger events when exiting through the Close window button on Windows

I am trying to find a way to trigger certain events (saving etc) when the user is closing the application through the X button not by pressing ESC.

As far as I know the exit function is not triggered in this case so I am trying to find if there is a way to do that through openframeworks directly or if there is an easy way to do it through glut.

I tried registering a function to trigger with the exit event of the ofMainLoop but it is crashing with a read access violation error on ofToDataPath.

I would like to ask if there an easy way to trigger events when a window closes? Can it be done through Openframeworks directly or should I start looking into the glut and windows callbacks.

the normal exit event should just work. in your ofapp declare an exit method without parameters or in other classes register a listener to the exit event in ofEvents

Thanks for your help Arturo!

Your answer led me to do a bit more digging in my code and I found out that I was still using ofAppGlutWindow instead of ofAppGLFWWindow so that was why exit was not working.