How can i get the PID of the Process which started me (my exe) - I'm seeking for the PID from the cmd.exe process (my program is started from an batch-job).
Printable View
How can i get the PID of the Process which started me (my exe) - I'm seeking for the PID from the cmd.exe process (my program is started from an batch-job).
this might do it:
VB Code:
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer Public Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer Dim hwnd As Integer hwnd = FindWindow("ConsoleWindowClass", vbNullString) Dim pid As Integer GetWindowThreadProcessId(hwnd, pid)
pid will contain the PID.
But what happens if more than one Console-Window is open?
This class is used for managing processes and their children, including a method for killing all of a processes shelled children.
I am sure that you can get that snipped and adapt it for your needs. It generates an array of UDTS which holds the relationship of a processId, and it's parentprocessid. All you'd need to do is iterate down the list looking for your processid, and then read of the parentid
if there is more than one, then it may or may not pick the one that you want. If the Console Window you want has a different Window Title from the other consoles, then you could include that in the FindWindow() so it gets the one that you want, for instance:
hwnd = FindWindow("ConsoleWindowClass", "Enter Window Title Here")