C#

Dieses Thema im Forum "Programmierung & Entwicklung" wurde erstellt von jimboc, 12. Juli 2006 .

  1. 12. Juli 2006
    Hi

    Weiß jemand wie man in C# mit der HWND die dazugehörige exe herausbekommt?

    danke schon mal
     
  2. 15. Juli 2006
    Google? ("hwnd exe")



    EXE-Name anhand eines Fenster-Handles ermitteln
    VB-Versionen: VB4, VB5, VB6
    Betriebssystem: Win9x, WinNT, Win2000, WinME
    Autor: Dieter Otter Homepage: http://www.tools4vb.de/
    Datum: 18.03.2002 Download: -
    Sprache: deutsch Views: 9837


    Beschreibung

    Wie oft kennt man das Handle eines Fenster, weiss aber nicht, zu welcher Anwendung das Fenster gehört. Mit unserem nachfolgendem Tipp stellen wir Ihnen genau für dieses Problem eine entsprechende Lösung vor.

    Die Funktion GetEXEFromHWND ermittelt den Dateinamen der Anwendung eines übergebenen Fensterhandles. Hierzu bedarf allerlei API-Deklarationen - aber sehen Sie selbst.

    Fügen Sie nachfolgenden Code in ein neues Modul ein:

    Option Explicit

    ' alle benötigten API-Deklarationen
    Private Declare Function GetWindowThreadProcessId Lib _
    "user32" (ByVal hWnd As Long, _
    lpdwProcessId As Long) As Long

    Private Declare Function CreateToolhelpSnapshot Lib _
    "Kernel32" Alias "CreateToolhelp32Snapshot" ( _
    ByVal lFlgas As Long, ByVal lProcessID As Long) _
    As Long

    Private Declare Function ProcessFirst Lib "Kernel32" _
    Alias "Process32First" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long

    Private Declare Function ProcessNext Lib "Kernel32" _
    Alias "Process32Next" (ByVal hSnapshot As Long, _
    uProcess As PROCESSENTRY32) As Long

    Private Declare Sub CloseHandle Lib "Kernel32" ( _
    ByVal hPass As Long)

    Private Const TH32CS_SNAPPROCESS As Long = 2&
    Private Const MAX_PATH As Long = 260

    Private Type PROCESSENTRY32
    dwSize As Long
    cntUsage As Long
    th32ProcessID As Long
    th32DefaultHeapID As Long
    th32ModuleID As Long
    cntThreads As Long
    th32ParentProcessID As Long
    pcPriClassBase As Long
    dwflags As Long
    szexeFile As String * MAX_PATH
    End Type

    ' Ermittelt den Namen der EXE-Datei eines Fenster
    ' anhand des übergebenen Fensterhandles (hWnd)
    Public Function GetExeFromHWND(hWnd As Long) As String
    Dim lThreadID As Long
    Dim lProcessID As Long
    Dim lSnapshot As Long
    Dim uProcess As PROCESSENTRY32
    Dim lProcessFound As Long
    Dim i As Integer
    Dim sEXEName As String

    ' Prozess-ID ermitteln
    lThreadID = GetWindowThreadProcessId(hWnd, lProcessID)

    ' Prüfen, ob ID's gültig
    If lThreadID <> 0 And lProcessID <> 0 Then
    ' "Snapshot" des aktuellen Prozess ermitteln
    lSnapshot = CreateToolhelpSnapshot( _
    TH32CS_SNAPPROCESS, 0&)

    ' Fehler?
    If lSnapshot = -1 Then Exit Function

    ' uProcess-Struktur füllen
    With uProcess
    .dwSize = Len(uProcess)

    ' alle aktuellen Prozesse durchlaufen
    lProcessFound = ProcessFirst(lSnapshot, uProcess)
    Do While lProcessFound
    If .th32ProcessID = lProcessID Then
    ' ProzessID gefunden
    ' jetzt EXE-Name ermitteln
    If InStr(.szexeFile, Chr$(0)) > 0 Then
    sEXEName = Left$(.szexeFile, _
    InStr(.szexeFile, Chr$(0)) - 1)
    End If
    Exit Do
    Else
    ' ...weitersuchen...
    lProcessFound = ProcessNext(lSnapshot, _
    uProcess)
    End If
    Loop
    End With

    ' Handle schliessen
    Call CloseHandle(lSnapshot)
    End If

    GetExeFromHWND = sEXEName
    End Function

    Erstellen Sie eine Form und plazieren Sie darauf einen CommandButton. Beim Klicken wird dann der EXEName der aktuellen Anwendung ermittelt:

    Private Sub Command1_Click()
    MsgBox GetExeFromHWND(Me.hWnd)
    End Sub

    Wenn Sie das Beispiel innerhalb der VB-IDE ausprobieren, sollte als EXEName VB6.EXE angezeigt werden (bzw. VB5.EXE unter Visual Basic 5). Kompilieren Sie Ihre Anwendung hingegen und starten dann die kompilierte EXE-Datei, so erscheint nicht mehr VB6.EXE, sondern vielmehr der EXEName Ihrer Anwendung.

    Erweitertes Beispiel
    Plazieren Sie auf die Form einen Timer tmrTimer, sowei zwei Label-Controls lblHandle und lblEXEName. In einem sehr kurzen Intervall soll quasi ständig das Handle des Fensters ermitteln, über welchem sich die Maus gerade befindet. Und anhand des Fensterhandles wird dann der entsprechende EXE-Name der Anwendung ermittelt und angezeigt.

    Fügen Sie hierzu folgenden Code in den Code-Teil der Form ein:

    Option Explicit

    ' zunächst die benötigten API-Deklarationen
    Private Type POINTAPI
    x As Long
    y As Long
    End Type

    Private Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long

    Private Declare Function WindowFromPoint Lib "user32" _
    (ByVal xPoint As Long, ByVal yPoint As Long) As Long

    Private Sub Form_Load()
    ' Timer initialisieren
    tmrTimer.Interval = 10
    End Sub

    Private Sub tmrTimer_Timer()
    Dim oPoint As POINTAPI
    Dim hWnd As Long

    ' Mausposition ermitteln
    If GetCursorPos(oPoint) <> 0 Then
    ' Fensterhandle des Controls, auf dem sich
    ' der Mauszeiger befindet
    hWnd = WindowFromPoint(oPoint.x, oPoint.y)

    ' Handle anzeigen
    lblHandle.Caption = hWnd

    ' EXE-Name
    lblEXEName.Caption = GetExeFromHWND(hWnd)
    End If
    End Sub
     
  3. 15. Juli 2006
    Wer lesen kann ich klar im Vorteil^^
    jimboc wollte für C#. Du hast ihm nen VB-Code gepostet. Da wird er reichtlich wenig mit anfangen können....
     
  4. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.