Solved

FORCE shutdown the Computer

Posted on 2001-06-07
3
310 Views
Last Modified: 2011-09-20
I can i make a program that can shutdown directly
"POWER OFF" MY Computer?
0
Comment
Question by:ryan_sabarre
  • 2
3 Comments
 

Author Comment

by:ryan_sabarre
ID: 6164675
how?
0
 
LVL 20

Accepted Solution

by:
Madshi earned 50 total points
ID: 6164709
You can use this function, it works on all systems, if both the current Windows version and the PC supports poweroff:

procedure Down(powerOff, force: boolean);
var c1 : dword;
begin
  if powerOff then c1 := EWX_SHUTDOWN or EWX_POWEROFF
  else             c1 := EWX_SHUTDOWN;
  if force then c1 := c1 or EWX_FORCE;
  ExitWindowsEx(c1, 0);
end;

Furthermore you need to enable the privilege SeShutdownPrivilege on NT4/2000/XP. You can e.g. use this function, which simply enables all available privileges:

procedure EnableAllPrivileges;
var c1, c2 : dword;
    ptp    : PTokenPrivileges;
    i1     : integer;
begin        
  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
            ptp^.Privileges[i1].Attributes := ptp^.Privileges[i1].Attributes or SE_PRIVILEGE_ENABLED;
          AdjustTokenPrivileges(c1, false, ptp^, c2, PTokenPrivileges(nil)^, cardinal(pointer(nil)^));
        end;
        FreeMem(ptp);
      end;
    finally CloseHandle(c1) end;
end;

Regards, Madshi.
0
 

Author Comment

by:ryan_sabarre
ID: 6164818
Thank you so much for the help
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
find a node in VST 2 73
SUM 2 INTEGER ARRAYS INTO 1 10 108
DBGrid or StringGrid ? 6 105
How to save the image in the .cds File ClientDataSet? 1 22
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 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