Can't run batch file in shell

I’ve been trying to come up with a way to start and stop Windows services from a desktop app. I found an example batch file on the web that I copied into Xojo and added code to name the service and what function (start, stop, pause, resume). Running the program generates the batch file, but the shell does not execute it. Double clicking on the newly created file in Windows Explorer does cause it to execute and it does exactly what it is supposed to do, ask the user if he wants to make the changes then makes them. I have tried setting different shell properties, but have not found a way to make it work. The batch file code is unchanged from the sample from the web except for the last line which I added. Earlier another version without all the echoes also worked when double clicked.

[code]Sub ChangeService(SvcName As String, SvcAction As String)
dim f As FolderItem
dim t As TextOutputStream
f = GetFolderItem(“SvcChange.bat”)
if f.Exists Then f.Delete
t = TextOutputStream.Create(f)

t.WriteLine “:::::::::::::::::::::::::::::::::::::::::”
t.WriteLine “:: Automatically check & get admin rights”
t.WriteLine “:::::::::::::::::::::::::::::::::::::::::”
t.WriteLine “:@echo off”
t.WriteLine “CLS”
t.WriteLine “ECHO.”
t.WriteLine “ECHO =============================”
t.WriteLine “ECHO Running Admin shell”
t.WriteLine “ECHO =============================”

t.WriteLine “:checkPrivileges”
t.WriteLine “NET FILE 1>NUL 2>NUL”
t.WriteLine “if ‘%errorlevel%’ == ‘0’ ( goto gotPrivileges ) else ( goto getPrivileges )”

t.WriteLine “:getPrivileges”
t.WriteLine “if ‘%1’==‘ELEV’ (shift & goto gotPrivileges)”
t.WriteLine “ECHO.”
t.WriteLine “ECHO **************************************”
t.WriteLine “ECHO Invoking UAC for Privilege Escalation”
t.WriteLine “ECHO **************************************”

t.WriteLine “setlocal DisableDelayedExpansion”
t.WriteLine “set ““batchPath=%~0"””
t.WriteLine “setlocal EnableDelayedExpansion”
t.WriteLine “ECHO Set UAC = CreateObject^(”“Shell.Application””^) > “”%temp%\OEgetPrivileges.vbs"""
t.WriteLine “ECHO UAC.ShellExecute “”!batchPath!”", ““ELEV””, “”"", ““runas””, 1 >> “”%temp%\OEgetPrivileges.vbs"""
t.WriteLine “”"%temp%\OEgetPrivileges.vbs"""
t.WriteLine “exit /B”

t.WriteLine “:gotPrivileges”
t.WriteLine “::::::::::::::::::::::::::::”
t.WriteLine “::START”
t.WriteLine “::::::::::::::::::::::::::::”

t.WriteLine “setlocal & pushd .”

t.WriteLine “REM Run shell as admin (example) - put here code as you like”
t.WriteLine "sc " + SvcAction + " " + SvcName

app.SleepCurrentThread 100
dim s As new Shell
s.TimeOut = -1
s.Mode = 2
End Sub

It could be that the shell is going out of scope before it gets a chance to run. Try using mode 0 & see what happens.

Why, instead of using a shell, don’t you point a folderitem to the newly created file, and use FolderItem.Launch on it ? That would be exactly the equivalent of what you do by double clicking on it. It should work just the same.

Thanks Michel. That works perfectly. I’ve been using Xojo and RB since 2005 and never noticed that function before. It looks very useful.

The only issue with this is that there is no way to get the return code of the bat file like you can with a shell. You can fix that by writing to a file and then reading that file after your call to f.launch.