Using Return Codes in VB

Posted on 2003-02-27
Medium Priority
Last Modified: 2008-01-09
I am attempting to execute the following program within a VB macro I am writing:  "psservice.exe query mcshield \\servername".  The servername is derived from information that has been entered into an excel spreadsheet.  

I need to know, for one, the best way to execute the exe along with the options within the macro.

The other thing I need to know is how to check the return code for the exe.  I know that if the service is found that the return code = 0.

Thanks for any help.

Question by:Cking
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2

Accepted Solution

webJose earned 225 total points
ID: 8044969
I would use the ShellExecuteEx API function like this:

Private Const SEE_MASK_FLAG_DDEWAIT As Long = &H100
Private Const SEE_MASK_NOCLOSEPROCESS As Long = &H40

Private Const SW_HIDE As Long = 0

    cbSize As Long
    fMask As Long
    hWnd As Long
    lpstrVerb As String
    lpstrFile As String
    lpstrParameters As String
    lpstrDirectory As String
    nShow As Long
    hInstApp As Long
    ' fields
    lpIDList As Long
    lpstrClass As Long
    hkeyClass As Long
    dwHotKey As Long
    hIconOrMon As Long
    hProcess As Long
End Type

Private Declare Function ShellExecuteEx Lib "shell32.dll" (ByRef lpExecInfo As SHELLEXECUTEINFO) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const INFINITE As Long = &HFFFFFFFF

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Public Function StartProcess(byval hWndParent as long, byval strFilename as string, byval strParams as string) As long


    With sei
        .cbSize = Len(sei)
        .hWnd = hwndparent
        .lpstrDirectory = vbnullstring
        .lpstrFile = strfilename
        .lpstrParameters = strparams
        .lpstrVerb = "open"
        .nShow = m_ShowMode
    End With
    If (ShellExecuteEx(sei) = 0) Then
        StartProcess = -1 'error
        Exit Function
    End If
    If (sei.hProcess <> 0) And (sei.hInstApp > 32) Then
        waitforsingleobject sei.hprocess, infinite
        getexitcodeprocess sei.hprocess, startprocess
        closehandle sei.hprocess
        startprocess = -1 'error
    End If
end function

The above function will shell the specified process with the specified parameters and then will wait for the shelled application to finish.  Once it is finished, it will get the exit code process.

Note however, that the use of WaitForSingleObject will effectively freeze your application until the shelled process ends.  If this is an inconvenience, you may want to try multithreading, or change to a good ol' DoEvents loop.

Expert Comment

ID: 8901006
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?

Expert Comment

ID: 8903346
I think my answer is correct and I think I should be awarded the points.

Author Comment

ID: 8916777
Thanks for the help with the question.  It did work.

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month11 days, 16 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question