Solved

Shutdown Apps and WinXP

Posted on 2006-07-16
5
863 Views
Last Modified: 2008-01-09
There will be one application and also this little utility running under WinXP.

I want the util to shutdown the app in a way that allows it to save any data, and then to close WinXP and switch off the PC.

I don't know much about the WinAPI but my reading gets me to this point -

procedure StopPC;
begin
   SetSuspendState(false, false, true); // give the app the chance to save data.

   SetSuspendState(false, true, true); // Incase it refuses to close then force it to close.

   ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);  // shutdown winxp and switch the PC power off.
end;

So how do I stop the SetSuspendState from closing the util before the util can issue the ExitWindowsEx?



0
Comment
Question by:frog
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:sun4sunday
Comment Utility
0
 

Author Comment

by:frog
Comment Utility
Thanks for your reply sun4sunday.

I don't have premium membership so I can't view the answer but it's interesting that his question says that ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0) didn't work.
From my reading of WinAPI help it seems that there are a lot of conditions that have to be met before you can shut down windows so I think I'll give that one a miss.

So now the question becomes how do I stop the first SetSuspendState from closing the util before the util can issue the second SetSuspendState?
Or should I just issue the SetSuspendState(false, true, true)?

As for the shut down of Win and the PC I will do it by brute force in extermal hardware by simply removing mains power from the PC.
That will be easy to do because the util is to be used to shut down the PC when a battery voltage drops.
The low battery condition will be applied to a serial port handshake pin which the util will monitor and so some time after the battery monitoring hardware has toggled the pin it can assume the app has closed and then just remove the mains power.
0
 
LVL 9

Accepted Solution

by:
sun4sunday earned 50 total points
Comment Utility
Copying the solution of the above PAQ given by Madshi

First of all you need to be logged in with a user that has the right to shut XP down. Then you need to enable the right privilege. I was tired of this privilege problem, so I wrote a little function which simply enabled all privileges your current user has:

function EnableAllPrivileges : string;
var c1, c2, c3 : dword;
    ptp        : PTokenPrivileges;
    i1         : integer;
    arrCh      : array [0..MAX_PATH] of char;
begin
  result := '';
  if OpenProcessToken(windows.GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, c1) then
    try
      c2 := 0;
      GetTokenInformation(c1, TokenPrivileges, nil, 0, c2);
      if c2 <> 0 then begin
        ptp := AllocMem(c2);
        if GetTokenInformation(c1, TokenPrivileges, ptp, c2, c2) then begin
          for i1 := 0 to integer(ptp^.PrivilegeCount) - 1 do begin
            c3 := MAX_PATH;
            if LookupPrivilegeName(nil, ptp^.Privileges[i1].Luid, arrCh, c3) then
                 result := result + #$D#$A + arrCh + ' '
            else result := result + #$D#$A + '??? ';
            result := result + booleanToChar(ptp^.Privileges[i1].Attributes and SE_PRIVILEGE_ENABLED <> 0);
            ptp^.Privileges[i1].Attributes := ptp^.Privileges[i1].Attributes or SE_PRIVILEGE_ENABLED;
          end;
          AdjustTokenPrivileges(c1, false, ptp^, c2, PTokenPrivileges(nil)^, cardinal(pointer(nil)^));
        end;
        FreeMem(ptp);
      end;
    finally CloseHandle(c1) end;
  Delete(result, 1, 2);  
end;



function BooleanToChar(bool: boolean) : string;
begin
  if bool then result := '+' else result := '-';
end;





sun4sunday
0
 

Author Comment

by:frog
Comment Utility
Thank you very much sun4sunday.

EnableAllPrivileges;
 and then
ExitWindowsEx(EWX_POWEROFF or EWX_FORCE, 0);

shuts down Windows and turns the PC off !!!


I sometimes wonder if Experts realise how much they help mugs like me who just write a few utils or small apps for themselves.

A few lines of code that is easy for you would be a totally impossible task for me. I would have spent a complete weekend trying and then given up.

Thank you very much for your help.




0
 
LVL 9

Expert Comment

by:sun4sunday
Comment Utility
You have to thankful to Madshi for the excellent piece of code

sun4sunday
0

Featured Post

Highfive Gives IT Their Time Back

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

11 Experts available now in Live!

Get 1:1 Help Now