Solved

Running EXE's in VB6

Posted on 2000-05-11
4
381 Views
Last Modified: 2008-02-26
Is there any controls, methods or objects or whatever that can actually allow you to run other .exe files in vb6?
0
Comment
Question by:da_dawg88
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 2

Expert Comment

by:Crin
ID: 2800279
Hello,
Try to use this function, it works both on NT/2000 and 95/98 OS.

Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long


Sincerely yours,

Crin
0
 
LVL 1

Expert Comment

by:4P
ID: 2800319
I just wanted to comment that the ShellExecute API call is primarely used to open documents (such as a Word document or an Excel workbook) without knowing where the host application is installed on the end user's computer.

A simpler solution is the use of the Shell function. This is a VB function, very simple to call.

RetVal = Shell(ExePath[, WinStyle])
where
ExePath is the path and name of the application you want to run.
WinStyle determines how to open the application, and it can be one of the following constants: vbHide, vbNormalFocus, vbMinimizedFocus, vbMaximizedFocus, vbNormalNoFocus, vbMinimizedNoFocus.
RetVal is the task ID of the started application.
0
 
LVL 3

Accepted Solution

by:
paulstamp earned 70 total points
ID: 2800409
One additional comment - 4P is right - shellexecute is really for opening documents rather than running EXEs, and Shell is VB's built-in way of running EXEs.

The drawback with Shell is that often you want your program to launch another EXE and WAIT until that EXE has finished running before your program resumes. To do this, you need to use something like the following :

(PLACE THE FOLLOWING IN GENERAL DECLARATIONS SECTION OF A STANDARD MODULE)

Public Type PROCESS_INFORMATION
   hProcess As Long
   hThread As Long
   dwProcessId As Long
   dwThreadId As Long
End Type

Public 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

Public Declare Function CreateProcess Lib "kernel32" Alias "CreateProcessA" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long



(PLACE THE FOLLOWING SUB ANYWHERE - USUALLY IN A MODULE)

Sub RunCommand(ByVal strCommand As String)

   Const NORMAL_PRIORITY_CLASS = &H20
   
   Dim udtProcessInfo As PROCESS_INFORMATION
   Dim udtStartUpInfo As STARTUPINFO
   Dim lngHandle As Long
   Dim lRC As Long
   
   ' Initialize the STARTUPINFO structure:
   udtStartUpInfo.cb = Len(udtStartUpInfo)

   ' Start the shelled application:
   If CreateProcess(0&, strCommand, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, udtStartUpInfo, udtProcessInfo) <> 0 Then
      ' Save the process handle
      lngHandle = udtProcessInfo.hProcess
   End If

   ' Check to see if Windows was able to start it...
   If lngHandle = 0 Then
      MsgBox "Failed to start "
      Exit Sub
   End If

   ' Wait until done...
   Do Until (WaitForSingleObject(lngHandle, 500&) = 0)
   Loop

   ' Close the handle to the process...
   lRC = CloseHandle(lngHandle)
   
End Sub
0
 

Author Comment

by:da_dawg88
ID: 2800507
Thanks a lot for your help
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

730 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