Link to home
Start Free TrialLog in
Avatar of moonlight
moonlight

asked on

Using EnumChildWindows crash my application

I am trying to track down another app by its name and kill it. I am almost successful, I do a EnumWindows and check the window names in a call back function.

I check that the window doesnt have a parent and matches the name and then I add it to my dletion container. afterwards when im done with the loop I kill it.

BUT.. :(

the window is killed. but if it has a modal window open, the modal window is not. I am trying to once adding the window for deletion, EnumChildWindows. But when I come back from the Call back function, my whole app crashes. why??

some code Inside first Callback:

If Left$(str2, Len(searchText)) Like searchText Then
    lParent = GetParent(hwnd)
    If lParent = 0 Then
       colHandle.Add hwnd
       EnumChildWindows hwnd, AddressOf fEnumChildWindowsCallBack, 0
       text = str
    End If
End If


.... and My callback:

Declare Function EnumChildWindows Lib "user32" (ByVal hwnd As Long, ByVal lpEnumFunc As Long, ByVal lparam As Long) As Long

Public Function fEnumChildWindowsCallBack(ByVal hwnd As Long, ByVal lparam As Long)
    fEnumChildWindowsCallBack = False
End Function


anotehr pecuiar thing.. if I use a debugger.. even my debugger crash after end function statement of the callback..
SOLUTION
Avatar of kklai72
kklai72

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
Avatar of moonlight
moonlight

ASKER

Thank you, you have already earned your points.. but I still can't close the windows belonging to that process, the modal windows for prefernxes or wizards, they seem not to be child windows but top windows, and their names are different depending on the language of the program.. is there no way to just say, hey, I want to close all the apps windows?
If you want to close the windows you got from the EnumChildWindow, you can simply use:

SendMessage hwnd,WM_CLOSE, 0,0

However, when you want to close the windows that belong to some process, then you should have the handle of the parent process and use (where hwnd is the handle of parent window)

SendMessage hwnd, WM_CLOSE,0,0

I am doing just that.. around the GetWindowText in my example I have

lParent = GetParent(hwnd)
If lParent = 0 Then...

but it doesnt close the "child" windows. More ever, even the child windows qualify in the If lParent = 0 Then.. statement... If being a child to the main window is not their connection. then how do I find it? but it belongs to the same process. when I have sent the sendmessage the main window closes, but the process and the modal preferences window are still going :(
firstly, I have a little bit confuse with what you want, is it that you can close an application if the application opened a modal window?

I have tried to close an application with "File Open" dialog opened. It works with something like that

EnumChildWindows hTarWnd, AddressOf EnumChildProc, 0

Public Function EnumChildProc(ByVal hwnd as Long, ByVal lParam as Long) as Boolean
           PostMessage hwnd, WM_CLOSE, 0, 0
           EnumChildProc=True
End Function
of course, you have to close the main window by:

PostMessage hTarWnd, WM_CLOSE, 0, 0

assume that hTarWnd is the handle of the parent window

Yes, I want to close the application. When sending WM_CLOSE to the main window, it closes, but the windows it has opened does not.My problem is I am sending that to the main window. I am shutting the main window down, but if it has opened other windows, they dont dissapear and they hold the process going, and I cannot shut them the same way since they have common names, like preferences, and they differ from one language version of the program to the other. What I want is to tell the process to shut down all its windows.Hope that clarifies it.

I have added more points to the question btw
ASKER CERTIFIED SOLUTION
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 I wil try this. A question now, is me going through the child windows necessary at all ?
I mean is a window required to shut down all its children when getting a WM_CLOSE? in that case my second callback is of no use?
Ok I have declared

Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Boolean, ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Boolean
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long

and

put your code after the window killing code:
GetWindowThreadProcessId colHandle.Item(1), pid
    If pid <> 0 Then
        MsgBox "Pid is " + CStr(pid), vbOKOnly, "Pid"
        hProcess = OpenProcess(PROCESS_TERMINATE, False, pid)
        TerminateProcess hProcess, 0
    End If

The result is that it finds the correct pid. but the process is still not terminated, and the modal window is still showing :o
I think there is a limitation in both sending WM_CLOSE, and TerminateProcess just like when you kill a process using "End Task" of task manager. It won't always successfully kill the process after you click the "End Task" button, you have to wait or some dialog asks for your reply.

I think what you can do is to monitor the behaviour of the process when you kill it mannual using task manager. If task manager can't give you a good result. You can't made it by using VB program.
its ok, I have solved it. I used EnumThreadWindows, and succeeded to kill all windows :)
thanks for your help, I was clueless..