?
Solved

Shutdown Apps and WinXP

Posted on 2006-07-16
5
Medium Priority
?
868 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 200 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month13 days, 2 hours left to enroll

777 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