Solved

Is there a better way to convert hwnd to ProcessName?

Posted on 2008-10-08
2
420 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

730 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