Solved

Reboot computer using API in Windows NT

Posted on 1997-06-13
10
426 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

706 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

20 Experts available now in Live!

Get 1:1 Help Now