Results 1 to 5 of 5

Thread: Howto get parent Process PID

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Posts
    6

    Howto get parent Process PID

    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).

  2. #2
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Howto get parent Process PID

    this might do it:

    VB Code:
    1. Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
    2.     Public Declare Function GetWindowThreadProcessId Lib "user32" Alias "GetWindowThreadProcessId" (ByVal hwnd As Integer, ByRef lpdwProcessId As Integer) As Integer
    3.  
    4. Dim hwnd As Integer
    5.         hwnd = FindWindow("ConsoleWindowClass", vbNullString)
    6.         Dim pid As Integer
    7.         GetWindowThreadProcessId(hwnd, pid)

    pid will contain the PID.

  3. #3

    Thread Starter
    New Member
    Join Date
    Feb 2003
    Posts
    6

    Re: Howto get parent Process PID

    But what happens if more than one Console-Window is open?

  4. #4
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Howto get parent Process PID

    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
    Attached Files Attached Files
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  5. #5
    Frenzied Member
    Join Date
    Jul 2005
    Posts
    1,168

    Re: Howto get parent Process PID

    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")

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width