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.
For the benefit of all those who are new to the VB, VB.net and VBScript melting pot, I realized that there are as much differences between VB(.net) and VBScript as there are similarities. Not to mention that run-time errors raised from running erroneous VBScripts mostly give no clue to solve the actual problem(s).
My intent was to run a simple program in Basic but I ended up mixing VB(.net) and VBScript. Sorry to those who tried to follow me up btw.
As far as AddHandler is concerned, it appears I cannot call this function in a VBScript so I needed to write it in VB(.net); thus a .vbs file is not the solution.
In the end I installed VS (Visual Studio) and had my program compiled and run. VB sources have the .vb extension and results in an executable once compiled. VS offers a lot of instant-completions while coding, which help writing code right off the first time. Contrary to what I thought and commented, even for simplistic Basic programs, it is recommended to have VS (the Express version costs nothing).
I believe the alternative to VS is as per Trigger's suggestion.
Again sorry for all this confusion. Hope this will help other newbies.