Solved

Is there a better way to convert hwnd to ProcessName?

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

815 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

8 Experts available now in Live!

Get 1:1 Help Now