Solved

Using EnumChildWindows crash my application

Posted on 2004-05-02
12
866 Views
Last Modified: 2007-12-19
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
Comment
Question by:moonlight
  • 6
  • 6
12 Comments
 
LVL 3

Assisted Solution

by:kklai72
kklai72 earned 100 total points
ID: 10975212
you missed Boolean at the end

Public Function fEnumChildWindowsCallBack(ByVal hwnd As Long, ByVal lparam As Long) As Boolean
    fEnumChildWindowsCallBack = False
End Function
0
 
LVL 3

Author Comment

by:moonlight
ID: 10975951
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
 
LVL 3

Expert Comment

by:kklai72
ID: 10976164
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:moonlight
ID: 10976447
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
 
LVL 3

Expert Comment

by:kklai72
ID: 10976669
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
 
LVL 3

Expert Comment

by:kklai72
ID: 10976692
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
 
LVL 3

Author Comment

by:moonlight
ID: 10977008
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
 
LVL 3

Accepted Solution

by:
kklai72 earned 100 total points
ID: 10977339
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
 
LVL 3

Author Comment

by:moonlight
ID: 10977834
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
 
LVL 3

Author Comment

by:moonlight
ID: 10978523
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
 
LVL 3

Expert Comment

by:kklai72
ID: 10982832
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
 
LVL 3

Author Comment

by:moonlight
ID: 10982848
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

763 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