I am using the win32api to get a list of all window titles. I need the list to be refreshed every few seconds and thats the problem: The memory usage of the app is increasing every time I call EnumWindowsCallback.
Here is a working demo: Run the code and see in the task manager how the memory usage of this app increases. Press RETURN to call the function again.
Imports System.Runtime.InteropServices
Imports System.Text
Class Example
Shared Sub Main()
For a = 1 To 5
Console.WriteLine("--------- (RUN NR " & a & ") PRESS RETURN")
Console.ReadLine()
Win32API.EnumWindowsDllImport(New Win32API.EnumWindowsCallback(AddressOf FillActiveWindowsList), 0)
Next
Console.WriteLine("READY!")
Console.ReadLine()
End Sub
Shared Function FillActiveWindowsList(ByVal _hWnd As IntPtr, ByVal lParam As IntPtr) As Boolean
Dim windowText As New StringBuilder(255)
Win32API.GetWindowText(_hWnd, windowText, 255)
Console.WriteLine(windowText)
Return True
End Function
End Class
Public Class Win32API
Public Delegate Function EnumWindowsCallback(hWnd As Integer, lParam As Integer) As Boolean
<DllImport("user32.dll", EntryPoint:="EnumWindows", SetLastError:=True, CharSet:=CharSet.Ansi, ExactSpelling:=True, CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function EnumWindowsDllImport(callback As EnumWindowsCallback, lParam As Integer) As Integer
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowTextA")> _
Public Shared Sub GetWindowText(hWnd As Integer, lpstring As StringBuilder, nMaxCount As Integer)
End Sub
End Class
at the beginning my EXE is about 3MB big, but after 5 function calls it is increased to 6 MB (and stays there) The problem is that it causes some crashes in my main application if the memory usage gets over 6 MB. The example above should not crash.
Do you see a problem here? Maybe there is a problem with the data types? or maybe it is a bug? Any help would be appreciated.