Link to home
Start Free TrialLog in
Avatar of donkellogg
donkelloggFlag for United States of America

asked on

How Do I Kill a Process in Visual Basic 6.0

I'm coding on a program for work, but I'm fairly new to Visual Basic. The program is 99% done but I have yet to figure out how to do the last part.

The code program needs kill the process of the program before preforming it's actions. The name of the Process that I need to kill is protouch.exe .

I've been searching the web, and found lots of samples but I can't get any of the code I've found to work. Could someone whip up a code block for me? Also I'm new to functions and other VB things, so could you include directions as to how to use the code.
Avatar of crystalby
crystalby

Try having a go with this.

Option Explicit

Public Declare Function TerminateProcess _
    Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

Public Declare Function FindWindow _
    Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long

Public Declare Function PostMessage _
    Lib "user32" Alias "PostMessageA" _
    (ByVal hwnd As Long, ByVal wMsg As Long, _
    ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WM_CLOSE = &H10
Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Public Declare Function GetWindowThreadProcessId _
    Lib "user32" (ByVal hwnd As Long, _
    lpdwProcessId As Long) As Long

Public Declare Function OpenProcess Lib "kernel32" _
    (ByVal dwDesiredAccess As Long, ByVal bInheritHandle _
    As Long, ByVal dwProcessId As Long) As Long



Sub Main()
    Dim hwnd As Long
    Dim ProcessId As Long
    Dim hProcess As Long
    Dim TermSucc As Boolean
   
    hwnd = FindWindow("Notepad", vbNullString)
   
    GetWindowThreadProcessId hwnd, ProcessId
   
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, True, ProcessId)
    TermSucc = TerminateProcess(hProcess, 0)
   
    'PostMessage hwnd, WM_CLOSE, 0&, 0&
End Sub


Option Explicit

Private Sub Command1_Click()
    Dim hwnd As Long
    Dim ProcessId As Long
    Dim hProcess As Long
    Dim TermSucc As Boolean
   
    hwnd = FindWindow("Notepad", vbNullString)
   
    GetWindowThreadProcessId hwnd, ProcessId
   
    hProcess = OpenProcess(PROCESS_ALL_ACCESS, True, ProcessId)
    TermSucc = TerminateProcess(hProcess, 0)
   
    'PostMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
Avatar of donkellogg

ASKER

Where do i put the name of the process? In place of Notepad?
Yeah, replace the Notepad with your process.
I'm getting a compiler errror. I had trie dthis code earlier I think I found it on a web site can't remember which. Hopefully I'm wrong and you can figure what the problem is.

Compiler Error..

constants, Fix-Length Strings, arrays user defiend types and declared statements not allowed as public member of object models.

IT fails in DEBUG on these two lines..

Public Const WM_CLOSE = &H10
Public Const PROCESS_ALL_ACCESS = &H1F0FFF

anyone have a clue what I need to do?
This part needs to be in the module, not the form itself.
*************************************
Option Explicit

Public Declare Function TerminateProcess _
   Lib "kernel32" (ByVal hProcess As Long, _
   ByVal uExitCode As Long) As Long

Public Declare Function FindWindow _
   Lib "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long

Public Declare Function PostMessage _
   Lib "user32" Alias "PostMessageA" _
   (ByVal hwnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, ByVal lParam As Long) As Long

Public Const WM_CLOSE = &H10
Public Const PROCESS_ALL_ACCESS = &H1F0FFF

Public Declare Function GetWindowThreadProcessId _
   Lib "user32" (ByVal hwnd As Long, _
   lpdwProcessId As Long) As Long

Public Declare Function OpenProcess Lib "kernel32" _
   (ByVal dwDesiredAccess As Long, ByVal bInheritHandle _
   As Long, ByVal dwProcessId As Long) As Long

******************************


This part is in the form.
*******************************
Option Explicit

Private Sub Command1_Click()
   Dim hwnd As Long
   Dim ProcessId As Long
   Dim hProcess As Long
   Dim TermSucc As Boolean
   
   hwnd = FindWindow("Notepad", vbNullString)
   
   GetWindowThreadProcessId hwnd, ProcessId
   
   hProcess = OpenProcess(PROCESS_ALL_ACCESS, True, ProcessId)
   TermSucc = TerminateProcess(hProcess, 0)
   
   'PostMessage hwnd, WM_CLOSE, 0&, 0&
End Sub
*******************************

The problem you had is because you have a public constant declare in a standard form.
It works with NOTEPAD but not with my Program. I replaced the app name NOTEPAD with the PROCESS I need to kill protouch.exe, no go :(

I have tried protouch, protouch.exe neither works.

Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of crystalby
crystalby

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you it was just a matter of changing the order of the vbNullstring. The code works perfect now thanks so much enjoy the 100 Points.
No worries.  I'm glad I could help! =)
Point of note...

I figured a way around the problem, the app I'm trying to kill has 3 different states for the TITLE of the window. They are WS16TERM, SYSNODE, or WS16TERM => (SYSNODE) I just made 3 different statements to makes sure it would kill the thing no matter which state it was in. A few variables here and there..

Wonder if there is a way to kill a proces by it's name in VB.. anyway I hacked this to fix my needs thanks.
Point of note...

I figured a way around the problem, the app I'm trying to kill has 3 different states for the TITLE of the window. They are WS16TERM, SYSNODE, or WS16TERM => (SYSNODE) I just made 3 different statements to makes sure it would kill the thing no matter which state it was in. A few variables here and there..

Wonder if there is a way to kill a proces by it's name in VB.. anyway I hacked this to fix my needs thanks.
I guess the secret is behind the

hwnd = FindWindow(vbNullString, "NameOfProcess")
and
hwnd = FindWindow("Notepad", vbNullString)

as long as it can find the handle for your app, the rest is just check and kill the process.


******************************************
Syntax

HWND FindWindow(LPCTSTR lpClassName, LPCTSTR lpWindowName
);
Parameters

lpClassName
[in] Pointer to a null-terminated string that specifies the class name or a class atom created by a previous call to the RegisterClass or RegisterClassEx function. The atom must be in the low-order word of lpClassName; the high-order word must be zero.
If lpClassName points to a string, it specifies the window class name. The class name can be any name registered with RegisterClass or RegisterClassEx, or any of the predefined control-class names.

If lpClassName is NULL, it finds any window whose title matches the lpWindowName parameter.

lpWindowName
[in] Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.

******************************************

To find your process, I guess you will need to look for the lpClassName.  It sounded like your process needs to be registered for it to pick out the lpClassName.