Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shutting down in Win2K 1

Status
Not open for further replies.

elziko

Programmer
Nov 7, 2000
486
GB
I'm trying to use this code (provided by GTO - thanks) to shut down my computer:

"Add the following API Declares and Constants to
a BAS Module:

Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
wReserved&)

Global Const EWX_FORCE = 4 'constants needed for exiting Windows
Global Const EWX_LOGOFF = 0
Global Const EWX_REBOOT = 2
Global Const EWX_SHUTDOWN = 1

Then you Shutdown/reboot/logoff windows with the following call:

lresult = ExitWindowsEx(EWX_SHUTDOWN, 0&) 'shut down the computer

Note: Replace the first parameter of the ExitWindowsEx function call
with the appropriate CONSTANT."

This works for restarting but not for shutting down. Anyone help me? Remeber I'm using WIN 2000!

Thanks

elziko
 
Thanks Rob. Seeing as it is the same declaration I was a bit dubious but I tried what your link says and sure enough it LogsOff OK but wont restart/shutdown.

Has anyone actually got something like this working on Win 2K?

OR: is there anything, maybe haning around in the windows directory, that will sg=hutdown a computer?

Thanks Again

elziko
 
It may have something to do with ACPI and Win2K. Hopefully someone out there can shed a little light on the problem.

Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Try this page. It uses a different api call that you may be able pull out for your purposes...


Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" Alias "InitiateSystemShutdownA" (ByVal lpMachineName As String, ByVal lpMessage As String, ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, ByVal bRebootAfterShutdown As Long) As Long

Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Thanks, things ae happening now!

I do need to power off after the computer has shutdown though. Any advice on changing the code to do this, no arguments seem to cover this.

Cheers

elziko
 
In Win2K (and maybe NT) there is a reg key of something like PowerOffAfterShutdown (something like that). You can probably find the key at
Later, Rob
robschultz@yahoo.com
-Focus on the solution to the problem, not the obstacles in the way.-
 
Tried the registry setting. I found it. I changed it. But it still doesn't power off, just shuts down and informs me that I can power off.

Any more help would be great. I got in working in Win95 no problem so it is a Win2K problem!

HELP!

Thanks

elziko
 
'Module Declarations

Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_FORCE As Long = 4
Private Const EWX_REBOOT = 2

Private Declare Function ExitWindowsEx Lib "user32" (ByVal _
dwOptions As Long, ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcessToken Lib "advapi32" (ByVal _
ProcessHandle As Long, _
ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, ByVal lpName As String, lpLuid _
As LUID) As Long
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

'A procedure :

Private Sub AdjustToken()
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

hdlProcessHandle = GetCurrentProcess()
OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or _
TOKEN_QUERY), hdlTokenHandle

' Get the LUID for shutdown privilege.
LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

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

End Sub

'Another procedure :

Public Sub ShutDown()
AdjustToken
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
End Sub

'a command button :

Private Sub Command1_Click()
ShutDown
End Sub

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
edderic,

thanks for your help but I'm still having the same problem. All the code given to me does not make **WIN2K** power off after a shutdown.

Has anyone actually tried this or any other code in win 2k?

For what its worth, I know it can be done because it powers down fine when I go through the start menu!

Hope you're not all too sick of my shut down problems but it is imporatant I find a solution.

I'm still scouring the net and cant find an answer ANYWHERE!

Thanks.... Anymore Help?!?!? ;)

elziko
 
>thanks for your help but I'm still having the same >problem. All the code given to me does not make **WIN2K** >power off after a shutdown.

Windows 2000 is Shutdown ,Yes or NO ?
The screen on your monitor is Black ? So the power is OFF on the computer of NOT (what computer you have?)

Eric De Decker
vbg.be@vbgroup.nl

Licence And Copy Protection AxtiveX
Source CodeBook for the programmer
 
This is strange! I had a feeling that something odd happened and your comments have confirmed this. When I tried it the computer seemed to shut the monitor down, but literally milliseconds later the monitor came back on and the usual "It Is Safe To Turn Your Computer" message was there.

I have a BCM mother board (VIA chipset). Maybe its some kind of bios/mobo problem?

However, I can still shutdoen & poweroff from the startmenu with no problem. Do you think it would be possible to activate the routine that the start menu uses through th API?

I'm going mad, but thanks,

elziko
 
I am having the same problem that you are, only with Windows NT 4.0. In my studies, I have found that the registry entry HKLM/SOFTWARE/Microsoft/WindowsNT/CurrentVersion/Winlogon/PowerDownAfterShutdown only gives you the option to Power Down from their Shutdown Screens. This function only works if you have the proper Microsoft Hadware Abstraction Layer Module (hal.dll) installed. If this option works, I would try calling ExitWindowsEX(EWX_POWEROFF), &HFFFF, where EWX_POWEROFF is a Long variable set to 8. If the registry entry works for you, this should work as well. Unfortunately niether of them work for me, so if anyone has a solution for NT4.0, I would love to find out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top