Solved

run another .exe application

Posted on 2001-08-09
7
221 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
7 Comments
 
LVL 28

Expert Comment

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

Expert Comment

by:appari
Comment Utility

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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 39

Accepted Solution

by:
appari earned 150 total points
Comment Utility
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
Comment Utility
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
Comment Utility
hearing...
0
 

Expert Comment

by:devendra_patil
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now