Bulletproof replacement for RunProgram()

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Bulletproof replacement for RunProgram()

Post 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?
( 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... )
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: Bulletproof replacement for RunProgram()

Post 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?
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: Bulletproof replacement for RunProgram()

Post 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.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
ebs
Enthusiast
Enthusiast
Posts: 530
Joined: Fri Apr 25, 2003 11:08 pm

Re: Bulletproof replacement for RunProgram()

Post 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)
Last edited by ebs on Thu Sep 30, 2021 5:04 pm, edited 1 time in total.
Fred
Administrator
Administrator
Posts: 16664
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Bulletproof replacement for RunProgram()

Post by Fred »

I just tried as well:

Code: Select all

Debug RunProgram("notepad444.exe")


and it returns 0. May be an antivirus stuff ?
User avatar
ChrisR
Addict
Addict
Posts: 1150
Joined: Sun Jan 08, 2017 10:27 pm
Location: France

Re: Bulletproof replacement for RunProgram()

Post by ChrisR »

:lol:
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Bulletproof replacement for RunProgram()

Post 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...
( 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... )
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8433
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Bulletproof replacement for RunProgram()

Post by netmaestro »

Seems reasonable to me:

Code: Select all

If FileSize("program") > 0
  RunProgram("program")
Endif
BERESHEIT
Post Reply