Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 887
  • Last Modified:

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..
0
moonlight
Asked:
moonlight
  • 6
  • 6
2 Solutions
 
kklai72Commented:
you missed Boolean at the end

Public Function fEnumChildWindowsCallBack(ByVal hwnd As Long, ByVal lparam As Long) As Boolean
    fEnumChildWindowsCallBack = False
End Function
0
 
moonlightAuthor Commented:
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?
0
 
kklai72Commented:
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

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
moonlightAuthor Commented:
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 :(
0
 
kklai72Commented:
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
0
 
kklai72Commented:
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

0
 
moonlightAuthor Commented:
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
0
 
kklai72Commented:
It is a rude way to close an application by TerminateProcess, try this

GetWindowThreadProcessId hTarWnd, pid
hProcess=OpenProcess(PROCESS_TERMINATE, FALSE, pid)
TerminateProcess hProcess, 0

Assume hTarWnd is the main window of a process.

Let's see if it can help
0
 
moonlightAuthor Commented:
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?
0
 
moonlightAuthor Commented:
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
0
 
kklai72Commented:
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.
0
 
moonlightAuthor Commented:
its ok, I have solved it. I used EnumThreadWindows, and succeeded to kill all windows :)
thanks for your help, I was clueless..
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now