Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use lua os.execute in windows to launch a program with out a flash of CMD

I am happily launching a program in a windows system from Lua using

strProgram = '"C:\\Program Files\\Ps Pad\\PSPad.exe"'
strCmd = 'start "" '..strProgram
os.execute(strCmd)

This works correctly, launching the program and the script finishing. How ever it flashes up a command window for a fraction of a second, does any one have a way from Lua to launch a program.

like image 949
Jane T Avatar asked Jun 15 '11 18:06

Jane T


2 Answers

Lua's os.execute command is based on the C standard library "shell" function. In Windows, this function will always create a command window, and it will always halt your current process until the window finishes. The latter also happens in Linux.

There is ultimately no way around this. Not through the Lua standard API. Because Lua needs to be light-weight and platform independent, the API is not allowed to use OS-dependent native APIs.

Your best bet would be to use the Lua Ex-Api module. It is effectively abandonware, and you may need to patch up a few compiler issues (I'm guessing the Windows port wasn't their first priority). But it is a reasonably good way to spawn processes. You can choose to wait until it finishes yourself, or let them run in parallel. And it won't throw up a command prompt window, unless the application itself uses one.

like image 113
Nicol Bolas Avatar answered Oct 02 '22 13:10

Nicol Bolas


This is the piece of code I use to call a batch from Lua, maybe help. In win console (command prompt) open and execute, same in unix (mac|nix)

-- sBatchFile = .bat for windows, .sh for x
function vfFork2(sBatchFile)
    local b = package.cpath:match("%p[\\|/]?%p(%a+)")
    if b == "dll" then 
        -- windows
        os.execute('start cmd /k call "'..sBatchFile..'"')
    elseif b == "dylib" then
        -- macos
        os.execute('chmod +x "'..sBatchFile..'"')
        os.execute('open -a Terminal.app "'..sBatchFile..'"')
    elseif b == "so" then
        -- Linux
        os.execute('chmod +x "'..sBatchFile..'"')
        os.execute('xterm -hold -e "'..sBatchFile..'" & ')
    end 
end 
like image 35
Franco Properzi Avatar answered Oct 02 '22 13:10

Franco Properzi