[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to shutdown W2K,XP programatically in spite of a locked workstation ??

Posted on 2005-04-06
6
Medium Priority
?
413 Views
Last Modified: 2010-08-05
Hi,

I've made a little tool to shutdown windows with a timer automatically. It works best - but only if the workstation isn't locked.
Do you know any possibility to shutdown as well in spite locked workstation ?  

The Function:
AutoExitWindows(EWX_POWEROFF or EWX_FORCEIFHUNG);

Thanks, Tom
0
Comment
Question by:Brainwashed2
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:CodedK
ID: 13718461
0
 
LVL 16

Accepted Solution

by:
CodedK earned 500 total points
ID: 13718470
Also:

Shutdown / restart / hibernate / standby / log off / turn off screen in any Windows version:

http://360software.dakotabcn.net/files/systemactions.zip
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13718734
This unit should help you!

sholmes

{===========================================================================================}
{                                                                                           }
{   Shutdown / restart / hibernate / standby / log off / turn off screen demo               }
{   v1.0                                                                                    }
{   Copyright © 2003 - Mariano López - 360º Software                                        }
{                                                                                           }
{   lopardo2003@hotmail.com                                                                 }
{   http://360software.dakotabcn.net/ (in construction)                                     }
{                                                                                           }
{   Last modified November 26, 2002 - 02:04 a.m.                                            }
{                                                                                           }
{===========================================================================================}
{                                                                                           }
{  PURPOSE                                                                                  }
{  This demo shows you how to shutdown, restart, etc. all Windows versions. It also detects }
{  which options are available in the system.                                               }
{                                                                                           }
{  COMPATIBILITY                                                                            }
{  This demo should work correctly under all Windows versions.                              }
{  I tested it under Windows 98 SE, ME, 2000 Prof, and XP Prof (SP1) showing no problems    }
{  at all.                                                                                  }
{  If you find any problem don't hesitate to e-mail me at the address above. If you can     }
{  solve it, please let me know.                                                            }
{                                                                                           }
{  KNOWN ISSUES                                                                             }
{  - No issues known.                                                                       }
{                                                                                           }
{  VERSION HISTORY                                                                          }
{  1.0 (Nov 26, 2002):                                                                      }
{   - Initial release.                                                                      }
{===========================================================================================}

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type                
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    rb1: TRadioButton;
    rb2: TRadioButton;
    rb3: TRadioButton;
    GroupBox2: TGroupBox;
    Button1: TButton;
    Button3: TButton;
    Button5: TButton;
    Button6: TButton;
    Button4: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation



{$R *.dfm}

  procedure SetPrivileges;
  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;
  end;

  function ExitWin(RebootParam: Longword): Boolean;
  begin
    SetPrivileges;
    Result := ExitWindowsEx(RebootParam, 0);
  end;



procedure TForm1.Button1Click(Sender: TObject);
begin
  if (rb1.Checked) then ExitWin(EWX_SHUTDOWN);
  if (rb2.Checked) then ExitWin(EWX_SHUTDOWN or EWX_FORCE);
  if (rb3.Checked) then ExitWin(EWX_SHUTDOWN or EWX_FORCEIFHUNG);
end;



procedure TForm1.Button2Click(Sender: TObject);
begin
  if (rb1.Checked) then ExitWin(EWX_REBOOT);
  if (rb2.Checked) then ExitWin(EWX_REBOOT or EWX_FORCE);
  if (rb3.Checked) then ExitWin(EWX_REBOOT or EWX_FORCEIFHUNG);
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  SetPrivileges;
  if (rb1.Checked) then SetSystemPowerState(true,false);
  if (rb2.Checked) then SetSystemPowerState(true,true);
  if (rb3.Checked) then ShowMessage('"Force if hung" option not available for stand by mode');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  SetPrivileges;
  if (rb1.Checked) then SetSystemPowerState(false,false);
  if (rb2.Checked) then SetSystemPowerState(false,true);
  if (rb3.Checked) then ShowMessage('"Force if hung" option not available for hibernate mode');
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  if (rb1.Checked) then ExitWin(EWX_LOGOFF);
  if (rb2.Checked) then ExitWin(EWX_LOGOFF or EWX_FORCE);
  if (rb3.Checked) then ExitWin(EWX_LOGOFF or EWX_FORCEIFHUNG);
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
Sleep(8000);
Beep;
SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, -1);
end;


procedure TForm1.FormCreate(Sender: TObject);
type
  TIsPwrHibernateAllowed = function : BOOL; stdcall;
  TIsPwrSuspendAllowed   = function : BOOL; stdcall;
var
  IsPwrHibernateAllowed : TIsPwrHibernateAllowed;
  IsPwrSuspendAllowed : TIsPwrSuspendAllowed;
  LibHandle : THandle;
begin
  LibHandle := LoadLibrary('POWRPROF.dll') ;

  if LibHandle <> 0 then
  begin
    @IsPwrHibernateAllowed := GetProcAddress(LibHandle, 'IsPwrHibernateAllowed');
    if @IsPwrHibernateAllowed <> nil then
    begin
      if not IsPwrHibernateAllowed then Button4.Enabled := False;
      @IsPwrHibernateAllowed := nil;
    end;

    @IsPwrSuspendAllowed := GetProcAddress(LibHandle, 'IsPwrSuspendAllowed');
    if @IsPwrSuspendAllowed <> nil then
    begin
      if not IsPwrSuspendAllowed then Button3.Enabled := False;
      @IsPwrSuspendAllowed := nil;
    end;
  end
  else
  begin
    Exit;
  end;
  FreeLibrary(LibHandle);
end;


end.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:CodedK
ID: 13722452
Hey shaneholmes hi....

In that question :
http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21377601.html

You said : " Why did you award Codek part of the points?"

Because i didnt read the authors question ...
Ok... now YOU dont read the comments right above u ?!!
 
My comment :
http://360software.dakotabcn.net/files/systemactions.zip

You comment :
http://360software.dakotabcn.net/

I think  Brainwashed2 has Winzip...
He can open the source files ...
And since the code is copyrighted i passed the link.
0
 
LVL 11

Expert Comment

by:shaneholmes
ID: 13722499
First of all, be nice.....
Secondly, that unit is posted elsewhere as well - I think i got it from the Google Groups search - OTTOMH

Brainwashed2 - im not interested in the points - if you feel like giving points to this question, pleaze give to CodedK

thanx!

sholmes
0
 
LVL 16

Expert Comment

by:CodedK
ID: 13724640
Sorry didnt mean to be rude.
but i insist that we meant the same thing so..... I dont want the points ....give it to him... Lol
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month18 days, 9 hours left to enroll

825 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