?
Solved

How Do I Kill a Process in Visual Basic 6.0

Posted on 2003-02-24
12
Medium Priority
?
384 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:donkellogg
[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
  • 6
  • 6
12 Comments
 

Expert Comment

by:crystalby
ID: 8012743
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
0
 

Author Comment

by:donkellogg
ID: 8013230
Where do i put the name of the process? In place of Notepad?
0
 

Expert Comment

by:crystalby
ID: 8013346
Yeah, replace the Notepad with your process.
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:donkellogg
ID: 8013814
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?
0
 

Expert Comment

by:crystalby
ID: 8013941
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.
0
 

Author Comment

by:donkellogg
ID: 8014010
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?
0
 

Accepted Solution

by:
crystalby earned 400 total points
ID: 8014187
I think it's the window name for that process, another word your "Me.Caption" if that's the case.

Try it with this line.

hwnd = FindWindow(vbNullString, "NameOfProcess")

instead of
hwnd = FindWindow("Notepad", vbNullString)

0
 

Author Comment

by:donkellogg
ID: 8014368
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.
0
 

Expert Comment

by:crystalby
ID: 8014432
No worries.  I'm glad I could help! =)
0
 

Author Comment

by:donkellogg
ID: 8014483
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.
0
 

Author Comment

by:donkellogg
ID: 8014548
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.
0
 

Expert Comment

by:crystalby
ID: 8020807
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.  
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

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…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
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…
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…

801 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