Closing process (cant find target window)

Luc

Well-known member
Joined
Nov 29, 2005
Messages
59
Programming Experience
1-3
I'm trying to be able to close processes in my application but he's never able to find the window handle, i would appreciate anny help.

declaration: (copy pasted this from a site and read a tutorial on how to use it)
[FONT=&quot] Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer[/FONT]
[FONT=&quot] Private Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer[/FONT]
[FONT=&quot] Private Declare Function OpenProcess Lib "kernel32" Alias "OpenProcess" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As Integer[/FONT]
[FONT=&quot] Private Declare Function CloseHandle Lib "kernel32" Alias "CloseHandle" (ByVal hObject As Integer) As Integer[/FONT]
[FONT=&quot] Private Declare Function TerminateProcess Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Integer, ByVal uExitCode As Integer) As Integer[/FONT]
[FONT=&quot] Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Integer, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer[/FONT]
[FONT=&quot] Private Const SYNCHRONIZE As Integer = &H100000[/FONT]
[FONT=&quot] Private Const PROCESS_TERMINATE As Integer = &H1[/FONT]
[FONT=&quot] Private Const WM_CLOSE As Integer = &H10 '[/FONT]

this goes in my button:
Dim hndl, proc_ID, proc_hndl As Integer
hndl = FindWindow(vbNullString, "explorer")
If hndl = 0 Then MessageBox.Show("Couldn't find") : Exit Sub

proc_ID = 0
GetWindowThreadProcessId(hndl, proc_ID)
If proc_ID = 0 Then MessageBox.Show("proc_ID error") : Exit Sub

proc_hndl = OpenProcess(SYNCHRONIZE Or PROCESS_TERMINATE, 0, proc_ID)

If proc_hndl = 0 Then MessageBox.Show("proc hndlr fout!") : Exit Sub

If (TerminateProcess(proc_hndl, 0) = 0) Then
MessageBox.Show("Failed to close")
Else
MessageBox.Show("Closed")
End If

CloseHandle(proc_hndl)
Thx
 
You just itterated through process collection right?
Something like:
VB.NET:
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] UBound(Diagnostics.Process.GetProcessesByName(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2] ActiveProcess("certain app")
[/SIZE][SIZE=2][COLOR=#0000ff]End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
where ActiveProcess routine holding soemthing like:
VB.NET:
{...}
[SIZE=2][COLOR=#008000]'Variable to hold individual Process.
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objProcess [/SIZE][SIZE=2][COLOR=#0000ff]As [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Process
[/SIZE][SIZE=2][COLOR=#008000]'Collection of all the Processes running on local machine
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] objProcesses() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Process
[/SIZE][SIZE=2][COLOR=#008000]'Get all processes into the collection
[/COLOR][/SIZE][SIZE=2]objProcesses = Process.GetProcesses()
[/SIZE][SIZE=2][COLOR=#0000ff]For [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Each[/COLOR][/SIZE][SIZE=2] objProcess [/SIZE][SIZE=2][COLOR=#0000ff]In[/COLOR][/SIZE][SIZE=2] objProcesses
[/SIZE][SIZE=2][COLOR=#008000]  'Check and exit if we have certain app running already
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  If[/COLOR][/SIZE][SIZE=2] UCase(objProcess.MainWindowTitle) = UCase(argStrAppToFind) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]    MessageBox.Show("Application " & argStrAppToFind & " is already running.", "", MessageBoxButtons.OK, MessageBoxIcon.Information)
    PrevHndl = objProcess.MainWindowHandle.ToInt32()
[/SIZE][SIZE=2][COLOR=#0000ff]    Exit [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]For
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]  End [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Next
[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff][COLOR=#000000]{...}[/COLOR][/COLOR][/SIZE]


Regards ;)
 
Back
Top