GetWindowText Function

Dug

Newcomer
Joined
Mar 31, 2009
Messages
3
Hi there,

I'm writing this application that gets the current tune from Winamp, iTunes and YouTube (video in YouTube's case!), and I had this working before, and I'm getting it working up to a point now, but what I have keeps on returning integers instead of the text.

So far I've got:

Code:
Public Function GetWindowHandle(ByVal partialTitle As String) As IntPtr
        For Each p As Process In Process.GetProcesses()
            If p.MainWindowTitle.IndexOf(partialTitle, 0, _
            StringComparison.CurrentCultureIgnoreCase) > -1 Then
                Return p.MainWindowHandle 'Found Window
            End If
        Next
        Return IntPtr.Zero 'No Match Found
    End Function


	Private Function GetYouTubeVideo() As String
		Dim hWnd As Integer
		Dim Length As Integer
        Dim hWndApp As Integer ' Window handle from GetWinHandle
        Dim title As String
        
        
        'The below statement is where you would put your partial title
        'If you want to change it to play around you can see how it works.
        hWnd = FindWindow("YouTube", Nothing)
        ' Begin search for handle
        hWndApp = GetWindowHandle(hWnd)
        
        Length = GetWindowTextLength(hWnd) + 1
        
       
        
        Return title

This isn't the whole application, but just the bits for this problem. I've been trying different things and searching google for ages now, so I'll have to give in and ask. I've got up to the point where it returned 0 if it didn't find YouTube and 46 if it did.

Would appreciate any help.

Thanks

Dug
 
Hi there,

Sorry for the long delay in replying.
The complete code I'm using so far is:

Code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA"(ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
Public Declare Function GetWindowTextLength Lib "user32" Alias  "GetWindowTextLengthA" (ByVal hWnd 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 Function GetWindowHandle (ByVal partialTitle As String) As IntPtr
		For Each p As Process in Process.GetProcesses()
			If p.MainWindowTitle.IndexOf (partialTitle, 0, _
					StringComparison.CurrentCultureIgnoreCase) > -1 then
				Return p.MainWindowHandle 'Found Window
			End If
		Next
		Return IntPtr.Zero 'No Match Found
	End Function
	
	Private Function GetYouTubeVideo() As String
		Dim hWnd As Long
		Dim hWndApp As Long
		Dim Length As Integer
		hWnd = GetWindowHandle("YouTube")
		hWndApp = FindWindow(hWnd, Nothing)
		Dim title As String = Space(Length + 1)
		Length = GetWindowTextLength(hWnd)
		GetWindowText(hWnd, title, Length + 1)
		Return title
	End Function

However I keep on getting the error "Arithmetic operation resulted in an overflow". It's probably the most annoying error I've come across in .NET. I've tried various alternate methods, but they all eventually seem to produce the same error.

Any thoughts?

Thanks

Dug
 
Try using
Visual Basic:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FindWindow( _
     ByVal lpClassName As String, _
     ByVal lpWindowName As String) As IntPtr
End Function
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function GetWindowText(ByVal hwnd As IntPtr, _
                       ByVal lpString As StringBuilder, _
                       ByVal cch As Integer) As Integer

for the declarations and see if it makes a difference.
 
Back
Top