by Abdus Salam Naeem (3 Submissions)
Category: Windows API Call/Explanation
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Sat 26th January 2002
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Restart or Shutdown your computer using APIs
API Declarations
'Name the form as form1
'FROM TOOL BOX TAKE
'
'one command buttons array on ur form and named it command1
'index 1 for shutdown and index 2 for restart
'one command button for exit and named it command2
'one command button for ok and named it command3
'one timer control and named it timer1
'tow label control and named it label1 and label2
'one text box and named it text1
'In case of any problem u can trace me at
'[email protected]
'==========================================================
Option Explicit
Private tempTime As Variant
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_RESTART As Long = 2
Private Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
If Index = 1 Then
'shut down the computer
lngResult = ExitWindowsEx(EWX_SHUTDOWN, 0&)
Else
'restart the computer
lngResult = ExitWindowsEx(EWX_RESTART, 0&)
End If
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
If Val(Text1.Text) > 60 Or Val(Text1.Text) <= 0 Then
MsgBox "dont enter value greater than 60 or less then 0", vbOKOnly, "wrong value"
Text1.Text = 0
End If
Label2.Caption = "wait---"
tempTime = Val(Text1.Text)
End Sub
Private Sub Form_Load()
Label1.Alignment = vbCenter
End Sub
Private Sub mncopy_Click()
frmAbout.Show
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 13 Then
If Val(Text1.Text) > 60 Or Val(Text1.Text) <= 0 Then
MsgBox "dont enter value greater than 60 or less then 0", vbOKOnly, "wrong value"
Text1.Text = 0
End If
Label2.Caption = "wait---"
tempTime = Val(Text1.Text)
End If
End Sub
Private Sub Timer1_Timer()
Static timediff As Variant
Static tempTime2 As Variant
If tempTime > 0 Then
tempTime2 = tempTime2 + 1 'to add seconds for each call
Label1.Caption = tempTime2
If tempTime2 = 60 Then 'if one minute complete
timediff = timediff + 1
Label1.Caption = timediff
tempTime2 = 0 'again from 0 second
End If
If timediff = tempTime Then
autoshutdown
End If
End If
End Sub
Private Sub autoshutdown()
Dim lngResult As Long
lngResult = ExitWindowsEx(EWX_SHUTDOWN, 0&)
End Sub