[RESOLVED] FindWindow api problem
i have used
FindWindow, GetWindow , GetClassName ,GetWindowTextLength , GetWindowText, PostMessage
APIs to implement BM_CLICK (to click button of another exe and its working fine)
But after closing vbp, im getting windows debug window !? (if exe used then it will be closed)
Any idea ?
thank you.
Re: FindWindow api problem
Can you give more details please? Such as the steps needed to replicate the issue, the OS used, etc.
Re: FindWindow api problem
Do you mean that the VB6 IDE is aborting when you close the Program?
Perhaps you could post your code. The most common reason when using APIs is not handling String Arguments properly.
Re: FindWindow api problem
Code:
'form
Const GW_CHILD = 5
Const GW_HWNDNEXT = 2
Private Const BM_CLICK = &HF5
Private Sub Command1_Click()
On Error GoTo a1
GetXLDESKChildWindows
Exit Sub
a1:
MsgBox Err.Description
End Sub
Sub GetXLDESKChildWindows()
On Error GoTo b1
Dim hWndParent As Long, hWndDskTop As Long, hWndChild As Long, Ret As Long
Dim strBuffer As String * 250
Dim i As Long
hWndDskTop = FindWindow(vbNullString, "F1")
hWndChild = GetWindow(hWndDskTop, GW_CHILD)
i = 1
Do Until hWndChild = 0
GetClassName hWndChild, strClass, 250
Ret = GetWindowTextLength(hWndChild)
strBuffer = Space(Ret)
GetWindowText hWndChild, strBuffer, Ret + 1
If hWndChild <> 0 Then
Call PostMessage(hWndChild, BM_CLICK, 0&, ByVal 0&)
End If
hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
i = i + 1
Loop
Exit Sub
b1:
MsgBox Err.Description
End Sub
'Module
Public Declare Function FindWindow Lib "user32" _
Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) _
As Long
'Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public 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
Public Declare Function SendMessage Lib "user32" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
lParam As Any) _
As Long
Public Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function EnumChildWindows Lib "user32" (ByVal hWndParent As Long, ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
Public Declare Function GetClassName _
Lib "user32" _
Alias "GetClassNameA" ( _
ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) _
As Long
Public Declare Function GetWindowText _
Lib "user32" _
Alias "GetWindowTextA" ( _
ByVal hwnd As Long, _
ByVal lpString As String, _
ByVal cch As Long) _
As Long
Public Declare Function GetWindowTextLength _
Lib "user32" _
Alias "GetWindowTextLengthA" ( _
ByVal hwnd As Long) _
As Long
Public Function EnumChildProc(ByVal hwnd As Long, ByVal lParam As Long) As Long
Dim A$, G$, L&
A = Space$(128)
L = GetClassName(hwnd, A, 128)
G = Left(A, L)
If LCase$(G) <> "" Then
fndTIT = Left(A, L): fndHWN = hwnd
Else
EnumChildProc = 1 'if not found...
End If
End Function
1 Attachment(s)
Re: FindWindow api problem
Re: FindWindow api problem
Code:
GetClassName hWndChild, strClass, 250
will almost certainly give you a problem as strClass is not defined and not initialised. You should define it and initialise it in a similar fashion to strBuffer (e.g. set it to 250 spaces) before calling the API.
I strongly recommend you put 'Option Explicit' as the first line of all your Forms and Modules; using that statement will cause the Compiler to warn you if any variables have not been defined and you could potentially avoid problems like this.
Re: FindWindow api problem
Thanks, problem solved after defining strClass :)