Page 1 of 1
Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 12:12 pm
by blueznl
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?
Re: Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 3:12 pm
by ebs
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?
Re: Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 3:21 pm
by eck49
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.
Re: Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 4:57 pm
by ebs
I'm sure there must be a Windows equivalent?
In Windows (at least 32-bit, I'm not sure about 64-bit), you can do this:
Code: Select all
*buffer = AllocateMemory(1024)
SearchPath_(#Null, "notepad.exe", "", 1024, *buffer, @pointer.l)
Debug PeekS(*buffer)
Re: Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 4:58 pm
by Fred
I just tried as well:
Code: Select all
Debug RunProgram("notepad444.exe")
and it returns 0. May be an antivirus stuff ?
Re: Bulletproof replacement for RunProgram()
Posted: Thu Sep 30, 2021 5:31 pm
by ChrisR
Re: Bulletproof replacement for RunProgram()
Posted: Fri Oct 01, 2021 10:30 pm
by blueznl
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...
Re: Bulletproof replacement for RunProgram()
Posted: Mon Oct 11, 2021 8:38 pm
by netmaestro
Seems reasonable to me:
Code: Select all
If FileSize("program") > 0
RunProgram("program")
Endif