Solved

Reboot computer using API in Windows NT

Posted on 1997-06-13
10
441 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
[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
  • 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
Industry Leaders: 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

734 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