Execute code on shutdown

Hello my fellow coders,

I have a question... I want the keylogger that i made to execute code when the application close's. The reason why is because it everytime my keylogger starts logging it write's "[" to a file indicating a new session. I want my keylogger to write "]" at the end of the file indicating the session closed. So this would create a log file similiar to the one shown below

[hello bob blablabla my@email.com{tab}aksgi2395]

i cant get this to work because when the program close's due to power loss of the pc or some one exit's the process it wont post the last "]". does anybody knows a method for this. It can also be when my while(keylog=true) loop ends.

so instead of return 0;
i want return log<<"]";
I am not sure if there's any way to do that reliably. Also don't know if I would use [ ][ ] as delimeters since they are common keys used, but that's just a personal opinion.

An approach I might take is if the file already has data, check the end of the file for your terminator "]". If it doesn't exist, then start with "][", else "[".

If it's not there when you parse it and hit EOF, so what, end of the session. :)
Last edited on
An approach I might take is if the file already has data, check the end of the file for your terminator "]". If it doesn't exist, then start with "][", else "[".

thanks for this piece of input i am going to implement it in some way. I am suprised that i didnt think of it before ^_^

also i log the ascii values of the keys with "." between each ascii value, like this 72.69.76.76.79.1. so all the other charcher like a-z and !-) can be used without causing problems.
You could improve your session end detection by processing the WM_QUERYENDSESSION message (http://msdn.microsoft.com/en-us/library/aa376890%28VS.85%29.aspx). I suppose that you need a top-level window for this.
Why not just have this as part of the routine for the destructor? This would make sure that it is closed everytime it's unloaded from memory (in a clean way). Although it may be compiler specific.

EDIT: I don't know what compiler you use but for mingw\GCC here's what I'm suggesting: http://www.ohse.de/uwe/articles/gcc-attributes.html#func-destructor
Last edited on
Note that no solution will solve the power loss / kill process problem. If either of those happen your program will end abruptly and you're SOL.

The best you can do in that situation is recover the next time the program is started (typically done by either making registry entries or saving temp files to the hard drive)
Also a simular way to accomplish this that would not be compiler specific is the "atexit(...)" command: http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/

As Disch emphasised though this is only useful during a clean exit. Although I thought that destructors are allowed to be executed since they would already be in the stack. I know that Outlook 2007/2010 runs some kind of cleanup routine when forced to quit so the idea is possible. I'll do some research and post here when I find something.

EDIT: I am of course talking about the 'force exit' not the 'kill power' method.
Last edited on
I am incorrect. The 'Kill Process' command does not allow destructors to run. This is an interesting puzzle, thank you for a project to work on :)!
Thanks for the input for clarification i use visual C++ express from windows 2010 downloaded from this link http://www.microsoft.com/express/Downloads/

@Computergeek0
i wil still be searching for any other method because i am intrested in this problem. I can now put the "]" bracked in its correct place.
But the first solution provided confused me i searched for WM_QUERYENDSESSION and from wat i have read its used to signal a program that it needs to exit. i could be horribly mistaken, the link seems to be broke to me it says Server Error in '/' Application.

@dish
could you clarify the the registry entry method?, the part i dont get is that i need to call a fuction that wil make these entry's. but my program operates in a while(bool = true) way. if the way is trough updating every time the while loop plays then would that increase my CPU usage a lot?

also i remember at school i was working on a program and suddenly some one accidently unplugged the power and my pc went out. i rebooted the box and my works was save, could this be because of the visual C++ express compiler backing up my work at some time before the power loss.



You have Visual Studio at school? Lucky! I have to use BBC BASIC...
Kahiko - it would seems Disch's suggestion may be to create a registry entry or temp file that basically only stores the state that the application is in. Eg, if you check it on next run and the session was not set to "closed" (or even just the existence of a particular key could work), that would flag weather or not you would need to append a "]" to the end of the log (and do any clean up that might be necessary). Once the application cleanly exits, set the flag, remove temp file, etc whatever you feel like doing..
+1 @ Psychopete. That's exactly what I was getting at.

Also someone brought to my attention in PM that killing the process via Task Manager may not actually abruptly end the program, so you might not face this issue in that situation.

However I didn't mean "kill process" in that specific sense.. I just meant it like if the process abruptly exits (by means of a power outage, or any other situation that would abnormally terminate a process).
Why don't you use sqlite ?
Topic archived. No new replies allowed.