Solved

tough one...

Posted on 2002-04-22
6
147 Views
Last Modified: 2010-05-02
hi
i have a program that runs a exe
i use the shell command
the question is how can i close the program within the code?
my guess is ill have to use API
Help!!
0
Comment
Question by:eyal_shapira
6 Comments
 
LVL 20

Accepted Solution

by:
hes earned 100 total points
ID: 6960589
Use the following
Declare

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 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 TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

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

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


In your code
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO

'Initialize the STARTUPINFO structure:
start.cb = Len(start)

'Start the shelled application:
ret& = CreateProcessA(0&, "Your.exe", 0&, 0&, 1&, _
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)

' When you want to stop it

ret& = TerminateProcess(proc.hProcess, 0)
ret& = CloseHandle(proc.hProcess)
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6960655
A cheapest one:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Private Sub Form_Load()
Dim ret As Long
ret = Shell("notepad", vbNormalFocus)
If ret <> 0 Then
    Sleep 2000
    AppActivate ret
    SendKeys "%{F4}"
End If
End Sub
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 6960677
listening
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:eyal_shapira
ID: 6960727
10x...
where do u find good material on API
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6960728
Of course, you need to set ret to a form scope variable and call:
AppActivate ret
   SendKeys "%{F4}"

when you want to close it.
I used sleep api just to wait for two second as an example.
Cheers
0
 
LVL 20

Expert Comment

by:hes
ID: 6960994
You are wlcome
Look Here

http://www.allapi.net/
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

813 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

9 Experts available now in Live!

Get 1:1 Help Now