CANoe vbs system variable control [duplicate] - vbscript

This question already exists:
CANoe reconnect to instance using com [closed]
Closed 10 months ago.
I am developing vbs script to launch CANoe and then readout system variable. I am able to launch CANoe successfully and having problem in reading system variable.
My code is as follow:
Set App = CreateObject("CANoe.Application")
Wscript.Sleep(3000)
If Err.Number Then
Wscript.Echo "Error in opening config file, quit the script"
WScript.Quit
Else
Wscript.Echo "CAnoe Launched successfully"
End If
'App.Open (ScriptPath & "\CANoe_VW_T7ACS.cfg")
App.Open(WScript.Arguments.Item(0))
Wscript.Sleep(3000)
If Err.Number Then
Wscript.Echo "Error in opening config file, quit the script"
WScript.Quit
Else
Wscript.Echo "CAnoe Config Loaded successfully"
End If
If App.Configuration.ReadOnly Then
Wscript.Echo "The configuration is read-only."
Else
Wscript.Echo "The configuration is writeable."
End If
App.Measurement.Start
WScript.Sleep(5000)
WScript.Sleep(5000)
WScript.Sleep(5000)
Set Measurement = App.Measurement
Wscript.ConnectObject Measurement, "Measurement_"
Set Pedal = App.CAPL.cclSysVarSetInteger("Relais::Relais_S_C1", 0)
Wscript.Echo Pedal
Set App = Nothing
Set Measurement = Nothing
The error I am getting at line
Set Pedal = App.CAPL.cclSysVarSetInteger("Relais::Relais_S_C1", 0)
is,
enter image description here
what can be the problem ?

Related

Windows 10 "Pin to Start" in delphi [duplicate]

This question already has answers here:
Loop through IContextMenu
(2 answers)
Closed 4 years ago.
i am trying to write a delphi application that automatically pins apps to the start menu, so they are easily visible in tablet mode.
I did some research and all i found was a VBScript that worked (see the code below).
So i tried to open the VBScript in my delphi application with Shell Execute
ShellExecute(Handle, 'open', Pchar('C:\tmp\VBScript.vbs'), 'C:\WINDOWS\system32\ notepad.exe', nil, SW_NORMAL);
But if i try to run the script with shell execute there is no "Pin to start" verb. Otherwise it works if i open it directly from the explorer.
Whats the difference between running the file directly from the windows explorer or from delphi with shell execute?
Or do you have an idea how i could try to pin apps only with delphi?
VBScript:
Dim Argumente
Dim File, objFSO
Dim strFolder, strExecutable
Set objShell = CreateObject("Shell.Application")
Set objFSO = CreateObject("Scripting.FileSystemObject")
arg0 = wscript.arguments.unnamed.item("0")
arg1 = wscript.arguments.unnamed.item("1")
File = arg0&arg1
If (objFSO.FileExists(File )) Then
Else
WScript.Echo "File " & File & " gibt es nicht. Script fehlgeschlagen"
WScript.Quit(2)
End If
strFolder = arg0
strExecutable = arg1
WScript.Echo "Folder:" & strFolder & ""
WScript.Echo "File:" & strExecutable & ""
Set objFolder = objShell.Namespace(strFolder)
Set objFolderItem = objFolder.ParseName(strExecutable)
Set colVerbs = objFolderItem.Verbs
'uncomment this section to display the available verbs
For Each objVerb In colVerbs
If objVerb <> "" Then
WScript.Echo objVerb
End If
Next
'Loop through the verbs and if PIN is found then 'DoIt' (execute)
blnOptionFound = False
For Each objVerb In colVerbs
If Replace(objVerb.name, "&", "") = "Pin to Start" Then
objVerb.DoIt
blnOptionFound = True
WScript.Echo "The application '" & strExecutable & "' was just Pinned to the Start Menu."
WScript.Quit(0)
End If
Next
if blnOptionFound = false then
WScript.Echo "The application '" & strExecutable & "' was already pinned to the Start Menu."
WScript.Quit(1)
end if
There is a special folder for pinned start menu items into which you just place a shortcut to your program
https://superuser.com/a/171129/442240
So no need to use complex scipts for this

"connection.Children(0)" triggers "The enumerator of the collection cannot find en element with the specified index."

I am using a VBScript to login automatically into SAP GUI.
It opens automatically the SAP GUI window, it loads the SAP server, but it doesn't populate automatically the user and password fields (remain blank).
It also gives a script error on line 52, char 4:
The enumerator of the collection cannot find en element with the specified index.
The code is the following:
REM The following script was written to log into the SAP server automatically.
REM To view historical information and credit for this script please see
REM the following thread on the SAP Community Network:
REM http://scn.sap.com/thread/3763970
REM This script was last updated by Paul Street on 7/1/15
REM Directives
Option Explicit
REM Variables! Must declare before using because of Option Explicit
Dim WSHShell, SAPGUIPath, SID, InstanceNo, WinTitle, SapGuiAuto, application, connection, session
REM Main
Set WSHShell = WScript.CreateObject("WScript.Shell")
If IsObject(WSHShell) Then
REM Set the path to the SAP GUI directory
SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
REM Set the SAP system ID
SID = "eaiserver.domain.com"
REM Set the instance number of the SAP system
InstanceNo = "38"
REM Starts the SAP GUI
WSHShell.Exec SAPGUIPath & "SAPgui.exe " & SID & " " & _
InstanceNo
REM Set the title of the SAP GUI window here
WinTitle = "SAP"
While Not WSHShell.AppActivate(WinTitle)
WScript.Sleep 250
Wend
Set WSHShell = Nothing
End If
REM Remove this if you need to test the above script and want a message box at the end launching the login screen.
REM MsgBox "Here now your script..."
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").maximize
session.findById("wnd[0]/usr/txtRSYST-MANDT").Text = "100"
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "I'veInsertedtheCorrectUsernameHere"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "I'veInsertedtheCorrectPassHere"
session.findById("wnd[0]/usr/txtRSYST-LANGU").Text = "PT"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 10
session.findById("wnd[0]").sendVKey 0
Thanks for the help!
I am using the exact same script, which I found somewhere on the SAP help forums.
When I've encountered this issue before it's usually because the SAP GUI window was loaded AFTER lines
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "username"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "password"
There are two ways to fix this particular script. One is to add a MsgBox that will pause the script, but give SAP GUI enough time to load. The other is to add WScript.Sleep(<a few seconds>) to allow SAP GUI to load. Like so ...
Note that the below code has BOTH examples, but only 1 is necessary. I prefer the .Sleep() because it requires no external input from a user.
If IsObject(WSHShell) Then
' Removed for clarity
End If
MsgBox "Click OK to continue" ' <-- MsgBox to pause script
WScript.Sleep(5000) ' <--- Wait 5 seconds for SAP GUI to load
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
If Not IsObject(connection) Then
Set connection = application.Children(0)
End If
If Not IsObject(session) Then
Set session = connection.Children(0)
End If
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
session.findById("wnd[0]").resizeWorkingPane 164,40,false
session.findById("wnd[0]/usr/txtRSYST-BNAME").text = "username"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").text = "password"
session.findById("wnd[0]/usr/pwdRSYST-BCODE").setFocus
session.findById("wnd[0]/usr/pwdRSYST-BCODE").caretPosition = 14
session.findById("wnd[0]").sendVKey 0
And of course, storing username and password in plain text is not a good practice. However, obfuscating passwords with the VBScript InputBox() is not possible. You will have to use the command line, or create an IE object which is outside the scope of this question
Here's some VBS code that tries to wait for SAP to login and load properly. It worked well for me so far.
Function SAP_start_and_login(connection_string, use_sso, user, pass)
WScript.Echo "executing function SAP_start_and_login()"
REM Variables! Must declare before using because of Option Explicit
Dim WSHShell, SAPGUIPath
REM Main
Set WSHShell = WScript.CreateObject("WScript.Shell")
If IsObject(WSHShell) Then
REM Set the path to the SAP GUI directory
SAPGUIPath = "C:\Program Files (x86)\SAP\FrontEnd\SAPgui\"
REM Starts the SAP GUI
if use_sso then
WSHShell.Exec SAPGUIPath & "sapshcut.exe -client={your_client} -sysname=whatevah -gui=" & connection_string & " -snc_name={your_snc_name} -snc_qop={your_snc_qop}"
else
WSHShell.Exec SAPGUIPath & "sapshcut.exe -client={your_client} -sysname=whatevah -gui=" & connection_string & " -user=" & user & " -pw=" & pass
end if
REM WSHShell.Exec SAPGUIPath & "saplogon.exe """ & system & """"
Set WSHShell = Nothing
WScript.Echo "{waiting for SAP to finish loading..}"
SAP_start_and_login = WaitForSAP(connection_string, 50)
WScript.Sleep 1000
End If
End Function
Function CheckSapIsRunning(connection_string)
Running = False
Ready = False
If Not IsObject(application) Then
On Error Resume Next
Set SapGuiAuto = GetObject("SAPGUI")
If (Err.Number <> 0) Then
WScript.Echo "SAPGUI object not found yet"
' Error raised, object not found.
' Restore normal error handling.
On Error GoTo 0
Else
'WScript.Echo "einai ok"
' Object found.
' Restore normal error handling.
On Error GoTo 0
Set application = SapGuiAuto.GetScriptingEngine
If application.Connections.Count() > 0 Then
For i = 0 To (application.Connections.Count()-1)
REM WScript.Echo i
Set Connection = application.Children(0+i)
Conn = Connection.ConnectionString()
REM WScript.Echo Conn
If InStr(Conn, connection_string) Then
Running = True
Exit For
End If
Next
End If
If Running Then
REM WScript.Echo connection_string + " is running!"
REM WScript.Echo "sessions="+CStr(connection.Children.Count)
If Not IsObject(session) Then
if connection.Children.Count > 0 then
Set session = connection.Children(0)
If IsObject(WScript) Then
WScript.ConnectObject session, "on"
WScript.ConnectObject application, "on"
End If
If not session.Busy then
Ready=True
REM WScript.Echo connection_string + " session is ready!"
Dim sapWindow
set sapWindow = session.findById("wnd[0]", False)
if sapWindow is Nothing then
WScript.Echo connection_string + " sap window element not there yet"
else
WScript.Echo connection_string + " is loaded and visible!"
end if
Else
WScript.Echo connection_string + " session is busy!"
end if
else
WScript.Echo connection_string + " session not loaded yet!"
end if
End If
else
WScript.Echo connection_string + " not running!"
End If
End If
End If
REM WScript.Echo application.Connections.Count()
CheckSapIsRunning = Ready
End Function
Function WaitForSAP(connection_string, timeout)
counter = 0
returnValue = False
While NOT CheckSapIsRunning(connection_string) AND counter < timeout
counter = counter + 1
WScript.Sleep 1000
REM WScript.Echo connection_string + " is not ready"
WEnd
if counter = timeout then
WScript.Echo "timeout of " + CStr(timeout) + " seconds reached."
else
returnValue = True
end if
WaitForSAP = returnValue
End Function
This can be used like this
If SAP_start_and_login(connection_string, use_sso, username, password) Then
If Not IsObject(application) Then
Set SapGuiAuto = GetObject("SAPGUI")
Set application = SapGuiAuto.GetScriptingEngine
End If
...{the rest of your recorded script}
Else
WScript.Echo "Could not get a functioning SAP session"
WScript.Quit 1
End If
I'm using sapshcut.exe because it was easy to have both SSO and non SSO in a similar manner. The connection string is important in order to distinguish and identify the instance of SAP that you want to use in the rest of the script. It can be found from your current shortcut along with the SSO parameters that you might be using. If the SSO is true the user and pass parameters are not used. The timeout is also useful to avoid an endless loop if SAP is down or unreachable.
This solution does not need a static sleep in the script in order to wait for SAP to load which could prevent issues if SAP took more than expected to load. Also no time is wasted if it is loaded sooner than expected.

fix vbs to launch hide System File Checker [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have this vbs (part of the script was provided by hackoo) to launch sfc.exe to fix system files, but like I need to add some features, such as a message, and I need while running sfc.exe, display a message "wait" (hiding the sfc.exe window) and the end of the program, exit the final message, but the script does not work well (sfc.exe fails hiding the window and displays the final message before concluding)
Option Explicit
' Run as Admin
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
On Error Resume next
mensaje = MSGBOX ("Start System File Checker", vbOKCancel, "System File Checker")
If mensaje = vbOK Then
Dim ws,MyCommand,Execution
Set ws = createobject("wscript.shell")
MyCommand = "%windir%\system32\SFC.exe /SCANNOW"
Execution = ws.run(MyCommand,1,False)
objshell.run NewPath,vbhide
CALL MSGBOX ("System File Checker has finished", VBOKONLY, "System File Checker")
Else
CALL MSGBOX ("System File Checker has been canceled", VBOKONLY, "System File Checker")
End If
On Error GoTo 0
Note: missing part "wait" while running hidden sfc.exe
Thanks
To hide the console you should write it like that :
Change False to True for waiting until the process will finish and the value 1 to 0 to hide the console
Execution = ws.run(MyCommand,0,True)
And your code should look like this one :
Option Explicit
' Run as Admin
If Not WScript.Arguments.Named.Exists("elevate") Then
CreateObject("Shell.Application").ShellExecute WScript.FullName _
, WScript.ScriptFullName & " /elevate", "", "runas", 1
WScript.Quit
End If
Dim ws,MyCommand,Execution,Question
Question = MSGBOX ("Did you want to start the System File Checker ?", vbOKCancel+vbQuestion, "System File Checker")
If Question = vbOK Then
Set ws = createobject("wscript.shell")
MyCommand = "%windir%\system32\SFC.exe /SCANNOW"
Execution = ws.run(MyCommand,0,True)
If Execution = 0 Then
Call MSGBOX ("System File Checker has finished", VBOKONLY, "System File Checker")
Else
Call MSGBOX ("System File Checker has been canceled", VBOKONLY, "System File Checker")
Wscript.quit(1)
End If
Else
Call MSGBOX ("System File Checker has been canceled", VBOKONLY, "System File Checker")
Wscript.quit(1)
End If

VBS Freezes When Waiting on Cmd Prompt

Hard to make a concise title, but basically, I have started an instance of command prompt from VBS to run an exe, everything works great and I verify my feedback with msgboxes of output line. When the command prompt gets to a part that is loading and says Verifying File (XX%), the VBS does not run anymore. It does not crash, it simply never moves on from its line. I even have noticed that if I don't constantly Writeline, it will pause before that. So while I wait, I constantly write a 1. I dont see anywhere it could be in an infinite loop without showing me a messagebox.
Please help.
set shell = WScript.CreateObject("WScript.Shell")
set oExec = Shell.exec("cmd.exe")
do while Not oExec.StdOut.AtEndOfStream
junkChar = oExec.stdOut.Read(1)
message = message & junkChar
'errormsg = oExec.stderr.readline
if asc(junkChar) = 13 and message <> junkChar then
msgbox message '& len(message)
message = ""
end if
if right(message,1) = ">" and not bool1stCmd then
'msgbox(message)
msgbox("Command" & cmdArchive)
oExec.stdIn.Writeline cmdArchive
bool1stCmd = True
'oExec.StdIn.Write VbCrLf
elseif bool1stCmd then
if InStr(1,message,"Enter a command>")>0 then
msgbox "Enter a command!"
end if
oExec.stdIn.writeline "1"
end if
msgbox "Loop again!"
Loop
msgbox "exiting loop"

PDFCreator COM Script Run as Service Fails to see Jobs Printed

I have a PDFCreator script I compiled in PrimalScript so that I could execute it via svrany and the problem I am running into is the fact that when I run the service the print jobs from the script are never seen by the queue. If I run the vbscript or the compiled exe from my session it works fine.
Here is the code in my vbs file that runs against pdfcreator version 2.1.1.820
Dim strExt, intStatus, strDestFileName, strInputFileName, strReason
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("Shell.Application")
Set PDFCreatorQueue = CreateObject("PDFCreatorBeta.JobQueue")
strInputFileName = "C:\Temp\Test.txt"
strDestFileName = "C:\Temp\Test.pdf"
PDFProcess
' ** Sub Routine to render file as PDF
Sub PDFProcess
Dim objFolder, job, intStatPDFCreator, intPageCount
intPageCount = 1
WScript.Echo "PDF Destination Name: " & strDestFile
WScript.Echo "Initializing PDFCreator queue..."
intStatPDFCreator = PDFCreatorQueue.Initialize()
WScript.Echo "PDFCreator Object Status: " & intStatPDFCreator
If intStatPDFCreator = 0 Then
If Not objFSO.FileExists(strInputFileName) Then
WScript.Echo "PDFCreator: Can't find the file: " & strInputFileName
Else
WScript.Echo "Printing Page: " & strInputFileName
objShell.ShellExecute strInputFileName, "", "", "print"
WScript.Sleep 1000
WScript.Echo "Currently there are " & PDFCreatorQueue.Count & " job(s) in the queue"
End If
WScript.Echo "Waiting for the job to arrive at the queue..."
if Not(PDFCreatorQueue.WaitForJobs(intPageCount, 10)) Then
strReason = "The print job did not reach the queue within " & 10 & " seconds"
WScript.Echo strReason
intStatus = 0
Else
WScript.Echo "Currently there are " & PDFCreatorQueue.Count & " job(s) in the queue"
WScript.Echo "Getting job instance and merging"
PDFCreatorQueue.MergeAllJobs
while(PDFCreatorQueue.Count > 0)
Set job = PDFCreatorQueue.NextJob
WScript.Echo "Staging PDF File: " & strDestFileName
job.ConvertTo(strDestFileName)
WScript.sleep 5000
If Not(job.IsFinished Or job.IsSuccessful) Then
strReason = "Could not convert the file: " & strDestFileName
WScript.Echo strReason
intStatus = 0
Else
WScript.Echo "Job finished successfully"
End If
Wend
End If
WScript.Echo "Releasing the object"
PDFCreatorQueue.ReleaseCom()
Else
strReason = "Failed to create PDFCreator COM instance."
WScript.Echo strReason
intStatus = 0
End If
End Sub
The service I have running under my domain credentials and have modified the registry to allow it to run interactively
SERVICE_NAME: tgprintprocessor
TYPE : 110 WIN32_OWN_PROCESS (interactive)
STATE : 4 RUNNING
(STOPPABLE, PAUSABLE, ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
PID : 3752
FLAGS :
I have set interactive Services Detection to a running state.
reviewing the PDFCreator trace log I don't see any errors of any kind.
If I open PDFCreator printer and view the print queue, I do see the job hit the queue and exit, and can even pause the printer so the job stops in the printer queue but the com object queue is oblivious to its existence.
I also tried running PDFCreator.exe additionally as a service incase it needed to have an instance of the exe running in the background as I noticed the application itself does not fire up in task manager post print job submission like I typically see when executing the script manually.
What is my question, I guess I don't know where else to look and if there is maybe something I am missing that I can add to the above test script to attempt trapping the problem.
Resolution to this issue and behavior above is to change the driver isolation mode from the default NONE, to SHARED. I did this under Print Management Snapin. Took a while to figure it out....
If you are using WScript it will stop at the first wscript.echo. Make sure you are running with CScript.

Resources