Solved

Is there a better way to convert hwnd to ProcessName?

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

762 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now