Solved

run another .exe application

Posted on 2001-08-09
7
268 Views
Last Modified: 2006-11-17
from my project i want to run another .exe application
i used shell command but i want this .exe i run to be modal i mean while it's running i can't click a round out of the form.
maybe there is something better than Shell command ?

0
Comment
Question by:raed
[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
7 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 6367723
What if you make your form in the another app - modal?
0
 
LVL 39

Expert Comment

by:appari
ID: 6367737

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

Global Const NORMAL_PRIORITY_CLASS = &H20&
Global Const INFINITE = -1&
Declare Function CloseHandle Lib "kernel32" (hObject As Long) As Boolean
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
       
Public Declare Function WaitForInputIdle Lib "user32" (ByVal hProcess As Long, ByVal dwMilliseconds As Long) As Long


Public Sub ExecuteAndWait(strcmdLine As String)
    Dim NameOfProc As PROCESS_INFORMATION
    Dim NameStart As STARTUPINFO
    Dim X As Long
    NameStart.cb = Len(NameStart)
    X = CreateProcessA(0, strcmdLine$, 0&, 0&, 1, NORMAL_PRIORITY_CLASS, 0&, 0&, NameStart, NameOfProc)
    Call WaitForInputIdle(NameOfProc.hProcess, 0)
    Call CloseHandle(NameOfProc.hThread)
        Do
            X = WaitForSingleObject(NameOfProc.hProcess, -1)
            If X <> WAIT_TIMEOUT Then
                ' ?A?v???P?[?V?????I?I?????U?????B
                Exit Do
            End If
        Loop While True
   
   
    X = CloseHandle(NameOfProc.hProcess)
    DoEvents
End Sub


call the sub ExecuteAndWait with the exe name with full path as param

RAO

0
 
LVL 43

Expert Comment

by:TimCottee
ID: 6367742
raed,
Here is a way of doing this using API calls.

' G. Choquette, Enron Corp, copyright 1998
' module to execute another program (windows or dos)
' and make that program retain total focus until completion
' uses Win32 API calls and therefore will not work on win 3.x

Public Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer

As String, ByVal nSize As Long) As Long

Private 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

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

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


Private 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

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


Private Proc As PROCESS_INFORMATION
Private Start As STARTUPINFO
Private Ret As Long

Private Const NORMAL_PRIORITY_CLASS = &H20
Private Const HIGH_PRIORITY_CLASS = &H80
Private Const INFINITE = -1&

Public Sub ExecuteAndWait(CmdLine As String)
' Initialize the STARTUPINFO structure:
Start.cb = Len(Start)

' Start the shelled application:
Ret = CreateProcessA(0&, CmdLine, 0&, 0&, 1&, _
   NORMAL_PRIORITY_CLASS, 0&, 0&, Start, Proc)
DoEvents
' Wait for the shelled application to finish:
Ret = WaitForSingleObject(Proc.hProcess, INFINITE)  'time in milliseconds
DoEvents
Ret = CloseHandle(Proc.hProcess)
DoEvents
Ret = CloseHandle(Proc.hThread)
DoEvents
End Sub

You simply add this code to a module and then call

ExecuteAndWait "MyCommandLine"
0
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.

 
LVL 39

Accepted Solution

by:
appari earned 150 total points
ID: 6367760
hi Timcottee,

i suppose this is the standard method to execute another exe and waiting for it to be unloaded. i am using this method in our projects but i am facing a problem with this. after loading the second exe if i move the form displayed by second exe the back ground is not getting refreshed. do you have any solution for this problem. in fact i have posted a question at
http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=visualbasic&qid=20161206

if you have a solution can take the points.

RAO
0
 
LVL 38

Expert Comment

by:PaulHews
ID: 6367874
Here is a version of ExecCmd that keeps the calling app responsive using a DoEvents loop to wait for the called app to return.
Option Explicit

      Private 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

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

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

      Private 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

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

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

      Private Const NORMAL_PRIORITY_CLASS = &H20&
      Private Const INFINITE = -1&
      Private Const WAIT_TIMEOUT& = &H102&
    Private Const WAIT_ABANDONED& = &H80&
    Private Const WAIT_ABANDONED_0& = &H80&
    Private Const WAIT_FAILED& = -1&
    Private Const WAIT_IO_COMPLETION& = &HC0&
    Private Const WAIT_OBJECT_0& = 0


Public Function ExecCmd2(cmdline$)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret As Long
   
    On Error GoTo errExit
   
    ' Initialize the STARTUPINFO structure:
    start.cb = Len(start)
   
    ' Start the shelled application:
    ret = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
    NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
   
    ' Wait for the shelled application to finish:
    ret = WaitForSingleObject(proc.hProcess, 100)
    Do While ret = WAIT_TIMEOUT
        DoEvents
        ret = WaitForSingleObject(proc.hProcess, 100)
    Loop
   
    Call CloseHandle(proc.hProcess)
    ExecCmd2 = ret&
    Exit Function
errExit:

   
End Function
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6369028
hearing...
0
 

Expert Comment

by:devendra_patil
ID: 6371793
Use the Api Process32First and Process32Next to Enumerate all running processes.these api all return u the exe filenames of the running files.Disable ur form till u find that the .exe which u launched is running.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

695 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