Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to kill a shell task ID 2

Status
Not open for further replies.

marcdibi

Technical User
Mar 18, 2008
12
US
Hello

I have extensive crystal filter design software under,
Excel/VB6 that I am linking to Eagleware for analysis. I am able to toggle between the design software and Eagleware.
This is where I need help . . .how do I check if dTaskID already exists (if YES kill it --> close the Eagleware application) before executing the shell command? Below is a sample of my code:

Dim dTaskID As Double, path As String
path = "C:\Program Files\GENESYS2008.01\Bin\Genesys.exe"
FileName2 = Sheet7.Cells(1, 12).Text
' Execute Filename2 Using Path
dTaskID = Shell(path + " " + FileName2, vbNormalFocus)

Thanks,
Regards,
Marc Dibi
 
Maybe try something like this:

Dim myObj as object

set myObj = Getobject(,"Genesys.Application")

If the set statement errors out then there's no instance of the application loaded and you can go from there.

I'm sure there'll be some other solutions along shortly but I think that might work.
 
Add the following to a module in your project:

Code:
Option Explicit
Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwFlags As Long
    szexeFile As String * 260
End Type
'-------------------------------------------------------
Declare Function OpenProcess Lib "kernel32.dll" (ByVal dwDesiredAccess As Long, _
ByVal blnheritHandle As Long, ByVal dwAppProcessId As Long) As Long


Declare Function ProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long


Declare Function ProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, _
uProcess As PROCESSENTRY32) As Long


Declare Function CreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" ( _
ByVal lFlags As Long, lProcessID As Long) As Long


Declare Function TerminateProcess Lib "kernel32.dll" (ByVal ApphProcess As Long, _
ByVal uExitCode As Long) As Long


Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long

 '-------------------------------------------------------
Public Sub KillProcess(NameProcess As String)

    Const PROCESS_ALL_ACCESS = 0
    Const PROCESS_TERMINATE = (&H1)
    Const TH32CS_SNAPPROCESS As Long = 2&
    Dim uProcess As PROCESSENTRY32
    Dim RProcessFound As Long
    Dim hSnapshot As Long
    Dim SzExename As String
    Dim ExitCode As Long
    Dim MyProcess As Long
    Dim AppKill As Boolean
    Dim AppCount As Integer
    Dim i As Integer
    Dim WinDirEnv As String
    
    
    If NameProcess <> "" Then
        AppCount = 0
        
        uProcess.dwSize = Len(uProcess)
        hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
        RProcessFound = ProcessFirst(hSnapshot, uProcess)
        
        Do
            i = InStr(1, uProcess.szexeFile, Chr(0))
            SzExename = LCase$(Left$(uProcess.szexeFile, i - 1))
            WinDirEnv = Environ("Windir") + "\"
            WinDirEnv = LCase$(WinDirEnv)
            
            If Right$(SzExename, Len(NameProcess)) = LCase$(NameProcess) Then
                AppCount = AppCount + 1
                MyProcess = OpenProcess(PROCESS_TERMINATE, False, uProcess.th32ProcessID)
                AppKill = TerminateProcess(MyProcess, ExitCode)
                Call CloseHandle(MyProcess)
            End If
            RProcessFound = ProcessNext(hSnapshot, uProcess)
        Loop While RProcessFound
        Call CloseHandle(hSnapshot)
    End If


End Sub

Use it as follows:
Code:
KILLPROCESS(Genesys.exe)

This is not my code. I found this on the web (in a place I can't remember) and have been using it in a labeling application to kill WINWORD.

-Brian
 
RivetHed,
Thanks for your hand.It makes sense to try.

Brian,
Thanks for the code. I'll test it and let you know.

Regards,
Marc
 
Hi Brian,

How do you put stuff in CODE presentation like you did in your reply?

Thanks.
Marc
 
In the bottom it says 'Process TGML' click on the words 'Process TGML' for examples
 
RivetHed,
The second code doesn't give me an error, but the first one did. Maybe Genesys is not recognized as an application...?

Brian,
You Welcome for the star and thanks for the TGML process.
I need help, since both KillProcess do nothing. I stored your code in another module in my VBA/Excel project.

Code:
<html>
  <head>
    <title>Page</title>
  </head>
  <body>
   Sub Analysis_Genesys()

KillProcess (Genesys)

Dim dTaskID As Double, path As String
Dim myObj As Object

path = "C:\Program Files\GENESYS2008.01\Bin\Genesys.exe"
Filename2 = Sheet7.Cells(1, 12).Text

' Genesys application exits?
Set myObj = GetObject("C:\Users\mdibi\Model.wsx")   'GetObject([pathname] [, class])

' Execute Filename2 Using Path
dTaskID = Shell(path + " " + Filename2, vbNormalFocus)

End Sub


Sub Test()

KillProcess (WINWORD)

Dim ID As Double, p As String, myObj As Object

p = "C:\Program Files\Microsoft Office\Office\WINWORD.EXE"
Filename2 = Sheet7.Cells(1, 12).Text

' Winword application exits? GetObject([pathname] [, class])
Set myObj = GetObject(Filename2)

' Execute Filename2 Using Path
ID = Shell(p + " " + Filename2, vbNormalFocus)

End Sub

  </body>
</html>

When I run the full code in his VB module, a Macro window pops up and wait for me to choose a macro name. So i create a macro name Kill() and I call it. But still nothing.

Code:
<html>
  <head>
    <title>Page</title>
  </head>
  <body>
Sub Kill()

KillProcess (Genesys)

End Sub
  </body>
</html>

Thanks for your time.
Regards,
Marc
 
No, just coping to much script from the help...:|

Code:
Sub Analysis_Genesys()

KillProcess (Genesys)

Dim dTaskID As Double, path As String
Dim myObj As Object

path = "C:\Program Files\GENESYS2008.01\Bin\Genesys.exe"
Filename2 = Sheet7.Cells(1, 12).Text

' Genesys application exits?
Set myObj = GetObject("C:\Users\mdibi\Model.wsx")   'GetObject([pathname] [, class])

' Execute Filename2 Using Path
dTaskID = Shell(path + " " + Filename2, vbNormalFocus)

End Sub


Sub Test()

KillProcess (WINWORD)

Dim ID As Double, p As String, myObj As Object

p = "C:\Program Files\Microsoft Office\Office\WINWORD.EXE"
Filename2 = Sheet7.Cells(1, 12).Text

' Winword application exits? GetObject([pathname] [, class])
Set myObj = GetObject(Filename2)

' Execute Filename2 Using Path
ID = Shell(p + " " + Filename2, vbNormalFocus)

End Sub

When I run the full code (from Brian) in a VB module, a Macro window pops up and wait for me to choose a macro name. So i create a macro name Kill() and I call it. But still nothing.

Code:
Sub Kill()

KillProcess (Genesys)

End Sub


Thanks for your help.
Regards,
Marc
 
Awesome! That so beautiful! You should see it! Ha Ha! ;)

Thanks Brian!
Regars,
Marc Dibi
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top