Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools


This demo will show you how to shutdown remote machine in a specified interval. When you shutdown machine you will see following warning message.






For Quick Demo

1. Start a new project in Visual Basic. Form1 is created by default.
2. Place 2 Command Buttons on the form.
3. Place 3 TextBox on the form
4. Place a Timer on the form
3. Add the following code to the Form1 code window:

Click here to copy the following block
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


Private Declare Function AbortSystemShutdown Lib "advapi32.dll" Alias _
    "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long

Dim Timeleft As Long

'//To diaplay API Error
Const FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100
Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000
Const LANG_NEUTRAL = &H0
Const SUBLANG_DEFAULT = &H1
Private Declare Function GetLastError Lib "kernel32" () As Long
Private Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
Private Declare Function FormatMessage Lib "kernel32" Alias "FormatMessageA" (ByVal dwFlags As Long, lpSource As Any, ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal lpBuffer As String, ByVal nSize As Long, Arguments As Long) As Long
Function GetAPIErrorDesc(ErrorCode As Long) As String
  Dim Buffer As String
  Buffer = Space(200)
  FormatMessage FORMAT_MESSAGE_FROM_SYSTEM, ByVal 0&, ErrorCode, LANG_NEUTRAL, Buffer, 200, ByVal 0&
  GetAPIErrorDesc = Buffer
End Function
'////////////////////////////////////////

Private Sub Command1_Click()
  'Initiate a System ShutDown on the remote PC called MYPC, a window will display
  'the message and after <<< n >>> seconds the system will shut down forcing every
  'application to stop. The system will be restarted
  Dim ret
  ret = InitiateSystemShutdown(Text1, Text2, Text3, True, True)
  If ret = 0 Then
    MsgBox GetAPIErrorDesc(Err.LastDllError), vbCritical
  Else
    Command1.Enabled = False
    Command2.Enabled = True
    Timer1.Enabled = True
    Timeleft = Text3
  End If
End Sub

Private Sub Command2_Click()
  'If before the 30 seconds elapse the user calls this function, the Shutdown
  'process will be aborted
  Dim ret
  ret = AbortSystemShutdown(Text1)
  If ret = 0 Then
    MsgBox GetAPIErrorDesc(Err.LastDllError), vbCritical
  Else
    Me.Caption = "Shutdown Canceled"
    Command1.Enabled = True
    Command2.Enabled = False
    Timer1.Enabled = False
  End If
End Sub


Private Sub Form_Load()
  Command1.Caption = "Start Shutdown"
  Command2.Caption = "Stop Shutdown"
  Text1 = "\\machine1"
  Text2 = "API Demo: Shutdown message"
  Text3 = 30

  Command2.Enabled = False
  Timer1.Interval = 1000                 '1 second
  Timer1.Enabled = False
End Sub

Private Sub Timer1_Timer()
  Me.Caption = "Shutting down [" & Text1 & "]...... (" & Timeleft & ") Seconds left"
  Timeleft = Timeleft - 1
End Sub


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.