Solved

Using EnumChildWindows crash my application

Posted on 2004-05-02
12
854 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 3

Author Comment

by:moonlight
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 3

Author Comment

by:moonlight
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

762 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now