Solved

How to shut down computer

Posted on 2002-04-09
4
315 Views
Last Modified: 2010-04-04
I am using win 98. In my program, I want to have a shutting down computer function. I try to use ExitWindowsEx(EWX_POWEROFF, 0). But it only does log off in win 98, win me and do nothing in win 2000.
So how can the program shut down a computer?

Thank u


Peter
0
Comment
Question by:petershaw9
4 Comments
 
LVL 17

Expert Comment

by:geobul
ID: 6927820
Hi,
Use EWX_SHUTDOWN instead. Or (EWX_FORCE or EWX_SHUTDOWN,0) to close all apps immediately.

Regards, Geo
0
 
LVL 1

Accepted Solution

by:
TAZI earned 200 total points
ID: 6927827
Hi ..

Herewith, a piece of code that may assist.

function MyExitWindows(RebootParam: Longword): Boolean;
var
  TTokenHd: THandle;
  TTokenPvg: TTokenPrivileges;
  cbtpPrevious: DWORD;
  rTTokenPvg: TTokenPrivileges;
  pcbtpPreviousRequired: DWORD;
  tpResult: Boolean;
const
  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    tpResult := OpenProcessToken(GetCurrentProcess(),
      TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
      TTokenHd);
    if tpResult then
    begin
      tpResult := LookupPrivilegeValue(nil,
                                       SE_SHUTDOWN_NAME,
                                       TTokenPvg.Privileges[0].Luid);
      TTokenPvg.PrivilegeCount := 1;
      TTokenPvg.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
      cbtpPrevious := SizeOf(rTTokenPvg);
      pcbtpPreviousRequired := 0;
      if tpResult then
        Windows.AdjustTokenPrivileges(TTokenHd,
                                      False,
                                      TTokenPvg,
                                      cbtpPrevious,
                                      rTTokenPvg,
                                      pcbtpPreviousRequired);
    end;
  end;
  Result := ExitWindowsEx(RebootParam, 0);
end;




procedure TForm1.Button1Click(Sender: TObject);
begin
  MyExitWindows(EWX_SHUTDOWN);
end;


Hope this helps

Regards
TAZI
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 6927853
'Shutdown PC with all Windows versions' article at http://www.delphi3000.com/article.asp?ID=2108
0
 
LVL 15

Expert Comment

by:simonet
ID: 6928010
Here's an article I wrote about this very same issue:


http://www.bhnet.com.br/~simonet/tipstricks/exitwindowsnt.htm

Yours,

Alex
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

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…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
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…

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

12 Experts available now in Live!

Get 1:1 Help Now