Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling SHELL synchronously!!! 4

Status
Not open for further replies.

johnvai

IS-IT--Management
Oct 4, 2003
127
LB
Dear Friends:

I read about the SHELL function in VB6.0 and knew that the SHELL function works asynchronously(This means that I want to call two seperate functions outside my Programs, like calling Notepad and Calculator).
But my problem is that I want to call the Calculator program after finishing the notepad.
If the program is terminated or closed, run the second program, which i tried hardly on it but it seems no other choice.

I have seen some people doing it in applications, but i don't know how?

Any hints?
 
In thread222-726872 PaulBent provides a sub that uses API calls to fire an app and wait for it to complete. Sounds like what you're looking for.
 
I found this code on this site. Credit goes to whoever wrote this code:

Private Sub Command1_Click()
' Add a reference to Windows Script Host Object Model
Dim RetVal As Long
Dim WSH As IWshShell
Dim WaitForTermination As Boolean
Dim CommandLine As String

Set WSH = New IWshShell_Class
CommandLine = "notepad.exe"
WaitForTermination = True
RetVal = WSH.Run(CommandLine, vbNormalFocus, WaitForTermination)
MsgBox "Notepad Closed!", vbInformation, "Program Termination"
Set WSH = Nothing
End Sub

Swi
 
Thanks man for the solution. i think it's gonna work.
I will fee you back as soon as it works.

Thanks,
 
This code will pause your app's thread until the shelled app closes.
[/code]
Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Const NORMAL_PRIORITY_CLASS = &H20&
Const INFINITE = -1&

Declare Function WaitForSingleObject Lib "kernel32" _
(Byval hHandle As Long, _
Byval dwMilliseconds As Long) As Long

Declare Function CreateProcessA Lib "kernel32" _
(Byval lpApplicationName As Long, _
Byval lpCommandLine As String, _
Byval lpProcessAttributes As Long, _
Byval lpThreadAttributes As Long, _
Byval bInheritHandles As Long, _
Byval dwCreationFlags As Long, _
Byval lpEnvironment As Long, _
Byval lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, _
lpProcessInformation As PROCESS_INFORMATION) As Long

Declare Function GetExitCodeProcess Lib "kernel32" _
(Byval hProcess As Long, lpExitCode As Long) As Long

Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Long) As Long

'________________________________________

Function fShellWait(strCmdLine As String) As Long

'--- Shells the passed command line and waits for the process to finish
'--- Returns the exit code of the shelled process

Dim udtProc As PROCESS_INFORMATION
Dim udtStart As STARTUPINFO
Dim lngRtn As Long

'Initialize the STARTUPINFO structure
udtStart.cb = Len(udtStart)

'Launch the shelled application and yield to it
lngRtn = CreateProcessA(Clng(0), strCmdLine, CLng(0), _
CLng(0), CLng(1), NORMAL_PRIORITY_CLASS, CLng(0), _
CLng(0), udtStart, udtProc)
DoEvents

'Wait for the shelled application to finish
lngRtn = WaitForSingleObject(udtProc.hProcess, INFINITE)
GetExitCodeProcess udtProc.hProcess, lngRtn
CloseHandle udtProc.hThread
CloseHandle udtProc.hProcess
fShellWait = lngRtn

End Function
[/code]

Paul Bent
Northwind IT Systems
 
I'm using Swi's code posted above and the Dim as IWshShell will not compile. I suspect it is related to the "Add reference to Windows Script Host Object Model" which I do not know how to do.

Can some one point me to info on how to do that?

TIA

Ralph D. Meredith
 
go under the Project menu of your vb appication (the vb ide) go down to the "References" menu option.

This will bring up a list of all the COM components installed on your system. If you don't have it in there you'll have to install the scripting host.


 
I am having the same problem. Which reference am I supposed to use for this code. I am trying to use the first example.
I also have another question since the shell command returns the task ID is there a way for me to test this taskId if it is still running? I have tested it and it does send back the ID.
Thanks

To go where no programmer has gone before.
 
There is also a FAQ written by dsi that works very well:

faq222-428

Swi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top