VB.NET delegates for callback.
I am required to secure the handle of a window and read all its child windows. The only way I think is to use EnumChildWindows API of win32. The API implements a callback function. Since my application is VB.NET, I am trying to implement the callback function as a delegate. However I am getting the following error message on the line where the EnumChildWindows API is called.
An unhandled exception of type 'System.NullReferenceException' occurred in WinAPI.exe
Additional information: Object reference not set to an instance of an object.
I tried handling this exception but with no positive results. I guess my implementation of delegates is incorrect. Please guide me.
[COLOR=dark-blue]
Option Strict Off
Imports System
Imports System.Runtime.InteropServices
Imports Microsoft.VisualBasic
Public Class Form1
Inherits System.Windows.Forms.Form
<DllImport("user32.dll")> Public Shared Function _
EnumChildWindows(ByVal hWndParent As Long, ByRef lpEnumFunc As EnumChildGetValueDelegate, ByVal lParam As Long) As Boolean
End Function
Delegate Function EnumChildGetValueDelegate(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Public Function EnumChildGetValue(ByVal hWnd As Long, ByVal lParam As Long) As Boolean
Dim slength As Long
Dim retval As Long
Dim iRet As Integer = 0
Dim CurClass As String = ""
Dim strGetValue As String = ""
Dim lpClassName As String
Dim strSetValueNew As String
Const WM_GETTEXTLENGTH = &HE
Const WM_GETTEXT = &HD
MsgBox("HELLO!")
iRet = Win32.GetClassName(hWnd, lpClassName, 50)
If iRet <> 0 Then
lpClassName = Trim$(lpClassName)
If InStr(lpClassName, Chr(0)) > 0 Then _
CurClass = Microsoft.VisualBasic.Left(lpClassName, InStr(lpClassName, Chr(0)) - 1)
If CurClass = "Edit" Then
slength = Win32.SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1
strGetValue = Space(slength)
retval = Win32.SendMessage(hWnd, WM_GETTEXT, slength, strGetValue)
strGetValue = Microsoft.VisualBasic.Left(strGetValue, retval)
strSetValueNew = strGetValue
Return False
Exit Function
End If
Return True
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim hWnd As Integer
Const WM_GETTEXTLENGTH = &HE
Const WM_CLOSE = &H10
Const WM_GETTEXT = &HD
Const WM_SETTEXT = &HC
Dim slength As Long
Dim retval As Long
Dim strGetValue As String = " "
Dim objNewDelegate As EnumChildGetValueDelegate
objNewDelegate = New EnumChildGetValueDelegate(AddressOf EnumChildGetValue)
hWnd = Win32.FindWindow(vbNullString, "Untitled - Notepad")
slength = Win32.SendMessage(hWnd, WM_SETTEXT, 0, "Daljeet")
slength = Win32.SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0) + 1
retval = Win32.SendMessage(hWnd, WM_GETTEXT, 2000, strGetValue)
If hWnd = 0 Then
MsgBox("Window Not Open")
Else
MsgBox(Win32.GetClassName(hWnd, vbNullString, 50))
MsgBox("Hi: " & Win32.GetWindow(hWnd, 5))
MsgBox(Win32.GetClassName(hWnd, vbNullString, 50))
End If
End Sub
End Class
[/COLOR]