Solved

Using EnumChildWindows crash my application

Posted on 2004-05-02
12
873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

728 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