LightbulbGetting a process's ID using API

Posts 1–15 of 17 · Page 1 of 2
Getting a process's ID using API
Possibly the worst way to do it, but my little contributon.



●First of all you're going to import the function we will use.
Code:
    <DllImport("Kernel32.DLL")> _
    Private Shared Function GetProcessId(ByVal Process As IntPtr) As Integer
    End Function
The parameter 'Process' is the handle to the process.

●Now lets create a little function that will return the process's ID if it is active, else 0.
Code:
    Private Function GetProcId(ByVal ProcessName As String) As Integer
        Try
            Dim ProcessHandle As IntPtr = Process.GetProcessesByName(ProcessName)(0).Handle
            Dim ProcessID As Integer = GetProcessId(ProcessHandle)
            Return ProcessID
        Catch ex As IndexOutOfRangeException
            Return 0
        End Try
    End Function
The 'IndexOutOfRange' error is triggered when the process is not active.

●The function in action:
Code:
MsgBox(GetProcId("Notepad"))
Nice but There is any way for get the ico of a process?
Quote Originally Posted by jonnyHS View Post
Nice but There is any way for get the ico of a process?
Quote Originally Posted by Sam... View Post
Icon.ExtractAssociatedIcon Method (System.Drawing)
Indeed Sam, but you still need the path of the executable or library.
Code:
Dim pFileName As String = Process.GetProcessByName("NotePad")(0).MainModule.FileName))
Dim pIcon As Icon = Icon.ExtractAssociatedIcon(pFileName)
You cant assign icons to image properties so you should convert it.
Code:
Dim pImage As Image = pIcon.ToBitmap
I did it but only for change de App Ico
Code:
        Dim hicon As IntPtr = ExtractAssociatedIcon(Handle, "C:\Program Files\Internet Explorer\iexplore.exe", 1)
        Dim ico As Icon = Icon.FromHandle(hicon)

        Me.Icon = ico
How can i put with process?
Quote Originally Posted by jonnyHS View Post
I did it but only for change de App Ico
Code:
        Dim hicon As IntPtr = ExtractAssociatedIcon(Handle, "C:\Program Files\Internet Explorer\iexplore.exe", 1)
        Dim ico As Icon = Icon.FromHandle(hicon)

        Me.Icon = ico
How can i put with process?
Save the icon then import it
Quote Originally Posted by jonnyHS View Post
I did it but only for change de App Ico
Code:
        Dim hicon As IntPtr = ExtractAssociatedIcon(Handle, "C:\Program Files\Internet Explorer\iexplore.exe", 1)
        Dim ico As Icon = Icon.FromHandle(hicon)

        Me.Icon = ico
How can i put with process?

I don't think it would be possible to change the icon while its running.
But, as geometrical said, try saving it to something like the temp folder, set the icon, then remove from temp.

...Why would you want to do that anyway?
Quote Originally Posted by jonnyHS View Post
I did it but only for change de App Ico
Code:
        Dim hicon As IntPtr = ExtractAssociatedIcon(Handle, "C:\Program Files\Internet Explorer\iexplore.exe", 1)
        Dim ico As Icon = Icon.FromHandle(hicon)

        Me.Icon = ico
How can i put with process?
If I understand you correctly...

The first/'main' module of a "Process" is the executable its self...

_proc.Modules(0).FileName '' full filepath to executable
or
_proc.MainModule.FileName '' full filepath ...


Note: There is a problem if your app is 32 bit and the 'target process' is 64 bit ..your process won't be able to access any 64 bit process.



else
what do you mean "put with process"?

sorry for double post.
Quote Originally Posted by abuckau907 View Post
 
Pic
Heh... I guess it does work.
I mean use it with a listview with process :P (add the process in a column and add le ico in other)

PD: i will test it in 1h have things to do

Edit: i dont have access to all de process even when use it as ADM, like u said
Quote Originally Posted by Geometrical View Post
Possibly the worst way to do it, but my little contributon. ...

Private Function GetProcId(ByVal ProcessName As String) As Integer
Try
Dim ProcessHandle As IntPtr = Process.GetProcessesByName(ProcessName)(0).Handle
Dim ProcessID As Integer = GetProcessId(ProcessHandle)
Return ProcessID
Catch ex As IndexOutOfRangeException
Return 0
End Try
End Function
The 'IndexOutOfRange' error is triggered when the process is not active.

●The function in action:
Code:
MsgBox(GetProcId("Notepad"))
Since you're using the Process class from .net, you don't need to declare the API function.

Process.GetProcesByName() returns an array of 'Processes' -- you currently get the .Handle() of the process and store it as an IntPtr: you could also use the .Id() property of the Process class instead of the api function. It's one more function to declare/worry about, but works equally well I guess. Since you're already using the Process class, might as well take advantage of it - not exactly a huge deal.

 
code

Code:
Public Class Form1

    ''// No Windows API Declaration

    Public Function TestSub(ByVal procName As String) As Int32
        Dim _proc As System.Diagnostics.Process = Process.GetProcessesByName(procName)(0)

        Return _proc.Id() '' with this logic, why bother creating another temporary..simply return .ByName(0).Id...TODO: handle process not found.
    End Function




    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Const targetProcName As String = "notepad"

        Dim _newID As Int32 = TestSub(targetProcName)
        MsgBox("Target App: " & targetProcName & Environment.NewLine _
             & "Process ID: " & _newID.ToString())
    End Sub
End Class

And you shouldn't have to catch an exception like that...check if the function actually returned anything before using the 'array of processes.'
Code:
    Public Function TestFunc(ByVal procName As String) As Int32
        Dim _procs() As System.Diagnostics.Process = Process.GetProcessesByName(procName)

        If _procs.Length = 0 Then
            ''Process not found
            Return 0
        Else
            ''Process found
            Return _procs(0).Id()
        End If
    End Function
Quote Originally Posted by abuckau907 View Post
Since you're using the Process class from .net, you don't need to declare the API function.
I said it's using the API, everyone already knows how to use the process class.

Not sure if read thread, or stupid...
@Geometrical Yes I read those..5 sentences (?). Ok, sure .

-------------------------------
Quote Originally Posted by jonnyHS View Post
I mean use it with a listview with process :P (add the process in a column and add le ico in other)

PD: i will test it in 1h have things to do

Edit: i dont have access to all de process even when use it as ADM, like u said
Which processes ? Please be more specific... are they 'system' processes? Windows doesn't give you full control over EVERY process.
Code:
Private Sub Listview()
        Dim proceso As Process
        Try
            ps = Process.GetProcesses()
            Listview1.BeginUpdate()
            Listview1.Clear()
            Listview1.Columns.Add("Name", 100, HorizontalAlignment.Left)
            Listview1.Columns.Add("ID", 60, HorizontalAlignment.Left)
            Listview1.Columns.Add("Priority", 60, HorizontalAlignment.Right)
            Listview1.Columns.Add("Memory", 100, HorizontalAlignment.Right)
            Dim p As Process
            For Each p In proceso
                Dim lvi As ListViewItem = New ListViewItem()
                lvi.Text = p.ProcessName
                lvi.SubItems.Add(p.Id.ToString())
                lvi.SubItems.Add(p.BasePriority.ToString())
                lvi.SubItems.Add(p.WorkingSet64.ToString())
                Listview1.Items.Add(lvi)

            Next p
            Listview1.EndUpdate()
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try

       
    End Sub
I want to create something like a task manager, but only for loading the process in to my cheat and i only need the icons and other function more to finish the project
Quote Originally Posted by jonnyHS View Post
Code:
Private Sub Listview()
        Dim proceso As Process
        Try
            ps = Process.GetProcesses()
            Listview1.BeginUpdate()
            Listview1.Clear()
            Listview1.Columns.Add("Name", 100, HorizontalAlignment.Left)
            Listview1.Columns.Add("ID", 60, HorizontalAlignment.Left)
            Listview1.Columns.Add("Priority", 60, HorizontalAlignment.Right)
            Listview1.Columns.Add("Memory", 100, HorizontalAlignment.Right)
            Dim p As Process
            For Each p In proceso
                Dim lvi As ListViewItem = New ListViewItem()
                lvi.Text = p.ProcessName
                lvi.SubItems.Add(p.Id.ToString())
                lvi.SubItems.Add(p.BasePriority.ToString())
                lvi.SubItems.Add(p.WorkingSet64.ToString())
                Listview1.Items.Add(lvi)

            Next p
            Listview1.EndUpdate()
        Catch e As Exception
            MessageBox.Show(e.Message)
        End Try

       
    End Sub
I want to create something like a task manager, but only for loading the process in to my cheat and i only need the icons and other function more to finish the project
So now all you need is to add the images (..when available) to the listview? ...Google that? You have everything else you need in the posts above.
Posts 1–15 of 17 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

None

Need help?