How do I detect if windows is shutting down?

This is a discussion on How do I detect if windows is shutting down? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, I'm trying to make a program to run in the background on my computer that times how long the ...

  1. #1
    Registered User *Tom*'s Avatar
    Join Date
    Oct 2005
    Location
    England
    Posts
    7

    How do I detect if windows is shutting down?

    Hi, I'm trying to make a program to run in the background on my computer that times how long the computer has been running for. I can set it as a scheduled task to run on startup, but I don't know if its possible to detect when windows is shutting down so that it can save the result to a file or something. I've tried doing some research, but all i can find is stuff about WM_QueryEndSession, but not anything relevant to C programming.
    Can anyone help?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    WM_QUERYENDSESSION is highly relevant to C programming. What you need to do is start a tiny (really tiny - you don't want to eat up memory) program that runs in the background and just waits for WM_QUERYENDSESSION.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    There are a few options. I have used the WM_QueryEndSession monitoring before to make it work in c++. Why is this method not a possibility for c? Or did you just not find examples in c?

    Another option is to make a Winlogon Notify dll. This is a little more sophisticated but gives you some interesting options. It starts earlier than even services (I cannot guarantee this is always the case, but in my testing it has been). Your dll can have functions launched when windows is starting, shutting down, when someone is logging on or logging off, and more (locking, unlocking, etc). It is quite powerful. With this method you could log how long an individual user was logged on for if you are so inclined. It is a very potent method. (It might also be more accurate if you are interested in tracking when the computer started as opposed to when a logon occurred if you are using a user run dependent automatic startup)

    I do believe you could also tie into SENS, which is built on top of Winlogon Notify. It would provide basically the same functionality.

  4. #4
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    WM_QUERYENDSESSION is useable in C programs (with a window). However, if you just want to detect log-off in a console program, you can use the SetConsoleCtrlHandler function:
    Code:
    BOOL WINAPI HandlerRoutine(DWORD dwCtrlType)
    {
    	/* Note, this function runs in a new thread. */
    
    	if (dwCtrlType == CTRL_LOGOFF_EVENT)
    	{
    		printf("User is logging off\n");
    		getchar();
    	}
    
    	return FALSE;
    }
    
    int main(void)
    {
    	SetConsoleCtrlHandler(HandlerRoutine, TRUE);	
    
    	...
    }

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,786

  6. #6
    Registered User *Tom*'s Avatar
    Join Date
    Oct 2005
    Location
    England
    Posts
    7
    Hi, thanks for the help. When I said I didn't find anything relevant to c programming I meant I didn't find any examples, not that WM_QueryEndSession wasn't relevant!
    You solved my problem, now I'm interested in the Winlogon Notify dll thing, but I don't know how to go about making one using microsoft development environment 2003.

  7. #7
    Registered User
    Join Date
    Nov 2005
    Posts
    88
    I am glad the previous posters found you the answer you were looking for.

    While the Winlogon Notify may be using a cannon to kill an ant in this particular instance, if you are just interested in learning, I would definitely recommend writing a test app that uses it. It will provide you with a reason to use Registry API functions, write a dll, learn multithreading techniques, all while utilizing a very powerful feature in windows (if you haven't done any of those things yet). If you go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify you will find where your reg key must be added in order to register your dll. You can look at SensLogon to see what options are available and just experiment, or you can go to msdn.com and find more info on Winlogon Notify. I have never developed in 2003, but I imagine it just requires you to create a dll project. All Winlogon Notify does is let you specify functions in your dll to call when events occur, so I do not think it is more complicated than just starting a dll project to get rolling.

  8. #8
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    Quote Originally Posted by mercury529
    Write a dll, learn multithreading techniques, all while utilizing a very powerful feature in windows (if you haven't done any of those things yet). If you go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\Notify you will find where your reg key must be added in order to register your dll. You can look at SensLogon to see what options are available and just experiment, or you can go to msdn.com and find more info on Winlogon Notify. I have never developed in 2003, but I imagine it just requires you to create a dll project. All Winlogon Notify does is let you specify functions in your dll to call when events occur, so I do not think it is more complicated than just starting a dll project to get rolling.
    cool.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Anyone using Windows 7?
    By Sharke in forum General Discussions
    Replies: 60
    Last Post: 07-12-2009, 08:05 AM
  2. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 07:14 AM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  4. Shutting Down windows
    By DirX in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2003, 11:08 AM
  5. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21