2

I'm running a VBScript that communicates to an exe file in Windows 7.

The VBScript works great!

The issues I have, is that once the PC has been in locked, goes to sleep or hiberation the VBScript doesn't communicate with the exe application.

The VBScript is running (I have a log that tells me every time a loop is complete, but its not communicating to the exe.

Below is code that is not working when the PC is locked.

Set WSHShell = WScript.CreateObject("WScript.Shell")
' info for exporting data
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Dim fso, MyFile, FileName, TextLine, cycles
Dim I
I = 0
Dim n
n = .1 'how often the program saves the data (in minutes)
cycles = 2 'how many times it will save
FileName = "C:\Users\Desktop\new.txt" 'location where the log file will save
Dim sl
sl = n * 60000 'change from seconds to ms for the sleep function
Set fso = CreateObject("Scripting.FileSystemObject")
' Open the file for output
Set MyFile = fso.OpenTextFile(FileName, ForAppending, True, TristateTrue)
' Write to the file.
MyFile.WriteLine "Log file for recording data from Yokogawa MX100 (" & cycles 
& " cycles)"
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
Do while I < cycles
a = Now()
WScript.Sleep 1000
WSHShell.Run "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.AppActivate "MXStandardE.exe"
WScript.Sleep 1000
WSHShell.SendKeys "%A"
WScript.Sleep 1000
WSHShell.SendKeys "{DOWN}"
WScript.Sleep 1000
WSHShell.SendKeys "{ENTER}"
I = I + 1
MyFile.Writeline I & " of " & cycles & " at " & a & " --time of each cycle is 
" & n & " minutes"
WScript.Sleep sl 'when sl is used loop time is in minutes
Loop
MyFile.Close
MsgBox ("Script has completed")
  • 3
    When a PC is entering sleep mode or hibernate, operation is suspended, so obviously operation of your script will be interrupted in those cases (otherwise the PC wouldn't be sleeping/hibernating in the first place). Simply locking the screen shouldn't have the same effect, though (unless the computer enters sleep mode right away when being locked). How *exactly* is your script "communicating"? And how do you know it doesn't when the PC is locked? Can you show more of your code? The snippet in your question doesn't show anything that should be interrupted by locking the screen. – Ansgar Wiechers Jul 24 '17 at 19:51
  • Thanks Ansgar, I edited my original question and added the complete code. The "Alt, Down, Enter" function is to save data from a DAQ. When I check the network I can see it is not saving, but the log is writing to the text file. If I see the application is not saving, I can go to the PC unlock and the application is saving again. Thanks again, -Verrell – Verrell Haywood Jul 25 '17 at 11:54

2 Answers2

3

As Ansgar already said, it's pretty obvious that nothing will work while the PC sleeps or hibernates. In the case where the PC is locked, techniques that rely on window management or direct input such as SendKeys won't work as expected, because the user's session, along with user-level applications, is essentially shelved to make way for the login screen or another user.

You might want to do some research into the SendMessage/PostMessage API, or you can stop using VBScript and replace it with a Scheduled Task or system service that runs using a local service account, assuming you just want to execute an exe without any UI interaction.

BoffinBrain
  • 6,337
  • 6
  • 33
  • 59
0

The question is: do you need to go under hibernation or pc suspension? If not, you should simply configure or turn off those settings. By only locking the session, the vbscript should be run and generate an outpout without any trouble. By checking and configuring advanced windows energy settings it might help you solving this.

Malbordio
  • 92
  • 3
  • 4
  • 20