Shell and wait

Garrett Sever
10-03-2001, 05:35 PM
I'm not the most Network savvy guy around for sure (You need to speak to <font color=blue>PrOpHeT</font color=blue> and <font color=blue>ChiefRedBull</font color=blue> for most of that stuff), however I've noticed a few requests for Shell and wait type projects, and I decided to put this example together.

It shows how to send a command to DOS and wait for it to be completed. It stores the output for the command in a temporary file and displays it in the Windows textbox.

The commands I demo in this example are NETSTAT, TRACERT, PING, and NET (you supply the argument... PRINT, or VIEW, or whatever).

Some relevant posts to this code are here (http://www.visualbasicforum.com/bbs/showflat.php?Cat=&Board=visbas&Number=50534&page=&view=&sb=&o=&vc=1) and here (http://www.visualbasicforum.com/bbs/showflat.php?Cat=&Board=visbas&Number=31332&page=&view=&sb=&o=&vc=1).

Hope you find this useful.

Regards,
-<font color=purple>The Hand</font color=purple>

<font color=green>All your code are belong to us...</font color=green> images/icons/tongue.gif

Garrett Sever
10-23-2001, 12:08 PM
Same project, with some "Cancel" functionality implemented.

Garrett Sever
02-10-2002, 07:55 AM
Was fishing around and found that the attachment for this was gone. A couple of people have sent me nastygrams asking where the code is. Lay off... here it is.... Without the cancel functionality. That was on my laptop at work, and the harddrive of said laptop got nuked last December.

Thinker
03-24-2002, 10:36 AM
Here is some code submitted by djf1010 that helps do a shell and
wait type process.

'Include API OpenProcess declare
'Include API CloseHandle declare
Public Const PROCESS_QUERY_INFORMATION = (&H400)

Dim lngTaskID As Long
Dim lngPID As Long
lngTaskID = Shell("command.com /c pause") 'Replace 'pause' with your DOS batch file or command(s)
Do
lngPID = OpenProcess(PROCESS_QUERY_INFORMATION, False, lngTaskID) 'Get the process ID - I think
DoEvents
CloseHandle lngPID
Loop Until lngPID = 0 ' When the app is done this becomes 0

Thanks to djf1010 for this.

Teric
04-11-2002, 01:40 PM
That's great! But what about an interactive FTP session? What if I wanted to issue DOS FTP line commands? Is it possible to keep a session open, send input, and get output from it?

Garrett Sever
04-11-2002, 01:48 PM
You definately wouldn't want to use the DOS shell to do that.

You'd be much better off going with a sockets/winsock solution for interactive sessions. I'm sure they could help you out in the Communications board.

Mikecrosoft
02-12-2003, 10:23 AM
Hi to all, before of a lot of questions, I make this function works:

This function works like any OpenAndWait, but this uses ShellExecuteEx. It means I can open any file with his associated program and wait until be closed.


Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SEE_MASK_INVOKEIDLIST = &HC
Public Const SEE_MASK_FLAG_NO_UI = &H400

Public Const INFINITE = &HFFFF 'Infinite Wait Time
Public Const SW_NORMAL = 1


Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
' Optional Fields
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type


Public Declare Function ShellExecuteEx Lib "shell32.dll" (SEI As SHELLEXECUTEINFO) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long


Public Sub ExecAndWait(ByVal hwnd As Long, ByVal ProgramPath As String)

Dim SEI As SHELLEXECUTEINFO

'Filling SEI structure
With SEI
.cbSize = Len(SEI) 'Bytes of the structure
.fMask = SEE_MASK_NOCLOSEPROCESS 'I need hProcess to be retrieved
.lpFile = ProgramPath 'Program Path
.lpVerb = "Open" 'Action to do
.nShow = SW_NORMAL 'How the program will be showed
.lpDirectory = Left(ProgramPath, (InStrRev(ProgramPath, "\")) - 1)
.hwnd = hwnd 'Window Handle

End With

ShellExecuteEx SEI 'Execute the program hProcess recives the Process Handle used next.

WaitForSingleObject SEI.hProcess, INFINITE 'Here wait until close the program

CloseHandle hProcess

End Sub

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum