The following research uses C ++ in C ++ to capture windows Shutdown events and check whether C ++ can be used as a program to remind me when it is shut down, this will not cause losses caused by shutdown when some files are not saved.
Very lucky and easy to find Microsoft. the SystemEvents class under the Win32 namespace, which has a static event SessionEnding that occurs when the system is logged out or shut down. This event is only valid in winform programs, but not in console programs, it cannot trigger events. Another point is that we must remove the added events when the program is launched, otherwise it will easily cause memory overflow.
The key code is as follows:
- Using System;
- Using System. Collections. Generic;
- Using System. Windows. Forms;
- Using Microsoft. Win32;
- Namespace Shutdown
- {
- Static class Program
- {
- /**////
- /// Main entry point of the application.
- ///
- [STAThread]
- Static void Main ()
- {
- Application. EnableVisualStyles ();
- Application. SetCompatibleTextRenderingDefault (false );
- FormShutdownFormShutdown=NewFormShutdown ();
- SystemEvents. SessionEnding + = new SessionEndingEventHandler (formShutdown. SystemEvents_SessionEnding );
- Application. Run (formShutdown );
- }
- }
- } Form code:
- Using System;
- Using System. Collections. Generic;
- Using System. ComponentModel;
- Using System. Data;
- Using System. Drawing;
- Using System. Text;
- Using System. Windows. Forms;
- Using Microsoft. Win32;
- Namespace Shutdown
- {
- Public partial class FormShutdown: Form
- {
- Const stringMESSAGE_TXT="Have you signed back? ";
- Const stringMESSAGE_TITLE="Prompt";
- Public FormShutdown ()
- {
- InitializeComponent ();
- }
This program passed the test in Windows2003 using C ++. When using the SystemEvents. SessionEnding event, remember to remove the event when the program exits.
However, there are two regrets:
1. This method cannot capture sleep events.
2. This program occupies too much memory, and only such a small function actually occupies 12 Mb of memory, which is caused by. Net framework. It is incredible.
Do you have any good ideas to overcome these two shortcomings?
- Introduction to C ++
- Summary Notes on learning and exploring C ++ library functions
- Basic Conception and method of C ++ Class Library Design
- Does C ++ really have market value?
- Basic Conception and method of C ++ Class Library Design