-4

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.

Maitre Bart
  • 157
  • 1
  • 11
  • `So I made a .vbs file` - made with what? Do you actually have Visual Studio installed? – GSerg Apr 04 '15 at 19:47
  • One does not really need an IDE to write code in a file. Of coarse it helps for medium and large projects. In my case I uses notepad++ because it has few lines. – Maitre Bart Apr 05 '15 at 14:17
  • Rephrased: I would say that one does not really need an IDE to write code in a file, unless the interpreter/compiler outputs insufficient information about the errors (which is apparently the case here). In my case I used notepad++ because it has few lines, but I'm now considering to switch to the IDE. – Maitre Bart Apr 05 '15 at 14:39
  • What I'm trying to understand is what language you are actually working with / think you are working with. It would seem you had vbsript in your mind, went online, found code samples for vb.net, tried to put them in a vbs file without realising they were completely different languages, then posted them here which made everyone think you intended to use vb.net in the first place. If you don't have the IDE installed, you probably don't have the VB compiler either and you can't run vb.net code at all. – GSerg Apr 05 '15 at 14:39
  • If you actually want a standalone non-exe executable script file, that's vbscript, but it does not have forms that can pop up and hang around. You'd be limited to `MsgBox`. – GSerg Apr 05 '15 at 14:42
  • Well your summary (GSerg) pretty well describes my situation. As I mentioned, I'm a newbee with VB. My confusion came from that fact that there's a great deal of confusions for me about VB, VB.net, VBScript. At first I did not know there were so many differences between them: for me Basic is Basic: same rules, same syntax. Well, it appears not; that is what I learned today. The other thing I learned today is that AddHandler cannot be called from VBScript. So in the end, I installed VS (since it's apparently the only way I can call AddHandler) and produced an executable that works now. – Maitre Bart Apr 05 '15 at 15:40

3 Answers3

0

Well here's a useless answer. As it's vbscript not VB.NET as his code is.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") 
Set objEvents = objWMIService.ExecNotificationQuery _
    ("SELECT * FROM Win32_PowerManagementEvent")

Do
    Set objReceivedEvent = objEvents.NextEvent
    wscript.echo objReceivedEvent.EventType
Loop

Use with CScript.

EventType

Data type: uint16

Access type: Read-only

Type of change in the system power state.

Value Meaning

4 Entering Suspend

7 Resume from Suspend

10 Power Status Change

11 OEM Event

18 Resume Automatic

OEMEventCode

Data type: uint16

Access type: Read-only

System power state defined by the original equipment manufacturer (OEM) when the EventType property of this class is set to 11 (OEM Event); otherwise, this property is set to NULL. OEM events are generated when an APM BIOS signals an APM OEM event. OEM event codes are in the range 0x0200h - 0x02FFh.

0

Your code is VB.NET not VBScript. You need to use VB.NET to compile your code and run it. There are instructions here on HOW TO COMPILE YOUR PROGRAM CODE.

http://social.msdn.microsoft.com/Forums/en-US/adcae113-4758-481a-a367-60d5d14d97d6/this-is-how-to-turn-vbs-and-js-files-into-exe-files-from-the-command-line-without-third-party-tools?forum=scripting

Trigger
  • 11
  • 2
  • 1
    My other answer has code that he can use. But the question is "How to make VB.NET code run". The answer is you have to compile (there's no IDE or debugger). – Trigger Apr 05 '15 at 05:55
0

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.

Maitre Bart
  • 157
  • 1
  • 11