If a program is somewhere included in the path, RunProgram() will execute it, even without a specified path:
RunProgram("notepad.exe")
If a program is NOT included in the path, RunProgram will halt my code / error out.
RunProgram("blabla.exe")
If a program is exactly specified including path, it will run:
RunProgram("c:\test\blabla.exe")
I have a mix of external programs (I'm writing a little launcher) and some of the called exe's may NOT be in the path, but if there is a mistake the program crashes, because I need to either test upfront if the file will exist, or gracefully handle the error RunProgram() throws at me.
Who has a solution?
Bulletproof replacement for RunProgram()
Bulletproof replacement for RunProgram()
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
Re: Bulletproof replacement for RunProgram()
I don't understand why attempting to run a program not in the path would cause an error.
When I do that, 'RunProgram()' returns a value of zero, which indicates that the program didn't run; no error or crash.
Is there some other reason that your program crashes?
When I do that, 'RunProgram()' returns a value of zero, which indicates that the program didn't run; no error or crash.
Is there some other reason that your program crashes?
Re: Bulletproof replacement for RunProgram()
If ebs has not nailed the issue....
Linux has a system program called 'which'. This searches the PATH for a file and reports a full path where it can be found or nothing if it can't.
I'm sure there must be a Windows equivalent? This would enable a graceful avoiding the crash.
Linux has a system program called 'which'. This searches the PATH for a file and reports a full path where it can be found or nothing if it can't.
I'm sure there must be a Windows equivalent? This would enable a graceful avoiding the crash.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Re: Bulletproof replacement for RunProgram()
In Windows (at least 32-bit, I'm not sure about 64-bit), you can do this:I'm sure there must be a Windows equivalent?
Code: Select all
*buffer = AllocateMemory(1024)
SearchPath_(#Null, "notepad.exe", "", 1024, *buffer, @pointer.l)
Debug PeekS(*buffer)
Last edited by ebs on Thu Sep 30, 2021 5:04 pm, edited 1 time in total.
Re: Bulletproof replacement for RunProgram()
I just tried as well:
and it returns 0. May be an antivirus stuff ?
Code: Select all
Debug RunProgram("notepad444.exe")
and it returns 0. May be an antivirus stuff ?
Re: Bulletproof replacement for RunProgram()
Ah! Try without a parameter...
RunProgram("")
Works as expected in the compiler, but throws an error in the IDE (5.73LTS).
It's funny how some of the things you run into are... weird.Still have to figure out .LNK files, but .URL's are also supported directly by Windows as a kind of shortcuts, and then there are shortcuts that look like shortcuts but are not... very confusing...
RunProgram("")
Works as expected in the compiler, but throws an error in the IDE (5.73LTS).
It's funny how some of the things you run into are... weird.Still have to figure out .LNK files, but .URL's are also supported directly by Windows as a kind of shortcuts, and then there are shortcuts that look like shortcuts but are not... very confusing...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
( The path to enlightenment and the PureBasic Survival Guide right here... )
- netmaestro
- PureBasic Bullfrog
- Posts: 8433
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Bulletproof replacement for RunProgram()
Seems reasonable to me:
Code: Select all
If FileSize("program") > 0
RunProgram("program")
Endif
BERESHEIT