Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Return value of Shell?

archibaldarchibald Posts: 9Member

Is there anyone that knows how to use return value of Shell function?

I would like to find a way to find out if the application I start in a Shell function call has finished, and if the application was successful.



Comments

  • iwilld0itiwilld0it Posts: 1,134Member
    :
    : Is there anyone that knows how to use return value of Shell function?
    :
    : I would like to find a way to find out if the application I start in a Shell function call has finished, and if the application was successful.
    :
    :
    :
    :

    [code]
    On Error Goto Handler
    If Shell("c:SomeFile.exe", vbNormalFocus) <> 0 Then
    ' Program executed sucessfully
    End If
    [/code]

    The return value is a unique task id on sucess, otherwise zero. It is wise to add an error trap, because the Shell function can throw an exception if the program cannot be found or open for some reason.
  • infidelinfidel Posts: 2,900Member
    :
    : Is there anyone that knows how to use return value of Shell function?
    :
    : I would like to find a way to find out if the application I start in a Shell function call has finished, and if the application was successful.

    Welcome to the world of Microsoft's Functions That Look Useful But Are Really A Pain In The Arse. Shell() returns a task ID (what the rest of the world calls a Process ID). You can use various API calls to find stuff out about processes, but it's a lot more hassle than it should be. I like using the Windows Script Host Object Model instead:

    [code]
    Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

    Private Sub Form_Load()

    Dim wsh As New WshShell
    Dim cmd As WshExec

    Set cmd = wsh.Exec("c:vimvim61gvim.exe")

    Do While cmd.Status = WshRunning
    Sleep 10
    Loop

    Debug.Print "Program exited with code: " & cmd.ExitCode
    Debug.Print "STDOUT: " & cmd.StdOut.ReadAll
    Debug.Print "STDERR: " & cmd.StdErr.ReadAll

    End Sub
    [/code]

    As you can see, the WshExec object gives you easy access to a process' status, exit code, standard streams, and it even gives you a Terminate() method to kill your child process. Doing all of this via API calls just isn't worth it when you've got such a handy object to use.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • infidelinfidel Posts: 2,900Member
    : [code]
    : On Error Goto Handler
    : If Shell("c:SomeFile.exe", vbNormalFocus) <> 0 Then
    : ' Program executed sucessfully
    : End If
    : [/code]
    :
    : The return value is a unique task id on sucess, otherwise zero. It is wise to add an error trap, because the Shell function can throw an exception if the program cannot be found or open for some reason.

    All that says is the process was started successfully, it doesn't tell you if the program encountered any errors while it was running. And it doesn't tell you when it finished.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • purplehazepurplehaze Posts: 57Member
    : :
    : : Is there anyone that knows how to use return value of Shell function?
    : :
    : : I would like to find a way to find out if the application I start in a Shell function call has finished, and if the application was successful.
    :
    : Welcome to the world of Microsoft's Functions That Look Useful But Are Really A Pain In The Arse. Shell() returns a task ID (what the rest of the world calls a Process ID). You can use various API calls to find stuff out about processes, but it's a lot more hassle than it should be. I like using the Windows Script Host Object Model instead:
    :
    : [code]
    : Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    :
    : Private Sub Form_Load()
    :
    : Dim wsh As New WshShell
    : Dim cmd As WshExec
    :
    : Set cmd = wsh.Exec("c:vimvim61gvim.exe")
    :
    : Do While cmd.Status = WshRunning
    : Sleep 10
    : Loop
    :
    : Debug.Print "Program exited with code: " & cmd.ExitCode
    : Debug.Print "STDOUT: " & cmd.StdOut.ReadAll
    : Debug.Print "STDERR: " & cmd.StdErr.ReadAll
    :
    : End Sub
    : [/code]
    :
    : As you can see, the WshExec object gives you easy access to a process' status, exit code, standard streams, and it even gives you a Terminate() method to kill your child process. Doing all of this via API calls just isn't worth it when you've got such a handy object to use.
    :
    :
    : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    :
    :


    hi tried this but iam iam unable to declare wshShell,wshExec

    Please Help

    Luv
    PurpleHaze

  • infidelinfidel Posts: 2,900Member
    : : :
    : : : Is there anyone that knows how to use return value of Shell function?
    : : :
    : : : I would like to find a way to find out if the application I start in a Shell function call has finished, and if the application was successful.
    : :
    : : Welcome to the world of Microsoft's Functions That Look Useful But Are Really A Pain In The Arse. Shell() returns a task ID (what the rest of the world calls a Process ID). You can use various API calls to find stuff out about processes, but it's a lot more hassle than it should be. I like using the Windows Script Host Object Model instead:
    : :
    : : [code]
    : : Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    : :
    : : Private Sub Form_Load()
    : :
    : : Dim wsh As New WshShell
    : : Dim cmd As WshExec
    : :
    : : Set cmd = wsh.Exec("c:vimvim61gvim.exe")
    : :
    : : Do While cmd.Status = WshRunning
    : : Sleep 10
    : : Loop
    : :
    : : Debug.Print "Program exited with code: " & cmd.ExitCode
    : : Debug.Print "STDOUT: " & cmd.StdOut.ReadAll
    : : Debug.Print "STDERR: " & cmd.StdErr.ReadAll
    : :
    : : End Sub
    : : [/code]
    : :
    : : As you can see, the WshExec object gives you easy access to a process' status, exit code, standard streams, and it even gives you a Terminate() method to kill your child process. Doing all of this via API calls just isn't worth it when you've got such a handy object to use.
    : :
    : :
    : : [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]
    : :
    : :
    :
    :
    : hi tried this but iam iam unable to declare wshShell,wshExec

    Did you add a Reference to the Windows Script Host Object Model?


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

  • kel1981bkel1981b Posts: 852Member
    : Did you add a Reference to the Windows Script Host Object Model?
    :
    I was trying to find it in a Reference list on my machine and was not able to do that. I am using Win2000, VisualStudio 6.0 Enterprise edition SP 5. Is it correct name Windows Script Host Object Model or it's listed under some other name?
  • paulj59paulj59 Posts: 420Member
    : : Did you add a Reference to the Windows Script Host Object Model?
    : :
    : I was trying to find it in a Reference list on my machine and was not able to do that. I am using Win2000, VisualStudio 6.0 Enterprise edition SP 5. Is it correct name Windows Script Host Object Model or it's listed under some other name?
    :

    I have the exact setup as you and found it by that name.
    It's c:winntsystems32wshocm.ocx
  • AmalfimarAmalfimar Posts: 58Member
    Wow, that code is what I was searching for pausing the program when it calls a shellexecute, why didn't you told me ??? :-)




    [hr]
    "If she's leaving you, remember that the way is for the one that leaves, and for the one that comes ;-)"

  • kamiccolo_litekamiccolo_lite Posts: 152Member
    is there anyway to send the program u just started a message, using stdout?
  • infidelinfidel Posts: 2,900Member
    : is there anyway to send the program u just started a message, using stdout?

    I suppose there is probably a way using APIs, but I imagine it would be a nightmare. I would do it like this:

    [code]
    Option Explicit

    Private Declare Sub Sleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)

    Public Sub RunProgram()
    'add a reference to Windows Script Host Object Model
    Dim wsh as New WshShell
    Dim exec as WshExec

    Set exec = wsh.Exec()

    Call exec.StdIn.WriteLine("text to send to your program")

    Do While exec.Status = WshRunning
    Sleep 10
    Loop
    End Sub
    [/code]

    The only drawback to this method is that you cannot hide any windows created by the program you call, unlike Shell which you can pass the vbHide parameter.


    [size=5][italic][blue][RED]i[/RED]nfidel[/blue][/italic][/size]

Sign In or Register to comment.