richtige Verwendung von WinAPI-Methoden und Delegates

  • VB.NET

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von Panter.

    richtige Verwendung von WinAPI-Methoden und Delegates

    ausgelagert aus Wie funktionieren bestimmte API's richtig? ~VaporiZed

    Hallo zusammen

    Ich muss leider zugeben, dass für mich das Problem wohl nicht Zugriff auf die API ist, sondern eben diese Delegate Geschichte. Das hier ist wohl ein gutes Beispiel, das zu erlernen, für mich ist das momentan leider zu viel.

    Ich habe mir mal überlegt, möglichst viel von diesem Delgate zu entfernen. Ich bin leider Anfänger, habe nun ewigs gebraucht und der Code ist vielleicht falsch. In den nächsten Tagen werde ich mal schauen, wie ich von diesem vereinfachten Code wieder auf den ursprünglichen Code zurückkomme. Ich muss das mal verdauen.

    Ich habe den Code für mich geschrieben, damit ich das erlernen kann. Falls jemand Fehler entdeckt, bitte melden - ich möchte etwas lernen. Aufgefallen ist mir noch, dass eben oft der MainWindowsHandle = 0 ist - das habe ich abgefangen, damit es nicht so viele Ausgaben gibt.

    Ich habe mal meinen Code hier eingestellt.

    VB.NET-Quellcode

    1. Imports System.Diagnostics
    2. Imports Microsoft.VisualBasic
    3. Public Class Form2
    4. Dim dt As New DataTable
    5. Dim dv As New DataView(dt)
    6. Dim Parent_Handle_str As String
    7. Dim Handle_0 As Boolean
    8. Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    9. With dt.Columns
    10. .Add("Handle", Type.GetType("System.Int64"))
    11. .Add("Handle_child", Type.GetType("System.Int64"))
    12. .Add("lparam", Type.GetType("System.Int64"))
    13. .Add("MainWindowTitle", Type.GetType("System.String"))
    14. .Add("ProcessID", Type.GetType("System.String"))
    15. .Add("HandleCount", Type.GetType("System.String"))
    16. .Add("MainModule", Type.GetType("System.String"))
    17. .Add("ProcessName", Type.GetType("System.String"))
    18. End With
    19. DataGridView1.DataSource = dv
    20. ' alle Prozesse durchlaufen
    21. Handle_0 = False
    22. For Each oProcess As Process In Process.GetProcesses
    23. ' Prozess-Infos ermitteln und im ListView anzeigen
    24. Dim windowHandle As IntPtr = oProcess.MainWindowHandle
    25. Dim row As DataRow = dt.NewRow()
    26. '' 1. Eintrag
    27. Try
    28. row!Handle = windowHandle.ToString
    29. Catch
    30. row!Handle = ""
    31. End Try
    32. If Len(row!Handle.ToString) > 0 Then
    33. '' 2. Eintrag
    34. row!MainWindowTitle = GetActiveWindowText(windowHandle) 'oProcess.MainWindowTitle.ToString
    35. '3. Eintrag
    36. Try
    37. row!ProcessID = CInt(oProcess.Id.ToString)
    38. Catch
    39. row!ProcessID = ""
    40. End Try
    41. '4. Êintrag
    42. Try
    43. row!HandleCount = oProcess.HandleCount.ToString
    44. Catch
    45. row!HandleCount = ""
    46. End Try
    47. Try
    48. row!MainModule = oProcess.MainModule.ToString
    49. Catch
    50. row!MainModule = ""
    51. End Try
    52. Try
    53. row!ProcessName = oProcess.ProcessName.ToString
    54. Catch
    55. row!ProcessName = ""
    56. End Try
    57. dt.Rows.Add(row)
    58. Parent_Handle_str = row!Handle.ToString
    59. 'Nur 1 x Handle 0..
    60. If windowHandle.ToInt64 = 0 Then
    61. If Handle_0 Then
    62. Else
    63. Handle_0 = True
    64. GetChild(windowHandle)
    65. End If
    66. Else
    67. GetChild(windowHandle)
    68. End If
    69. End If
    70. Next
    71. End Sub
    72. Public Function Enumerate(hWnd As IntPtr, lParam As IntPtr) As Boolean
    73. Dim row As DataRow = dt.NewRow()
    74. row!Handle = Parent_Handle_str
    75. row!Handle_child = hWnd.ToInt64
    76. row!MainWindowTitle = GetActiveWindowText(hWnd)
    77. dt.Rows.Add(row)
    78. Return True
    79. End Function
    80. Public Function GetChild(hwnd As IntPtr) As Boolean
    81. Dim x As Boolean
    82. x = EnumChildWindows(hwnd, AddressOf Enumerate, CType(0, IntPtr))
    83. Return True
    84. End Function
    85. End Class



    und hier noch mein Modul (ich habe Modul gewählt und nicht Klasse - leider kenne ich den Unterschied nicht)

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Imports System.Text
    3. Module Module1
    4. <DllImport("user32.dll", SetLastError:=True)>
    5. Private Function GetForegroundWindow() As IntPtr
    6. End Function
    7. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    8. Private Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer
    9. End Function
    10. <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
    11. Private Function GetWindowTextLength(ByVal hwnd As IntPtr) As Integer
    12. End Function
    13. 'no specific Charset APIs
    14. <DllImport("user32.dll", SetLastError:=True)>
    15. Public Function EnumChildWindows(hWndParent As IntPtr, lpEnumFunc As EnumWindowsProc, lParam As IntPtr) As Boolean
    16. End Function
    17. Public Delegate Function EnumWindowsProc(hWnd As IntPtr, lParam As IntPtr) As Boolean
    18. Public Function GetActiveWindowText(hWnd As IntPtr) As String
    19. Dim length As Integer ', hWnd As IntPtr = GetForegroundWindow()
    20. If hWnd.ToInt32 = 0 Then
    21. Return Nothing
    22. End If
    23. length = GetWindowTextLength(hWnd)
    24. If length = 0 Then
    25. Return Nothing
    26. End If
    27. Dim sb As New System.Text.StringBuilder("", length)
    28. GetWindowText(hWnd, sb, sb.Capacity + 1)
    29. Return sb.ToString()
    30. End Function
    31. End Module

    Dieser Beitrag wurde bereits 4 mal editiert, zuletzt von „VaporiZed“ ()

    @Panter Was soll dieser Code tun?
    Prozeduren und Variablen eines Moduls können von überall aus dem Programm heraus zugreifen.

    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!