שתף באמצעות


VBA Getting the PID of Another Application

Question

Thursday, May 1, 2014 9:41 PM

Hello,

Apologies I'm still learning a lot about VB.NET using Visual Studio 2013 Express.  I'm writing a small application to send keystrokes to MS Flight Simulator x (I know there are better ways to do this using SimConnect).  Basically I have written a VB program to detect signals from a COM port with a toggle switch attached.  I can get the process to work but I have to hard code the PID

Sample

Dim

procID AsInteger

procID = 12116

AppActivate(procID)               

My.Computer.Keyboard.SendKeys("w", True)

Is there a way that I can get the PID for FSX.EXE using VB as this will change every time the application starts?  The graphic shows the number I need.  Many thanks 

All replies (1)

Friday, May 2, 2014 4:48 AM ✅Answered

If you only have one instance of fsx.exe running then this code will only return that one instance of it although it searches for all instances of it running. It gets the process ID from the processes name. So if Flight Simulators process name in Task Manager or Resource Monitor is fsx.exe then the name to use should be fsx just like for Notepad.exe the name to use is Notepad.

If more than one instance of the process name is running then p ("Dim p As Process()") is an array, I believe, and will contain all the process information for all the processes running under that name.

The process ID is returned as an integer.

Option Strict On

Public Class Form1

    Dim ProcessID As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.CenterToScreen()
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim p As Process() = Process.GetProcessesByName("fsx")

        If p.Length > 0 Then
            For i As Integer = 0 To p.Length - 1
                ProcessID = (p(i).Id)
                TextBox2.AppendText("One instance of Flight Simulators process ID is " & CStr(ProcessID) & vbCrLf)
            Next
        End If
    End Sub

End Class

La vida loca