Listing Performing an External Call

Sub TestExternalAccess()

' Obtains the result of the call. Dim Result As Long

' Perform the call.

Result = AccessExternalProgram("CMD.EXE /k " + _ Chr(34) + _

"Dir >> Output.TXT | Notepad.EXE Output.TXT" + Chr(34))

' Show the call results. Select Case Result

Case -1

MsgBox "Couldn't Start Application"

Case WAIT_OBJECT_0

MsgBox "Everything Worked!"

Case WAIT_ABANDONED

MsgBox "Application Failed to Wait"

Case WAIT_TIMEOUT

MsgBox "Application Timed Out"

End

Select

End Sub

The code begins by creating a result variable. It then makes the call. In this case, the external program is a command prompt. Using the /k argument is quite powerful because you can tell the command processor to execute any command you want. The example calls the Dir command, which displays a directory of the current location on the hard drive. The Dir command output is redirected to Output.TXT using the redirection symbol. After the Dir call is complete, the command process pipes the information to Notepad.EXE, which displays the content of the Output.TXT file onscreen.

At this point, you see a command prompt, and a copy of Notepad opens on your display. Close Notepad first and then the command prompt. Only after you close both windows does the AccessExternalProgram() function return a result to the test program. The test program displays the result onscreen based on the Result value.

Was this article helpful?

0 0

Post a comment