Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WinAPI in VB (GetWindowText, GetWindowTextLength, GetClassName)

Status
Not open for further replies.

dllCoRupT626

Programmer
Oct 15, 2002
12
0
0
US
dllCoRupT626 (Programmer) Feb 12, 2003
Right to the point... I'm trying to List all of the Top LvL Window's, Everything works fine until it try's to get the ClassName of the Window and the Window Text (Title Bar).

This is all in a module:

'Declare's


Public Declare Function GetClassName Lib "user32.dll" Alias "GetClassNameA" (ByVal hWnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Integer

Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal nMaxCount As Long) As Integer

Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" (ByVal hWnd As Long) As Integer

Public Declare Function EnumWindows Lib "user32.dll" (ByVal lpEnumFunc As Long, ByVal lParam As Integer) As Boolean

'Function's


Public Function fGetTopLvLWindowsEnumProc(hWnd As Long, ByVal lParam As Long) As Boolean
Dim tmpWinLen, tmpClassLen As Long
On Error GoTo error_Handler

If topWindowNames(1) <> &quot;¬ÑÉÑáѦÖÑä¬&quot; Then
ReDim Preserve topWindowNames((UBound(topWindowNames) + 1))
End If
If topWindowClassNames(1) <> &quot;¬ÑÉÑáѦÖÑä¬&quot; Then
ReDim Preserve topWindowClassNames((UBound(topWindowClassNames) + 1))
End If
If topWindowHandles(1) <> 0 Then
ReDim Preserve topWindowHandles((UBound(topWindowHandles) + 1))
End If

tmpWinLen = GetWindowTextLength(hWnd)
Call GetWindowText(hWnd, topWindowNames(UBound(topWindowNames)), tmpWinLen + 1)
topWindowHandles(UBound(topWindowHandles)) = hWnd
tmpClassLen = GetClassName(hWnd, topWindowClassNames(UBound(topWindowClassNames)), 151)

If topWindowNames(UBound(topWindowNames)) = &quot;&quot; Then topWindowNames(UBound(topWindowNames)) = &quot;No-Name&quot;
frmMain.View1.ListItems.Add , , hWnd
frmMain.View1.ListItems.Item(I).SubItems(1) = topWindowClassNames(UBound(topWindowClassNames))
frmMain.View1.ListItems.Item(I).SubItems(2) = topWindowNames(UBound(topWindowNames))
I = I + 1

fGetTopLvLWindowsEnumProc = True 'Tell's EnumWindows to continue
Exit Function

error_Handler:
fGetTopLvLWindowsEnumProc = False 'Tell's EnumWindows to stop
End Function

Public Function fGetTopLvLWindows() As Boolean
On Error GoTo error_Handler

fGetTopLvLWindows = True
ReDim topWindowNames(1) 'Clear's the array
ReDim topWindowClassNames(1) 'Clear's the array
ReDim topWindowHandles(1) 'Clear's the array
topWindowNames(1) = &quot;¬ÑÉÑáѦÖÑä¬&quot;
topWindowClassNames(1) = &quot;¬ÑÉÑáѦÖÑä¬&quot;
topWindowHandles(1) = 0

I = 1
frmMain.View1.ColumnHeaders.Add , , &quot;Handle&quot;, ((frmMain.View1.Width / 3) - 110)
frmMain.View1.ColumnHeaders.Add , , &quot;Class Name&quot;, ((frmMain.View1.Width / 3) - 110)
frmMain.View1.ColumnHeaders.Add , , &quot;Window&quot;, ((frmMain.View1.Width / 3) - 110)

EnumWindows AddressOf fGetTopLvLWindowsEnumProc, False

Exit Function

error_Handler:
fGetTopLvLWindows = False
End Function


topWindowHandles, topWindowNames, and topWindowClassNames are all dimed at the start of the program

'
Public topWindowHandles() As Long
Public topWindowNames() As String
Public topWindowClassNames() As String
'

fGetTopLvLWindows() is called at Form_Load

Everything actually work's but GetClassName put's NULL in topWindowClassNames(UBound(topWindowClassNames)) even though i put nMaxCount to 151, GetWindowText put's NULL in topWindowNames(UBound(topWindowNames)) to, and GetWindowTextLength Return's 0.

That never change's :(
And i know the Handle to the window is right cause i checked it with another program.

That worked last nite, but Today when i ran it, when it called fGetTopLvLWindows() it gave me an error:
&quot; The instruction at &quot;0x0fc0106a&quot; referenced memory at &quot;0x003e01d4&quot;. The memory could not be &quot;read&quot;. &quot;

Someone help Please.
 
Sorry it's in VB but it use's WinAPI so i figured i would post it here to.

I've only used this forum like 3 times now.

Sorry again :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top