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.
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.
The 'IndexOutOfRange' error is triggered when the process is not active.
●The function in action:


●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
●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 function in action:
Code:
MsgBox(GetProcId("Notepad"))







