First off, I'm pretty newbie to the VB language, VB scripting and VB.net programming, but rather good at other languages and platforms.
My goal is to run a simple VB-based program from the command line and have it popping a message box up (that part I figured it out). In addition, I want the message box (as well as the script) to terminate if the computer goes in sleep mode (or if it resumes from it) in the mean time.
I found this code on the VBForums about the handler:
'add the handlers for the system standby, resume, and shutdown
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
AddHandler Microsoft.Win32.SystemEvents.SessionEnding, AddressOf SessionEnding
[...]
Private Sub PowerModeChanged(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
Select Case e.Mode
Case Microsoft.Win32.PowerModes.Resume
'windows is resuming from sleep
Case Microsoft.Win32.PowerModes.Suspend
'goodnite windows
End Select
End Sub
Private Sub SessionEnding(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.SessionEndingEventArgs)
Select Case e.Reason
Case Microsoft.Win32.SessionEndReasons.Logoff
'logoff
Case Microsoft.Win32.SessionEndReasons.SystemShutdown
'shutdown
End Select
End Sub
So I made a .vbs file that uses the above (first version) and ran it:
Sub PowerModeChanged(ByVal sender As System.Object, _
ByVal e As Microsoft.Win32.PowerModeChangedEventArgs)
Select Case e.Mode
Case Microsoft.Win32.PowerModes.Resume
'windows is resuming from sleep
WScript.Quit
Case Microsoft.Win32.PowerModes.Suspend
'goodnite windows
WScript.Quit
End Select
End Sub
Sub Main()
Set objArgs = WScript.Arguments
msgText = objArgs(0)
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, AddressOf PowerModeChanged
MsgBox msgText
End Sub
Main()
But I got all kinds of syntax errors (on As
, AddressOf
, etc.). After some tests and googling around I came to realize that some VB entities appears to be more type-strict and has a more evolved syntax than others. So here's my second version which almost succeeds to pass the syntax phase:
Sub PowerModeChanged(sender, e)
Select Case e.Mode
Case Microsoft.Win32.PowerModes.Resume
'windows is resuming from sleep
WScript.Quit
Case Microsoft.Win32.PowerModes.Suspend
'goodnite windows
WScript.Quit
End Select
End Sub
Sub Main()
Set objArgs = WScript.Arguments
msgText = objArgs(0)
AddHandler Microsoft.Win32.SystemEvents.PowerModeChanged, PowerModeChanged
MsgBox msgText
End Sub
Main()
Now the interpreter complains about "Require: 'Microsoft'" at the AddHandler
line. At this point I don't know what to do. I tried Microsoft.AddHandler
but did not work.
So I would appreciate if you could help me fix this line, and tell me if there's any other thing that could make this little program working.
Thank you.