Solved

Shutdown Apps and WinXP

Posted on 2006-07-16
5
866 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

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