No VB6.
Main Topics
Browse All TopicsI thought I had solved my problem in this prior thread (http://www.experts-exchan
The process that I want to kill doesn't have a window so FindWindow() doesn't find hwnd so subsequently I can't terminate the process. I can see the Process in TaskManager and can kill it from there but need to kill it from code.
How else can I find the hwnd?
Thanks,
NG
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I think this is what you want: http://www.experts-exchang
By the way, this task is like 3 lines of code in .Net, and you can get Visual Basic .Net Express for free. ;)
Hi nike_golf,
I'll try to explain the difference here but the rules still apply for both. The main thing you need to terminate a process is the (Process Id) because we need this to get a real handle to the that process using OpenProcess(). There is a coulpe different ways to achieve this. In your other question you knew the title and wanted to kill that specific instance of the application so what we did was use FindWindow() to get the windows handle then we used GetWindowThreadProcessId()
The above is different in this case you won't be able to use FindWindow() instead you will have to find a match in the executable name of running list of processes on the computer. The link provided by (tgerbert) is exactly what you need to do in this situation however it will need some modification to work correctly.
Use the following API instead:
CreateToolhelp32Snapshot
Process32First
Process32Next
This will fill a PROCESSENTRY32 structure for each process on the system.
The szExeFile of this structure is the executable name that you will need to match with the executable name of the application being terminated.
The th32ProcessID of this structure is what you will use in OpenProcess() if there is a match in the executable name.
Hope that helps.
The link for VB .Net Express is http://www.microsoft.com/e
But definitely have a look at VB .Net, make a copy of one of your VB6 projects and let VB.Net upgrade it, poke around a bit.
And if you're curious, in .Net to find a process by name and kill it can be done in 1 line:
Process.GetProcessesByName
(Of course in reality you'd have some error checking etc).
Ultimately I used the following:
Private 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
Private Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot"
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long
Private Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)
Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_TERMINATE As Long = &H1
Public Function KillApp(EXEName As String) As Boolean 'Kills application
Dim hSnapShot As Long, nProcess As Long
Dim uProcess As PROCESSENTRY32
hSnapShot = CreateToolhelpSnapshot(2, 0)
uProcess.dwSize = LenB(uProcess)
nProcess = Process32First(hSnapShot, uProcess)
Do While nProcess
If InStr(1, uProcess.szExeFile, EXEName, vbTextCompare) > 0 Then
'ProcessKill uProcess.th32ProcessID
MsgBox "found" & nProcess
KillApp = True
End If
nProcess = Process32Next(hSnapShot, uProcess)
Loop
CloseHandle hSnapShot
End Function
Public Function ProcessKill(ByVal ProcessID As Long) As Boolean
Dim hProc As Long
Const fdwAccess As Long = SYNCHRONIZE Or PROCESS_TERMINATE
hProc = OpenProcess(fdwAccess, 0&, ProcessID)
If hProc Then
If TerminateProcess(hProc, 0&) Then
ProcessKill = True
End If
Call CloseHandle(hProc)
End If
End Function
Public Sub closeapp()
answer = KillApp("sqlservr.exe") 'Used sqlservr.exe as a test application
MsgBox answer
End Sub
Business Accounts
Answer for Membership
by: tgerbertPosted on 2009-10-30 at 11:57:29ID: 25705854
Are you using Visual Basic .Net?