highlander
February 8th, 2000, 03:10 PM
I'm trying to shut down the computer, I tried shutwindowex api and shell "rundll.exe....... but It's not working for me.
Is there any cach all shut down trick. Please help.
Thank you.
P.S. I need it to work on 98/NT and if possible on 95.
Aaron Young
February 8th, 2000, 03:27 PM
This should work just fine:
private Declare Function ExitWindowsEx Lib "user32" (byval uFlags as Long, byval dwReserved as Long) as Long
private Const EWX_FORCE = 4
private Const EWX_LOGOFF = 0
private Const EWX_REBOOT = 2
private Const EWX_SHUTDOWN = 1
private Sub Command1_Click()
ExitWindowsEx EWX_FORCE Or EWX_SHUTDOWN, 0&
End Sub
Aaron Young
Analyst Programmer
ajyoung@pressenter.com
aarony@redwingsoftware.com
highlander
February 8th, 2000, 03:37 PM
I tried this and It's not working for me.
I'm calling this proc from form unload event and it's not doing any thing.
At the moment i'm on NT4.
Thanks for your help.
Lothar Haensler
February 9th, 2000, 01:56 AM
you need to call AdjustTokenPrivileges before you are allowed to shutdown an NT workstation.
MSDN article Q168796 has all details including sourcecode.
Chris Eastwood
February 9th, 2000, 02:43 AM
Lothar's right, and I have some code right here that does it (author unknown). The code checks what version of windows you are running before attempting a shutdown :
You'll need some buttons on your form (cmdForceShutdown, cmdLogoff, cmdForceLogoff, exit, and cmdShutDown), you'll also need a listbox (systemMessage) and a textbox (systemType)
option Explicit
'
private Const EWX_LogOff as Long = 0
private Const EWX_SHUTDOWN as Long = 1
private Const EWX_REBOOT as Long = 2
private Const EWX_FORCE as Long = 4
private Const EWX_POWEROFF as Long = 8
'
'The ExitWindowsEx function either logs off, shuts down, or shuts
'down and restarts the system.
'
private Declare Function ExitWindowsEx Lib "user32" _
(byval dwOptions as Long, _
byval dwReserved as Long) as Long
'
'The GetLastError function returns the calling thread's last-error
'code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
private Declare Function GetLastError Lib "kernel32" () as Long
'
private Const mlngWindows95 = 0
private Const mlngWindowsNT = 1
public glngWhichWindows32 as Long
'The GetVersion function returns the operating system in use.
private Declare Function GetVersion Lib "kernel32" () as Long
'
private Type LUID
UsedPart as Long
IgnoredForNowHigh32BitPart as Long
End Type
'
private Type LUID_AND_ATTRIBUTES
TheLuid as LUID
Attributes as Long
End Type
'
private Type TOKEN_PRIVILEGES
PrivilegeCount as Long
TheLuid as LUID
Attributes as Long
End Type
'The GetCurrentProcess function returns a pseudohandle for the
'current process.
private Declare Function GetCurrentProcess Lib "kernel32" () as Long
'The OpenProcessToken function opens the access token associated with
'a process.
private Declare Function OpenProcessToken Lib "advapi32" _
(byval ProcessHandle as Long, _
byval DesiredAccess as Long, _
TokenHandle as Long) as Long
'The LookupPrivilegeValue function retrieves the locally unique
'identifier (LUID) used on a specified system to locally represent
'the specified privilege name.
private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(byval lpSystemName as string, _
byval lpName as string, _
lpLuid as LUID) as Long
'The AdjustTokenPrivileges function enables or disables privileges
'in the specified access token. Enabling or disabling privileges
'in an access token requires TOKEN_ADJUST_PRIVILEGES access.
private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(byval TokenHandle as Long, _
byval DisableAllPrivileges as Long, _
NewState as TOKEN_PRIVILEGES, _
byval BufferLength as Long, _
PreviousState as TOKEN_PRIVILEGES, _
ReturnLength as Long) as Long
private Declare Sub SetLastError Lib "kernel32" _
(byval dwErrCode as Long)
'
private Sub AdjustToken()
'********************************************************************
'* This procedure sets the proper privileges to allow a log off or a
'* shut down to occur under Windows NT.
'********************************************************************
Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2
'
Dim hdlProcessHandle as Long
Dim hdlTokenHandle as Long
Dim tmpLuid as LUID
Dim tkp as TOKEN_PRIVILEGES
Dim tkpNewButIgnored as TOKEN_PRIVILEGES
Dim lBufferNeeded as Long
'set the error code of the last thread to zero using the
'SetLast error function. Do this so that the GetLastError
'function does not return a value other than zero for no
'apparent reason.
SetLastError 0
'Use the GetCurrentProcess function to set the hdlProcessHandle
'variable.
hdlProcessHandle = GetCurrentProcess()
If GetLastError <> 0 then
systemMessage.AddItem "GetCurrentProcess error==" & GetLastError
End If
OpenProcessToken hdlProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
If GetLastError <> 0 then
systemMessage.AddItem "OpenProcessToken error==" & GetLastError
End If
'get the LUID for shutdown privilege
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
If GetLastError <> 0 then
systemMessage.AddItem "LookupPrivilegeValue error==" & GetLastError
End If
tkp.PrivilegeCount = 1 ' One privilege to set
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED
'Enable the shutdown privilege in the access token of this process
AdjustTokenPrivileges hdlTokenHandle, _
false, _
tkp, _
len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded
If GetLastError <> 0 then
systemMessage.AddItem "AdjustTokenPrivileges error==" & GetLastError
End If
End Sub
'
private Sub cmdLogoff_Click()
ExitWindowsEx (EWX_LogOff), &HFFFF
systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
'
private Sub cmdForceLogoff_Click()
ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
'
private Sub cmdShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT then
AdjustToken
systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
'
private Sub cmdForceShutdown_Click()
If glngWhichWindows32 = mlngWindowsNT then
AdjustToken
systemMessage.AddItem "Post-AdjustToken GetLastError " & GetLastError
End If
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
systemMessage.AddItem "ExitWindowsEx's GetLastError " & GetLastError
End Sub
'
private Sub exit_Click()
End
End Sub
'
private Sub Form_Load()
'********************************************************************
'* When the project starts, check the operating system used by
'* calling the GetVersion function.
'********************************************************************
Dim lngVersion as Long
lngVersion = GetVersion()
If ((lngVersion And &H80000000) = 0) then
glngWhichWindows32 = mlngWindowsNT
systemType.Text = "Windows NT"
else
glngWhichWindows32 = mlngWindows95
systemType.Text = "Windows 95"
End If
'
End Sub
Chris Eastwood
CodeGuru - the website for developers
http://codeguru.developer.com/vb