Solved

Is there a better way to convert hwnd to ProcessName?

Posted on 2008-10-08
2
421 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 86

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

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

717 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