Solved

FORCE shutdown the Computer

Posted on 2001-06-07
3
304 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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

708 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

19 Experts available now in Live!

Get 1:1 Help Now