Solved

Shutdown Apps and WinXP

Posted on 2006-07-16
5
867 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 9

Expert Comment

by:sun4sunday
ID: 17119641
0
 

Author Comment

by:frog
ID: 17126687
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
ID: 17151983
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
ID: 17158952
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
ID: 17160814
You have to thankful to Madshi for the excellent piece of code

sun4sunday
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

726 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