CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2001
    Posts
    91

    Getting the Returnvalue of an application started from VB

    Hi out there,

    I created a VB-program that calls another application via shellexecute.
    That app is a backup program, that gets commandline parameters from my VB-program.

    Now sometimes the backup program crashes, and the backup files are not correctly finished or incomplete.

    The backup program delivers a returnvalue that is 0 when it completes the backup successfully, if the value is <> 0 the backup failed.

    The returnvalue of Shellexecute AFAIK delivers only Information about the execution of the command, so it does not help.
    But then how can I get the returnvalue of the application in VB?
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  2. #2
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Getting the Returnvalue of an application started from VB

    You can use SendMessage API from your backup program to notify the main app.

  3. #3
    Join Date
    May 2001
    Posts
    91

    Re: Getting the Returnvalue of an application started from VB

    Hi Shaikh.Riyaz.a,

    seems I did not make it clear:

    The Backup Program is a 3rd Party tool. We bought it.

    My program calls that backup program with user-specific commandline-parameters

    AFAIK sendmessage could be used if I could change the code of the backup Program, or am I missing something?
    have a nice day

    Patzer
    _____________________________
    Philo will never be forgotten

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Getting the Returnvalue of an application started from VB

    Here's how to trap the return code...

    Code:
    Option Explicit
    
    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _
    ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Private Const SW_SHOWNORMAL As Long = 1
    Private Const SW_HIDE As Long = 0
    
    Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    Dim sSave As String, Ret As Long
    
    Private Sub Form_Load()
        'KPD-Team 1998
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        Dim sSave As String, Ret As Long
        'Create a buffer
        sSave = Space(255)
        'Get the system directory
        Ret = GetSystemDirectory(sSave, 255)
        'Remove all unnecessary chr$(0)'s
        sSave = Left$(sSave, Ret)
    End Sub
    
    Private Sub Command1_Click()
       Dim d As Long
       d = ShellExecute(Me.hwnd, "Open", sSave & "CMD.exe", " /c dir c: > c:\Myfile.txt", "c:\", SW_SHOWNORMAL)
       MsgBox d
    End Sub
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Dec 2006
    Location
    Pune, India.
    Posts
    579

    Re: Getting the Returnvalue of an application started from VB

    Quote Originally Posted by Patzer
    The Backup Program is a 3rd Party tool. We bought it.

    My program calls that backup program with user-specific commandline-parameters
    Then right now in what format does it return the success or failure of its own? I mean how come you know that it ran successfully?

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Getting the Returnvalue of an application started from VB

    It doesn't crash?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Aug 2000
    Location
    South East England
    Posts
    86

    Re: Getting the Returnvalue of an application started from VB

    If you don't want to use On Error Goto YourErrorCode
    to check for crash.
    d will return the following value.
    Code:
     Private Sub Command1_Click()
       Dim d As Long
       d = ShellExecute(Me.hwnd, "Open", sSave & "CMD.exe", " /c dir c: > c:\Myfile.txt", "c:\", SW_SHOWNORMAL)
       MsgBox d
       ' The return values (d=...) are;
       ' <=32 ' = Some type of error.
       ' > 32 ' = OK. No error
       '  = 0 ' = Operating system is out of memory.
       '  = 2 ' = File (CMD.exe) was not found.
       '  =42 ' = Found and ran without any errors.
       ' See URL; http://allapi.mentalis.org/apilist/ShellExecute.shtml
       ' for more information.
    End Sub
    Don't spend too much time on your computer.
    Sit on a chair instead!!
    It's a lot more comfortable and better for your hardware.

    :-/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured