|
-
Feb 9th, 2010, 05:55 AM
#1
Thread Starter
Junior Member
[RESOLVED] Question about handles / find window
hi. I made a program but i see in process explorer that the number of Handles of my app is counting up continously, compared to other regular programs. I beleived that it was caused by my multiple calls using ReadProcessMemory or OPenProcess API to an external program that was on a timer that triggers ever 1/3 of a sec.
Is it ok if i leave my program like that? how can i avoid that, but still able to check if the program is still running or not?
i also had problem looking for the handle for a particular program. i got managed SendMessage and WM_SetText to work but i need to input the handle to that control manually. i dont know why findwindowex did not work.
Here is the details spy++ about the control im targeting:
Handle: 0039110C [this one is changing evertime the program goes to system tray, when minized its not changing]
Caption: ""
Class:RichEdit20A
Style:501110C4
when i look on its properties First Child and Owner Windows is '(None)'
thanks!
-
Feb 9th, 2010, 07:29 AM
#2
Frenzied Member
Re: Question about handles / find window
No it is not okay to leave your program that way... That is a classic memory leak! And since you are using OpenProcess, you need to use CloseHandle...
Remarks
The handle returned by the OpenProcess function can be used in any function that requires a handle to a process, such as the wait functions, provided the appropriate access rights were requested.
When you are finished with the handle, be sure to close it using the CloseHandle function.
FindWindow, when called the first time, finds the parent window...
The FindWindow function retrieves a handle to the top-level window whose class name and window name match the specified strings. This function does not search child windows. This function does not perform a case-sensitive search.
You should/could use EnumChildWindows API to enumerate through the child windows of the other application looking for its class name... http://vbnet.mvps.org/index.html?cod...indowsdemo.htm
Good Luck
Option Explicit should not be an Option!
-
Feb 9th, 2010, 07:30 AM
#3
Re: Question about handles / find window
 Originally Posted by philanimetv
I beleived that it was caused by my multiple calls using ReadProcessMemory or OPenProcess API to an external program that was on a timer that triggers ever 1/3 of a sec.
That is certainly a possibility... you are Opening something, but are you also Closing it?
For example, with OpenProcess you need to close with CloseHandle:
http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx
-
Feb 9th, 2010, 07:34 AM
#4
Frenzied Member
Re: Question about handles / find window
[side]Wow! First time faster than the master [/side]
Option Explicit should not be an Option!
-
Feb 9th, 2010, 07:38 AM
#5
Re: Question about handles / find window
Memory management: if you reserve memory, make sure you free it.
API management: if you create something, make sure you destroy it.
Fail either one and your application will eventually crash. When using something new make sure you read the documentation carefully and that things really work the way you think they do. I know it takes time and slows down, but it is very much worth the time spent. Also, often when you lookup one API function you want to use you probably need another one.
-
Feb 10th, 2010, 02:09 AM
#6
Thread Starter
Junior Member
Re: Question about handles / find window
CloseHandle pHandle
It work for me, it was on the form unload before but now i transfered it at the end of my timers' code. My program didn't crash after more than 12 18 hours left running hehehe thanks for that.
ill try "EnumChildWindows API" now. thanks very much vb5prgrmr and for the others for helping.
i'll get back here after i tried Enum if i have problems.
Thanks!
-
Feb 10th, 2010, 03:39 AM
#7
Thread Starter
Junior Member
Re: Question about handles / find window
Code:
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const BM_CLICK = &HF5
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal Hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Sub Form_Load()
Dim lngresult As Long
lngresult = FindWindow(vbNullString, "Other Program Options") 'can be #32770 classname
Debug.Print "Main: " & vbTab & lngresult
lngresult = FindWindowEx(lngresult, 0, vbNullString, "RichEdit20A")
Debug.Print "Target: " & lngresult
End Sub
im still getting "0" at FindWindowEx.
-
Feb 10th, 2010, 08:00 AM
#8
Frenzied Member
Re: Question about handles / find window
lpszWindow
Pointer to a null-terminated string that specifies the window name (the window's title). If this parameter is NULL, all window names match.
Which means you need to pass a null terminated string for that arguement...
Code:
Dim CName As String * 12
CName = "RichEdit20A"
Now, put a break point after that and see the blank space after the text... That is what you need and if you still have problems use CName = "RichEdit20A" & Chr(0)
Good Luck
Option Explicit should not be an Option!
-
Feb 10th, 2010, 08:15 AM
#9
Re: Question about handles / find window
VB6 strings automatically have a null character at the end (BSTR structure: 4 bytes length + string data + 2 bytes null character). When passed to API the string is converted ANSI so that the null character is preserved. Thus that is not a problem and the code is OK.
-
Feb 10th, 2010, 08:21 AM
#10
Thread Starter
Junior Member
Re: Question about handles / find window
hi. I tried vb5prgrmr's suggestion but it didnt work
@vb5prgrmr: you are talking about the FindWindowsEx right?
-
Feb 11th, 2010, 01:03 AM
#11
Frenzied Member
Re: Question about handles / find window
Yes, I was but I copied from the wrong help file page...and it merri is right...
Option Explicit should not be an Option!
-
Feb 11th, 2010, 01:07 AM
#12
Thread Starter
Junior Member
Re: Question about handles / find window
i see, now how can i resolve my iss that i cant find my target handle value?
-
Feb 11th, 2010, 01:13 AM
#13
Frenzied Member
Re: Question about handles / find window
Give the EnumChildWindows API a try...
Good Luck
Option Explicit should not be an Option!
-
Feb 11th, 2010, 02:09 AM
#14
Thread Starter
Junior Member
Re: Question about handles / find window
i tried EnumChildWindows and i saw multiple lines in the debug text, and i found a line containing:
Enum {handle to control}, "RichEdit20A", {control's current text}
now how can i get only the handle vale for that control, because it returned a lot of unneeded handle values?
Last edited by philanimetv; Feb 11th, 2010 at 02:16 AM.
-
Feb 11th, 2010, 03:46 AM
#15
Thread Starter
Junior Member
Re: Question about handles / find window
I Found a workaround somehow. I inserted a code that whenever it enumerates childwindows and finds a match with a classname RichEdit20A, it will stop enumerating ang will store the value of the hwind of found classname to a control in form1.
I found out why i cant find the RichEdit20A because i need to activate that childwindow that contains the RichEdit20A control before enumchilwindows find the handle to that.
Thanks for the help! i greatly apreciate it.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|