Save files and shutdown system using C++ Builder

Posted on 2004-11-09
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
Question by:a510369
    LVL 9

    Expert Comment

    Hola Luis:


    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,
            (PTOKEN_PRIVILEGES)NULL, 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

    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.

    LVL 9

    Accepted Solution

    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

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
    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.
    This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
    THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

    733 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

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now