Solved

Reboot computer using API in Windows NT

Posted on 1997-06-13
10
438 Views
Last Modified: 2006-11-17
I want to reboot the computer in Windows NT when a certain condition is met. I use the API function (ExitWindows) which works in Windows 95, but fails to work in Windows NT. This fuction is supposed to work in any Win-32 environment.

Fuction declaration:
Private Declare Function ExitWindows Lib "User32" Alias "ExitWindowsEx" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long

Function call:
If ... Then ExitWindows EWX_REBOOT, &HFFFFFFFF

0
Comment
Question by:kamall
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 1

Expert Comment

by:Amberite
ID: 1427247
Make SURE that to declare EWX_REBOOT, you are not using: Const EWX_REBOOT As Long = 2
Instead you must use:
Public Const EWX_REBOOT = 2This is a common mistake and can lead to the problem. ALSO, instead of using that declare, try this:
Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long - This is the standard Windows declaration.


0
 
LVL 5

Expert Comment

by:y96andha
ID: 1427248
If you'd like to pass the question on to someone else, I could give you a code working under Windows NT.
0
 
LVL 1

Expert Comment

by:Amberite
ID: 1427249
wait your turn y96...
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 5

Expert Comment

by:y96andha
ID: 1427250
Sure, I just wanted you to know that your answer won't work, Amberite.
0
 
LVL 1

Expert Comment

by:Amberite
ID: 1427251
hmm.. maybe.. by the way, what does y96andha mean?
0
 
LVL 5

Expert Comment

by:y96andha
ID: 1427252
It's my University of Linköping login ID.

My name is Andreas Hansson, I started University level studies 1996, and the letter Y is used for the study programme I am in.

If you're interested, you can find some info about the University on http://www.liu.se/ and about this particular study programme on http://www.edu.isy.liu.se/y/Main-e.html. The full English name for it is "Applied Physics and Electrical Engineering".

My University e-mail is y96andha@cyd.liu.se.

About the question: The problem is that the WinNT security requires you to enable the SeShutdownPrivilege before you can shutdown the system. And I just happened to have the code for it in a program I wrote some time ago.


0
 
LVL 7

Author Comment

by:kamall
ID: 1427253
Dear Amberit
Thank you for trying to answer my question. I really appreciate it.
In fact, your suggested solution can not work because:

1. I allready have the Const EXW_REBOOT declared in my form, and when you use the constant only in the specific form, there is no need to declare it as Public Const in a module. This also applies to the Subs and Functions. I learned this from my experience in VB in the past.

2. Since my code works perfectly in Win95 which is a full 32-bit platform, it should work exactly the same in Win NT (I suppose).

Windows NT have many restricted security considerations, and I expected that my problem is in a way or another related to these considerations.

Thank you again Amberit.

y96andha, thank you for your interest. Please suggest your solution.
0
 
LVL 5

Accepted Solution

by:
y96andha earned 200 total points
ID: 1427254
This comes from another program I have written, so not all of the declarations may be necessary. EWX_FORCE for example is provided in case you would like to force a shutdown, where you would use (EWX_FORCE+EWX_REBOOT,0) as the parameters to ExitWindowsEx. This is tested with Windows NT and VB 4 professional, but that should be enough, as you said that you had a working solution for Windows 95.

I will be leaving on vacation tomorrow, returning Thursday next week, so it will take a while for me to respond to any questions you might have.




Declarations:


Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const EWX_REBOOT = 2
Private Const EWX_FORCE = 4
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8



Private Type LUID
    LowPart As Long
    HighPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
        LowPart As Long
        HighPart As Long
        Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    LowPart As Long
    HighPart As Long
    Attributes As Long
End Type

Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, ByVal PreviousState As Long, ByVal ReturnLength As Long) As Long
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long




Code to actually do the shutdown:


    Dim tok As Long
    Dim sht As LUID
    Dim st As TOKEN_PRIVILEGES
    Dim success As Long
    success = OpenProcessToken(GetCurrentProcess, TOKEN_QUERY + TOKEN_ADJUST_PRIVILEGES, tok)
    Debug.Print "OpenProcessToken"; success
    success = LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, sht)
    Debug.Print "LookupPrivilegeValue"; success
    st.PrivilegeCount = 1
    st.LowPart = sht.LowPart
    st.HighPart = sht.HighPart
    st.Attributes = SE_PRIVILEGE_ENABLED
    success = AdjustTokenPrivileges(tok, 0, st, 0, 0, 0)
    Debug.Print "AdjustTokenPrivileges"; success
    success = ExitWindowsEx(EWX_REBOOT, 0)
    Debug.Print "ExitWindowsEx"; success

0
 
LVL 13

Expert Comment

by:Mirkwood
ID: 1427255
Bought This Question.
0
 
LVL 7

Author Comment

by:kamall
ID: 1427256
Too much money and too much copy-paste Mirkwood :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

829 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