LightbulbGetting a process's ID using API

Posts 16–17 of 17 · Page 2 of 2
i have an idea of how to do it but it said "you dont have the necesary access" and i even use it like ADM

Edited: i leave that and i found an information http://msdn.microsoft.com/en-us/libr...imagelist.aspx

So i did this from that but i have 2 problems:

1-they always repeat themselves 2 times (the items/process)
2- i dont have any idea for put all de incons in the column i only didi it with simples pictures :P

Code:
    Private Sub Listview()
       
        Dim ps() As Process
        Try
            ps = Process.GetProcesses()
            ProcessList.BeginUpdate()
            ProcessList.Clear()
            ProcessList.Columns.Add("Name", 100, HorizontalAlignment.Left)
            ProcessList.Columns.Add("ID", 60, HorizontalAlignment.Left)
            ProcessList.Columns.Add("Priority", 60, HorizontalAlignment.Right)
            ProcessList.Columns.Add("Memory", 100, HorizontalAlignment.Right)
            Dim p As Process
            For Each p In ps

                Dim item As New ListViewItem(p.ProcessName, 0)
                item.SubItems.Add(p.Id.ToString())
                item.SubItems.Add(p.BasePriority.ToString())
                item.SubItems.Add(p.WorkingSet64.ToString())
                ProcessList.Items.AddRange(New ListViewItem() {item})

                ' Create two ImageList objects. 
                Dim imageListSmall As New ImageList()
                Dim imageListLarge As New ImageList()

                ' Initialize the ImageList objects with bitmaps.
                imageListSmall.Images.Add(Bitmap.FromFile("C:\1.png"))
                imageListLarge.Images.Add(Bitmap.FromFile("C:\3.png"))



                'Assign the ImageList objects to the ListView.
                ProcessList.LargeImageList = imageListLarge
                ProcessList.SmallImageList = imageListSmall

                ProcessList.EndUpdate()

            Next p

        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try


    End Sub
help?
try running the program as admin? And/or running the IDE as administrator.

Running Visual Studio as normal user (notice error list)

 
code from image

Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim _allProcs() As Process = Process.GetProcesses() '' a list of ALL running processes
        Dim _outString As New System.Text.StringBuilder
        Dim _outError As New System.Text.StringBuilder

        _outError.AppendLine("ERROR: ")
        For Each _proc As Process In _allProcs
            Try
                _outString.AppendLine(_proc.ProcessName & "  id: " & _proc.Id.ToString() & "   file: " & _proc.MainModule.FileName)
            Catch ex As Exception
                _outError.AppendLine(_proc.ProcessName & "->id: " & _proc.Id())
            End Try

        Next
        MessageBox.Show(_outString.ToString() & Environment.NewLine & _outError.ToString())
    End Sub

Running the IDE as Admin (you are able to access more of the programs).


Let me know if that helps. Use a Try/Catch block --> If an exception is thrown, it's probably because "Access is denied". If it's a system process, simply ignore it. Use code similar to mine and first see which processes you can't access, and if you can access your game. If you absolutely must get access to the process, try manually calling OpenProcess() and see if you can get a valid handle / with what access rights. When I ran the above code there were only 3 processes which I couldn't access as Admin: "System", "Idle" and some third one.

edited: I've never used a listview. Good Luck. --you were shown above how to use process.mainmodule.filename to get the icon, and the icon class has a convenient ToBitmap() function. I'm not sure what format/class the listview expects, but I think you have what you need.

As far as duplicates..create a new List(Of String) (or another 'collection' type that grows automatically), and each time you add a process to the list, also record its .ProcessName and .Id. Then, only add a process to the listView if that name/id haven't already been added to the list. Careful as some programs can/will have several copies running (think multiboxing). Have you every worked with arrays or other containers?

edit2:
Dim imageListSmall As New ImageList()
Dim imageListLarge As New ImageList()

' Initialize the ImageList objects with bitmaps.
imageListSmall.Images.Add(Bitmap.FromFile("C:\1.pn g"))
imageListLarge.Images.Add(Bitmap.FromFile("C:\3.pn g"))

'Assign the ImageList objects to the ListView.
ProcessList.LargeImageList = imageListLarge
ProcessList.SmallImageList = imageListSmall
So you're 1) Createing a NEW ImageList, then 2) Adding a BITMAP to it and 3) Assigning the ImageList to the listview. We showed you how to get an ICON object from the filepath, and then you can convert that to a bitmap by calling .ToBitmap() on it. Basically the same as you're already doing (adding a bitmap to the imagelist). That should work the same?

But there is a problem with your logic...it will erase any icons that are already in the list. ? You create a NEW ImageList and then add 2 images to it. Any images that were in the old list don't get saved. I'm sure you could set your imageList == ListView.Images to get a reference to that list, and just add to it (?) I'm not sure, haven't worked with listviews, sorry. Or use a For loop and add all the process icons to the imagelist in one go.
Posts 16–17 of 17 · Page 2 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Talk with us