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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Running external EXE programs. 1

Status
Not open for further replies.

dingle75

Programmer
Nov 10, 1999
1
GB
Can anybody tell what the commands are for calling an external .EXE file and running it.<br>
<br>
Thanks dingle75<br>

 
<br>
use the shell function available in VB. Look for Shell in <br>
the help<br>
<br>
Eg : shell notepad.exe
 
Or use the Win32 api function called ShellExecute. It's more complicated, but it also allows you to do things like open web pages, Word documents, etc. just by giving the name of the URL/file.<br>
<br>
Chip H.<br>

 
With Tags of commandbuttons :<br>
<br>
Option Explicit<br>
<br>
#If Win16 Then<br>
Declare Function GetWindowsDirectory Lib &quot;kernel&quot; (ByVal lpBuffer$, ByVal Size%) As Integer<br>
#ElseIf Win32 Then<br>
Declare Function GetWindowsDirectory Lib &quot;kernel32&quot; Alias &quot;GetWindowsDirectoryA&quot; (ByVal lpBuffer As String, ByVal nSize As Long) As Long<br>
#End If<br>
<br>
Private Sub btnCommand_Click(Index As Integer)<br>
On Error Resume Next<br>
If Index &lt; 4 Then<br>
'Tag button 1 = Explorer<br>
a% = Shell(WinDir() + &quot;\&quot; + btnCommand(Index).Tag + &quot;.exe&quot;, 1)<br>
Else<br>
End<br>
End If<br>
End Sub<br>
<br>
<p>Eric De Decker<br><a href=mailto:vbg.be@vbgroup.nl>vbg.be@vbgroup.nl</a><br><a href= Visual Basic Center</a><br>
 
Along the same lines, if you're looking to shell to a program and wait for the shelled program to finish before returning to VB, here's a class that will do that for you.<br>
<br>
Option Explicit<br>
<br>
Private Type STARTUPINFO<br>
cb As Long<br>
lpReserved As String<br>
lpDesktop As String<br>
lpTitle As String<br>
dwX As Long<br>
dwY As Long<br>
dwXSize As Long<br>
dwYSize As Long<br>
dwXCountChars As Long<br>
dwYCountChars As Long<br>
dwFillAttribute As Long<br>
dwFlags As Long<br>
wShowWindow As Integer<br>
cbReserved2 As Integer<br>
lpReserved2 As Long<br>
hStdInput As Long<br>
hStdOutput As Long<br>
hStdError As Long<br>
End Type<br>
<br>
Private Type PROCESS_INFORMATION<br>
hProcess As Long<br>
hThread As Long<br>
dwProcessID As Long<br>
dwThreadID As Long<br>
End Type<br>
<br>
Private Declare Function WaitForSingleObject Lib _<br>
&quot;kernel32&quot; (ByVal hHandle As Long, ByVal dwMilliseconds _<br>
As Long) As Long<br>
<br>
Private Declare Function CreateProcessA Lib &quot;kernel32&quot; _<br>
(ByVal lpApplicationName As Long, ByVal lpCommandLine As _<br>
String, ByVal lpProcessAttributes As Long, ByVal _<br>
lpThreadAttributes As Long, ByVal bInheritHandles As Long, _<br>
ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, _<br>
ByVal lpCurrentDirectory As Long, lpStartupInfo As _<br>
STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) _<br>
As Long<br>
<br>
Private Declare Function CloseHandle Lib &quot;kernel32&quot; (ByVal hObject _<br>
As Long) As Long<br>
<br>
Private Const NORMAL_PRIORITY_CLASS = &H20&<br>
Private Const INFINITE = -1&<br>
<br>
Public Function ExecCmd(strCommandLine) As Long<br>
<br>
'Returns: Nonzero on success, zero on failure.<br>
<br>
Dim tProcess As PROCESS_INFORMATION<br>
Dim tStartup As STARTUPINFO<br>
Dim lReturn As Long<br>
<br>
On Error GoTo ExecCmdErr<br>
<br>
' Initialize the STARTUPINFO structure:<br>
tStartup.cb = Len(tStartup)<br>
<br>
' Start the shelled application:<br>
lReturn = CreateProcessA(0&, strCommandLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, tStartup, tProcess)<br>
<br>
' Wait for the shelled application to finish:<br>
lReturn = WaitForSingleObject(tProcess.hProcess, INFINITE)<br>
lReturn = CloseHandle(tProcess.hProcess)<br>
<br>
ExecCmd = lReturn<br>
<br>
Exit Function<br>
<br>
ExecCmdErr:<br>
'Return an error.<br>
ExecCmd = 0<br>
End Function<br>
<p>Steve Meier<br><a href=mailto:sdmeier@jcn1.com>sdmeier@jcn1.com</a><br><a href= > </a><br>
 
I tried this routing which works fine for notepad.&nbsp;&nbsp;However when using it with wordpad, control would not return to the calling program when exiting the program.&nbsp;&nbsp;(I do not want control to return until the wordpad file is closed)&nbsp;&nbsp;Any suggestions?<br><br>Dim hprog, hProc, RetVal As Long 'Used for Notepad routine<br>Private Declare Function OpenProcess Lib &quot;kernel32&quot; _<br>&nbsp;&nbsp;&nbsp;(ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, _<br>&nbsp;&nbsp;&nbsp;ByVal dwProcessId As Long) As Long<br>Private Declare Function WaitForSingleObject Lib &quot;kernel32&quot; _<br>&nbsp;&nbsp;&nbsp;(ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long<br>Private Declare Function CloseHandle Lib &quot;kernel32&quot; _<br>&nbsp;&nbsp;&nbsp;(ByVal hObject As Long) As Long<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' This routing works fine<br>Public Sub call notepad()<br>&nbsp;&nbsp;&nbsp;Const PROCESS_ALL_ACCESS = 0<br>&nbsp;&nbsp;&nbsp;hprog = Shell(&quot;NotePad.Exe c:\bas\vb\ballot.txt&quot;, vbMaximizedFocus) 'returns taskID<br>&nbsp;&nbsp;&nbsp;'Get process handle<br>&nbsp;&nbsp;&nbsp;hProc = OpenProcess(PROCESS_ALL_ACCESS, False, hprog)<br>&nbsp;&nbsp;&nbsp;'wait until the process terminates<br>&nbsp;&nbsp;&nbsp;If hProc &lt;&gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RetVal = WaitForSingleObject(hProc, INFINITE)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle hProc<br>&nbsp;&nbsp;&nbsp;End If<br>End Sub<br><br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;' This routing doesn't work right<br>Public Sub call wordpad()<br>&nbsp;&nbsp;Const PROCESS_ALL_ACCESS = 0<br>&nbsp;&nbsp;&nbsp;hprog = Shell(&quot;c:\program files\accessories\WordPad.Exe c:\bas\vb\ballot.txt&quot;, vbMaximizedFocus) 'returns taskID<br>&nbsp;&nbsp;&nbsp;'Get process handle<br>&nbsp;&nbsp;&nbsp;hProc = OpenProcess(PROCESS_ALL_ACCESS, False, hprog)<br>&nbsp;&nbsp;&nbsp;'wait until the process terminates<br>&nbsp;&nbsp;&nbsp;If hProc &lt;&gt; 0 Then<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RetVal = WaitForSingleObject(hProc, INFINITE)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CloseHandle hProc<br>&nbsp;&nbsp;&nbsp;End If<br>End Sub<br><br>Thanks in advance,<br>Dan
 
Dan,<br><br>Look at the 5th post in this thread, it should do what you want.&nbsp;&nbsp;Just copy and paste it into VB.<br><br> <p>Steve<br><a href=mailto:tribesaddict@swbell.net>tribesaddict@swbell.net</a><br><a href= > </a><br>
 
I belive one of those happens to be a cut/paste of my FAQ here. <p>Karl<br><a href=mailto:kb244@kb244.8m.com>kb244@kb244.8m.com</a><br><a href= </a><br>Experienced in : C++(both VC++ and Borland),VB1(dos) thru VB6, Delphi 3 pro, HTML, Visual InterDev 6(ASP(WebProgramming/Vbscript)<br>
 
Remember - the Shell function is for registered programs. if the program is not registered you may have to call the full path, such as: &quot;C:\windows\notepad.exe&quot;<br><br>My two cents. (Worth half a penny!)<br> <p>Cody ford<br><a href=mailto:codyford@yahoo.com>codyford@yahoo.com</a><br><a href= > </a><br>VB, VBS, Seagate Info/Crystal Reports<br>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top