Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

NT shutdown

Posted on 2000-03-10
27
Medium Priority
?
529 Views
Last Modified: 2010-04-04
I want to use the function:
exitwindowsex(EWX_shutdown,0)
to shut down the NT server, but I don't know how to set the privilleges
Sample code please
thanx
0
Comment
Question by:gikam
[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
  • 12
  • 8
  • 3
  • +3
27 Comments
 
LVL 12

Expert Comment

by:Faruk Onder Yerli
ID: 2603648
// windows restart
procedure RestartWindows;
begin
     if not ExitWindows(EW_RestartWindows, 0) then
     ShowMessage('Windows is not restarted');
 end;


// System Reboot
procedure RebootSystem;
    begin
      if not ExitWindows(EW_RebootSystem, 0) then
        ShowMessage('Reboot is not complete');
    end;


0
 

Expert Comment

by:PetriWork
ID: 2603712
look at http://borrisholt.com under WinNT - Shutdown.zip
Complete solution =)
0
 
LVL 5

Expert Comment

by:TheNeil
ID: 2604340
Sorry, couldn't resist...

Is that Exit Windows Ex or Exit Window Sex (how does that work then)?

The Neil =;)
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 15

Expert Comment

by:simonet
ID: 2604362
Following...
0
 
LVL 1

Expert Comment

by:aldyn
ID: 2615170
Setting privileges to shutdown computer. Note that changes privileges requires some privileges too.

const  SE_SHUTDOWN_NAME = 'SeShutdownPrivilege';

var hToken: DWORD;
    tp: TTokenPrivileges;
    outtp: PTokenPrivileges;
    ol: ^DWORD;
    luid: TLargeInteger;
begin
    try
        if not OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, hToken ) then Abort;

        if not LookupPrivilegeValue(nil, SE_SHUTDOWN_NAME , luid) then Abort;
        tp.PrivilegeCount:=1;
        tp.Privileges[0].luid:=luid;
        tp.Privileges[0].Attributes:=SE_PRIVILEGE_ENABLED;
        outtp:=nil; ol:=nil;
        if not AdjustTokenPrivileges(hToken, FALSE, tp, sizeof(TTOKENPRIVILEGES),
                                outtp^, ol^ ) then Abort;
        if GetLastError() <> ERROR_SUCCESS then Abort;
    except
        Beep;
    end;
end;
0
 
LVL 2

Author Comment

by:gikam
ID: 2618629
PetriWork, Aldyn,
I noticed that I must have the
TOKEN_ADJUST_PRIVILEGES
privilege too. So how can I set this privilege, also?
sorry, your examples are incomplete.
thanx, and please try again

Petri's example is very good but lacks the TOKEN_ADJUST_PRIVILEGES privilege
0
 
LVL 1

Expert Comment

by:aldyn
ID: 2618828
Ask your administrator for it.
The Windows NT security model does not allow uncontrolled privileges grants to everyone who wish it. So if your process has no such privileges at startup you there is no way to obtain them in future. If you have no administrator privileges then you will have to ask administrator for additional rights.
By the way, there is an excellent API function - InitiateSystemShutdown - and it works over net too.
   
0
 
LVL 2

Author Comment

by:gikam
ID: 2622408
Adjusted points from 50 to 75
0
 
LVL 2

Author Comment

by:gikam
ID: 2622409
I am the administrator and I don't know how to set the privilege to my application. Am I too stupid?!
0
 
LVL 1

Expert Comment

by:aldyn
ID: 2622491
Well. As I see the code samples do not work for you. What errors/exceptions do you see? And have you located where they occur? Have you used InitiateSystemShutdown call? Does it work?

Then, try to run UserManger from the administration menu and click the Policy->User Rights menu in it. Try to set the rights to shtdown the computer to yourself.

And one more thing: what groups are you member of? It could cause problems too. For example if you are a member of groups Administrators and Guests at the same time then it is likely that you've lost some privileges. Make sure that you are a member of the one - administrators - group only.

Good luck.
0
 
LVL 15

Expert Comment

by:simonet
ID: 2625785
Here's an article I wrote a while ago on shutting down Windows NT:

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

 It has been tested with:

- Windows NT 4 Server SP 3/4/5/6
- BackOffice SBS
- BackOffice Server
- Windows NT Workstation SP 4/5

Configurations not listed above have not been tested, but I have no reasons to belive it wouldn't work with other configs. I remind you that you must have some previleges in order to set the shutdown privilege.

Yours,

Alex
Athena's Place: http://www.bhnet.com.br/~simonet
0
 
LVL 2

Author Comment

by:gikam
ID: 2626735
Adjusted points from 75 to 90
0
 
LVL 2

Author Comment

by:gikam
ID: 2626736
aldyn
most of your example work, AdjustTokenPrivileges succeeds but GetLastError after it returns 1300 (ERROR_NOT_ALL_ASSIGNED)
There are no error messages displayed on the screen.
In policies>userrights I have the right to shut down.(its normal since I can shutdown from Start menu, isnt it?).
I am the member of administrators only

simonet
I cant reach your link

would all of you mind if you try some more?
thanx
0
 

Expert Comment

by:PetriWork
ID: 2626972
I must say that I've had no problems with the example that I posted.
I've used it on over 1000 machines (Win9x, WinNT and Win2k) without any problems yet. Do you have the possibility to test this on another machine and also change the GetLastError to RaiseLastWin32Error so we can get some "easily read" information?

Regards,
  Peter
0
 
LVL 15

Expert Comment

by:simonet
ID: 2627254
How come you can't reach the link?

I just tested it and it works. Anyway, here's the ENTIRE article, as it appears on my site:

Tips & Tricks
Shutting Windows NT down

A quite common question I see often in discussion groups is how to shutdown/reboot Windows NT. The problem is that the usual API for the task, ExitWindowsEx, doesn't work right away with Windows NT. It's necessary to "ask for permission" before calling ExitWindows NT. This means that a process must have the SE_SHUTDOWN_NAME privilege for rebooting, powering off, or shutting the system, forced or not (using the EWX_FORCE flag).

For those who have taken a look at the Win32 Development Guide, a help file that comes with Delphi, there's a topic that explains how to shut down Windows NT, but that code is in C++. I've translated the code to Delphi, creating a function called ExitWindowsNT.

Calling ExitWindowsNT is just like calling ExitWindowsEx with only the uFlags paramter. The uFlags parameter specifies the type of shutdown. More about this is explained in the topic "ExitWindowsEx" in the Win32 SDK.

All that said, let's go the function itself:

 
procedure ExitWindowsNT(uFlags : integer);
var
  hToken : THANDLE;
  tkp, tkDumb : TTokenPrivileges;
  DumbInt : integer;
begin
  FillChar(tkp, sizeof(tkp), 0);
  // Get a token for this process
  if not (OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES
          or TOKEN_QUERY, hToken)) then
     raise Exception.create('OpenProcessToken failed with code '
               + inttostr(GetLastError));

  // Get the LUID for the Shutdown privilege
  LookupPrivilegeValue(nil, pchar('SeShutdownPrivilege'),
                       tkp.Privileges[0].Luid);

  tkp.PrivilegeCount := 1; // one privilege to set
  tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;

  // Get the shutdown provolege for this process
  AdjustTokenPrivileges(hToken, false, tkp, sizeof(tkDumb), tkDumb, DumbInt);

  // Cannot test the return value of AdjustTokenPrivileges
  if GetLastError <> ERROR_SUCCESS then
     Raise Exception.create('AdjustTokenPrivileges failed with code '
               + inttostr(GetLastError));

  // shut down the system and for all applications to close
  if not ExitWindowsEx(uFlags, 0) then
     Raise Exception.create('ExitWindowsEx failed with code '
               + inttostr(GetLastError));
end;

        
 

A few examples on using the function above are:

ExitWindowsNT(EWX_SHUTDOWN or EWX_FORCE); ---- This will shutdown Windows NT without making questions. All opened applications will be closed and any unsaved data will be lost.

ExitWindowsNT(EWX_REBOOT); ---- This will reboot the system. Applications will be sent the WM_QUERYENDSESSION message so they can stop the process.

For other possible combinations of EWX_FORCE, EWX_REBOOT, EWX_POWEROFF, EWX_LOGOFF, EWX_SHUTDOWN, please refer to the Win32 Development Guide.




---------
Yours,

Alex
0
 
LVL 2

Author Comment

by:gikam
ID: 2635210
simonet
in you example, AdjustTokenPrivileges generates a compiler error:
[Error] main.pas(74): Types of actual and formal var parameters must be identical
I have Delphi 4
0
 
LVL 15

Expert Comment

by:simonet
ID: 2635579
Just change the DumbInt var type to Cardinal, longint or TLargeInteger (the code completion feature in the IDE should give you a description of what the correct type should be).

Alex
0
 
LVL 2

Author Comment

by:gikam
ID: 2638898
Adjusted points from 90 to 100
0
 
LVL 2

Author Comment

by:gikam
ID: 2638899
simonet
It didnt work. Would you help me debug it?
I got the error:
AdjustTokenPrivileges failed with code 1300
0
 
LVL 15

Expert Comment

by:simonet
ID: 2843654
Are you logged on as an Administrator?

If not, what are the privileges you have as an NT user?

Alex
0
 
LVL 2

Author Comment

by:gikam
ID: 2860910
I am logged as an administrator!
0
 
LVL 15

Expert Comment

by:simonet
ID: 2925976
Gikam, are you there?

0
 
LVL 2

Author Comment

by:gikam
ID: 2952152
i just replied
0
 
LVL 2

Author Comment

by:gikam
ID: 2952159
i just replied
0
 
LVL 15

Expert Comment

by:simonet
ID: 2971216
What exact errors are you getting?

What parameters are you passing to the function?

Alex
0
 
LVL 2

Author Comment

by:gikam
ID: 2984873
I wrote
read above
I've got tiered of this.
do you have a working example?
0
 
LVL 15

Accepted Solution

by:
simonet earned 200 total points
ID: 2984953
The example I gave you works fine for everybody.
Perhaps you are passing the wrong parameters to it.

A few examples on using the function above are:

ExitWindowsNT(EWX_SHUTDOWN or EWX_FORCE); ---- This will shutdown Windows NT without making questions. All opened applications will be closed and any unsaved data will be lost.

ExitWindowsNT(EWX_REBOOT); ---- This will reboot the system. Applications will be sent the WM_QUERYENDSESSION message so they can stop the process.


For other possible combinations of EWX_FORCE, EWX_REBOOT, EWX_POWEROFF, EWX_LOGOFF, EWX_SHUTDOWN, please refer to the Win32 Development Guide.

The function I sent you is from my site:
http://www.bhnet.com.br/~simonet/tipstricks/exitwindowsnt.htm

Yours,

Alex


0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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…
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…
Suggested Courses

705 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