Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Save files and shutdown system using C++ Builder

Posted on 2004-11-09
Medium Priority
Last Modified: 2013-11-17
I´m Luis Vela, from Lima Peru, I´m working in a Thesis and I need to develope a software to shutdown my PC, but before that the software needs to save the unsaved files, close all programs and then shutdown the system, I wonder if somebody can help me to do that, I use C++ Builder and I need the code.
I´ll be waiting your answer here, or to vela_luis@yahoo.com
Question by:a510369
  • 2
  • 2

Expert Comment

ID: 12548240
Hola Luis:

From http://msdn.microsoft.com/library/default.asp?url=/library/en-us/sysinfo/base/how_to_shut_down_the_system.asp:

BOOL MySystemShutdown()
   HANDLE hToken;
   // Get a token for this process.  
   if (!OpenProcessToken(GetCurrentProcess(),
      return( FALSE );  
   // Get the LUID for the shutdown privilege.  
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
   tkp.PrivilegeCount = 1;  // one privilege to set    
   tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;  
   // Get the shutdown privilege for this process.  
   AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,
   if (GetLastError() != ERROR_SUCCESS)
      return FALSE;  
   // Shut down the system and force all applications to close.  
   if (!ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0))
      return FALSE;
   return TRUE;

Windows does what you want (close all apps, etc, etc) by itself. All you need is to initiate the system shutdown.


Author Comment

ID: 12549262
Hola Cayce ¿como estas? Thank you for your answer, please if it´s posible to you, Could you send me How to save my opened files automatically before shutdown windows?, I´ll thank you so much. My thesis is about a UPS system, like APC does with PowerChute it saves all non saved MSOffice´s files, close opened programs and then turn off PC.
I´m fairly new in C++ Builder, I only need the code for this action. Regards.


Accepted Solution

Cayce earned 1500 total points
ID: 12549637
What PoweChute does is use the MS Office API to ask all the Office applications to close down (and save) the opened documents before shutdown.
Windows shutdown does only send a message to all opened applications asking if it's possible to shutdown. It's responsibility of each application to save and close its opened files before shutdown.

So what it would take to do what you request is to make your application compatible to a myriad of applications sending them the proper messages so they close and save their opened documents, it's not an easy task (there's no windows api to request and application to save and close) and you should make a plugin for each supported application (that's what APC powerChute does). Otherwise you'll have to rely on just shutting windows down (which will cause applications with opened documents to display a dialog box asking the user to save or discard changes, and it there's no user at the moment after some minutes windows will forcely close all opened files and applications).

Author Comment

ID: 12550061
Thank you very much Cayce, I´ll be working on that point, but If there's something you know about that Please don´t forget to  give me a hand.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month14 days, 12 hours left to enroll

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question