×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Retrieving Exit Codes (Error levels) from a command line utility
3

Retrieving Exit Codes (Error levels) from a command line utility

Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Hi Team,

I have a command line utility to send emails. After its execution it returns Exit Codes or Error Levels from which we would know if it went well or the kind of error it generated.

I am executing the utility from within VFP using ShellExecute. Everything is fine. However, I am not able to find a way to retrieve the Exit Code returned by the utility. I have seen a few write-ups but they're all regarding setting up a batch file. But, I think, to involve a batch file is not my appropriate solution as I am calling this utility in the middle of a functional process running inside a form.

Thanks in advance for the suggestions/advises.

Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Hi Olaf,

Thank you very much. Exactly what I was looking for I hope.
Will check it and let you know.

I was wondering why you're saying 'Once more today' but then saw the post below mine about 'Checking and wait for checking to finish...'.
Sorry, I had not seen that, or may be missed it because the title appeared about something else.

Thank you,
Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

No need to mention, it's clear you only search for an answer and when you don't find it ask your question and look into your own thread.
It's just funny how such coincidences happen quite often.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Dear Olaf,
Yes, you're right! At least in my case winky smile

By the way, I found a code using 'WScript.Shell' which also has a provision to wait for the called routine to finish. That was much more simpler than 'ShellExecute' to implement. Below is the function I am using.

CODE -->

LOCAL oShell, lErrCode
oShell = CREATEOBJECT("WScript.Shell")
*/ pCmd = Command to execute
*/ 0 = Hide run window
*/ 1 = Wait till execution exists.  This is required to get the ERRORLEVEL returned on lErrCode variable
lErrCode = oShell.Run(pCmd, 0, .T.)
oShell = null 
RELEASE oShell
RETURN lErrCode 

It's working perfectly in my Windows 10 64 bit laptop. Need to check in older versions and 64/32 bit combinations.
By the way, are there any known dis-advantages of 'WScript.Shell' as such compared to 'ShellExecute'?

Thank you,
Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Hi Olaf and all others,

The code I posted above works like a charm in my Win 7 32bit system too!

Thanks,
Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

Yes, this solution was discussed in thread184-1790999: Suppress DOS box flashing

But Koen Pillar had some problem with it. The more complicated CreateProcess/WaitForSingleObject is not depending on any OLE class and will also work on systems restricting shell scripting. You can hide the complexity by defining your own class or functions library with a RUN method/function hiding all that and are better off.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Olaf,

I see that there is obviously a point in what you're saying. I will try to create a class (as given in the link you posted in the beginning) and to use ShellExecute instead of WScript.Shell (a swap again !)

Thanks
Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

Quote:

are there any known dis-advantages of 'WScript.Shell' as such compared to 'ShellExecute'?

The wscript method depends on the Windows Scripting Host (WSH) being installed and activated. I've often read that system administrators sometimes disable WSH for security reasons. That said, I've never come across a case of that actually happening, and I'm not sure why it should be an issue. But it is the reason that ShellExecute() might be the preferred method.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Retrieving Exit Codes (Error levels) from a command line utility

Well, it's still a dependency you can avoid using CreateProcess/WaitForSingleObject.

Notice, Rajesh, the best equivalent to Wscript.Shell.Run() is not ShellÉxecute, ShellExecute doesn't tell you a process id of the process it started so you can't wait for it to exit and don't get the exit code, the only thing ShellExecute also offers is setting window state. The wiki link is about usage of CreateProcess/WaitForSingleObject.

There's more to it, when you use CreateProcess you may also use the stdin/stdout/err pipes and other interprocess communication methods (as far as the other process supports them).

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Mike,

Very clear! Thanks.

Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

(OP)
Olaf,
Thank you for that explanation on 'CreateProcess / WaitForSingleObject'.
I got it.

Rajesh

RE: Retrieving Exit Codes (Error levels) from a command line utility

Quote:

the only thing ShellExecute also offers is setting window state

For what it's worth, wscript.shell.run also offers the window state - in its second parameter. As far as I can see, these values are the same as with ShellExecute().

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Retrieving Exit Codes (Error levels) from a command line utility

Yes, I said so, but ShellExecute does not offer to wait for the process to finish, CREATEPROCESS/WAITFORSINGLEOBJECT does,
Therefore, Mike, ShellExeecute is NOT the analog function to replace oShell.Run if you want MORE than control the window size/visibiliy.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

RE: Retrieving Exit Codes (Error levels) from a command line utility

Quote:

ShellExeecute is NOT the analog function to replace oShell.Run if you want MORE than control the window size/visibiliy.

I never said otherwise, Olaf. I was simply picking up your point about the window state.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads

RE: Retrieving Exit Codes (Error levels) from a command line utility

Well, and I said "the only thing ShellExecute also offers is setting window state" in the sense that it doesn't offer the other important feature asked by the thread title: To wait for the exit code.

But you recommended using ShellExecute when not using Wcript.Shell.Run(), but ShellExecute lacks that feature and so it's the wrong recommendation.

I hope that makes clear now, why I didn't second your recommendation of ShellExecute as alternative, you forgot what is asked for, ShellExecute does not suffice for that.

Bye, Olaf.

Olaf Doschke Software Engineering
https://www.doschke.name

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close