Solved

Is there a better way to convert hwnd to ProcessName?

Posted on 2008-10-08
2
419 Views
Last Modified: 2012-05-05
it works but im getting a warning.

processLB says "Unused local variable"  

processLB.GetProcessById says: " Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

GetWindowThreadProcessId(hwnd, ProcessID)

        Dim processLB As Process
        Dim tempProc As Process = processLB.GetProcessById(ProcessID)

        Dim processName As String = tempProc.ProcessName
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Lst = ListBox1      '--------> ADD THIS LINE
        EnumWindows(AddressOf EnumWindowsCallBack, 0)
    End Sub
End Class
 
 
Module Module1
    Private Declare Auto Function GetWindowThreadProcessId Lib "user32.dll" (ByVal hWnd As IntPtr, ByRef ProcessID As Integer) As Integer
 
    Public Lst As ListBox
 
    Public Delegate Function EnumWindowsProc(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
 
    Public Declare Function EnumWindows Lib "user32.dll" _
            (ByVal lpEnumFunc As EnumWindowsProc, ByVal lParam As Int32) As Int32
 
    Public Declare Function GetWindowTextLength Lib "user32.dll" Alias "GetWindowTextLengthA" _
        (ByVal hwnd As IntPtr) As Int32
 
    Public Declare Function GetWindowText Lib "user32.dll" Alias "GetWindowTextA" _
        (ByVal hwnd As IntPtr, ByVal lpString As String, ByVal cch As Int32) As Int32
 
    'Callback function to enum windows
    Public Function EnumWindowsCallBack(ByVal hwnd As IntPtr, ByVal lParam As Int32) As Int32
        Dim sSave As String
        Dim ProcessID As Long
 
        GetWindowThreadProcessId(hwnd, ProcessID)
 
        Dim processLB As Process
        Dim tempProc As Process = processLB.GetProcessById(ProcessID)
 
        Dim processName As String = tempProc.ProcessName
 
        If (processName = "notepad") Then
 
 
            'Get the windowtext length
            sSave = Space(GetWindowTextLength(hwnd) + 1)
 
            'get the window text
            GetWindowText(hwnd, sSave, Len(sSave))
 
            'remove the last Chr(0)
            sSave = Microsoft.VisualBasic.Left(sSave, Len(sSave) - 1)
 
            'Error below: Reference to a non-shared member requires an Object Reference
            Lst.Items.Add(sSave)
            'Lst.Items.Add(processName)
 
 
            If sSave.Trim <> "" Then
                Debug.WriteLine(sSave)
            End If
        End If
        Return 1 'continue enumeration        
    End Function
 
End Module

Open in new window

0
Comment
Question by:vaultworld
2 Comments
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 22675127
Just change this:

        Dim processLB As Process
        Dim tempProc As Process = processLB.GetProcessById(ProcessID)

To:

        Dim tempProc As Process = Process.GetProcessById(ProcessID)
0
 
LVL 1

Author Closing Comment

by:vaultworld
ID: 31504500
you the best!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question