Solved

Running EXE's in VB6

Posted on 2000-05-11
4
375 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
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

810 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