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
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

569 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