Save files and shutdown system using C++ Builder

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
a510369Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CayceCommented:
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;
   TOKEN_PRIVILEGES tkp;
   // Get a token for this process.  
   if (!OpenProcessToken(GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken))
      return( FALSE );  
   // Get the LUID for the shutdown privilege.  
   LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME,
        &tkp.Privileges[0].Luid);  
   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.

Cayce
0
a510369Author Commented:
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.

Luis
0
CayceCommented:
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).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
a510369Author Commented:
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.
vela_luis@yahoo.com
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.