How to use C ++ to create a Windows Shutdown event

Source: Internet
Author: User

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:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Windows. Forms;
  4. Using Microsoft. Win32;
  5. Namespace Shutdown
  6. {
  7. Static class Program
  8. {
  9. /**////
  10. /// Main entry point of the application.
  11. ///
  12. [STAThread]
  13. Static void Main ()
  14. {
  15. Application. EnableVisualStyles ();
  16. Application. SetCompatibleTextRenderingDefault (false );
  17. FormShutdownFormShutdown=NewFormShutdown ();
  18. SystemEvents. SessionEnding + = new SessionEndingEventHandler (formShutdown. SystemEvents_SessionEnding );
  19. Application. Run (formShutdown );
  20. }
  21. }
  22. } Form code:
  23. Using System;
  24. Using System. Collections. Generic;
  25. Using System. ComponentModel;
  26. Using System. Data;
  27. Using System. Drawing;
  28. Using System. Text;
  29. Using System. Windows. Forms;
  30. Using Microsoft. Win32;
  31. Namespace Shutdown
  32. {
  33. Public partial class FormShutdown: Form
  34. {
  35. Const stringMESSAGE_TXT="Have you signed back? ";
  36. Const stringMESSAGE_TITLE="Prompt";
  37. Public FormShutdown ()
  38. {
  39. InitializeComponent ();
  40. }

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?

  1. Introduction to C ++
  2. Summary Notes on learning and exploring C ++ library functions
  3. Basic Conception and method of C ++ Class Library Design
  4. Does C ++ really have market value?
  5. Basic Conception and method of C ++ Class Library Design

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.