Getting Process Id 
Author Message
 Getting Process Id

I'm looking to get the Process Id of a Specific App running in NT or 95/98,
all I really have is the App Title to Identify the Process.

Does any one know of the API to get this.

I'm looking for the Process Id so I can either Close the App or Activate it.

Cheers

Simon



Fri, 10 Aug 2001 03:00:00 GMT  
 Getting Process Id
Hi-
    This isn't exactly what you want, but maybe it'll
give you some ideas.
Joe
____________________snip_______________________________
Option Explicit

Private Declare Function FindWindow Lib _
   "user32" Alias "FindWindowA" _
   (ByVal lpClassName As String, _
   ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib _
   "user32" Alias "SendMessageA" _
   (ByVal hwnd As Long, ByVal wMsg As Long, _
   ByVal wParam As Long, lParam As Any) As Long
Private Declare Function BringWindowToTop Lib _
   "user32" (ByVal hwnd As Long) As Long
Private Const WM_CLOSE = &H10

Private Sub Command1_Click()
   Shell "NotePad.EXE", vbNormalFocus
End Sub

Private Sub Command2_Click()
   Dim lRetVal As Long
   ' Use class name if you have it
   lRetVal = FindWindow("Notepad", vbNullString)
   'lRetVal = FindWindow(vbNullString, "Untitled - Notepad")
   If lRetVal <> 0 Then
      lRetVal = SendMessage(lRetVal, WM_CLOSE, 0&, 0&)
   End If
End Sub

Private Sub Command3_Click()
   Dim lRetVal As Long
   ' Use class name if you have it
   lRetVal = FindWindow("Notepad", vbNullString)
   'lRetVal = FindWindow(vbNullString, "Untitled - Notepad")
   If lRetVal <> 0 Then
      lRetVal = BringWindowToTop(lRetVal)
      ' You could use this too
      'AppActivate ("Untitled - Notepad")
   End If
End Sub

Private Sub Form_Load()
   Command1.Caption = "Shell NotePad"
   Command2.Caption = "Close NotePad"
   Command3.Caption = "Activate NotePad"
End Sub
____________snip___________________________________________
--
 ***********************************************************

 Microsoft Developer MVP- Visual Basic
 Check out Yankee Clipper Plus- August 98 PCComputing
 Mag "Amazing Free Stuff: Programs and Power Toys"
 http://www.tiac.net/users/lvasseur/ycphome.html
"There is only one good: knowledge, and one
evil: ignorance."  Socrates
 **********************************************************
 PS- Please reply to the newsgroup- except in the
 case of flames, insults, etc. (Don't bother.)

Quote:

>I'm looking to get the Process Id of a Specific App running in NT or 95/98,
>all I really have is the App Title to Identify the Process.

>Does any one know of the API to get this.

>I'm looking for the Process Id so I can either Close the App or Activate
it.

>Cheers

>Simon



Fri, 10 Aug 2001 03:00:00 GMT  
 Getting Process Id
Hi Simon,

Here is a sample:

1.Start a new project
2. Place a Command1 button on Form1.
3. Copy the following code to Form1:

Private Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, _
ByVal wCmd As Long) As Long

Private Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _
(ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long

Private Declare Function GetWindowTextLength Lib "user32" Alias _
"GetWindowTextLengthA" (ByVal hWnd As Long) As Long

Private Declare Function IsWindowVisible Lib "user32" _
(ByVal hWnd As Long) As Long

Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As
Long, lpdwProcessId As Long) As Long

'GetWindow() constants
    Private Const GW_HWNDFIRST = 0
    Private Const GW_HWNDLAST = 1
    Private Const GW_HWNDNEXT = 2
    Private Const GW_HWNDPREV = 3
    Private Const GW_OWNER = 4
    Private Const GW_CHILD = 5
    Private Const GWL_HWNDPARENT = (-8)

    Private Const GWW_HWNDPARENT = (-8)
Private Function FindAppWindow(sAppTitle As String, hWndStart As Long) As Long
    'Finds a Window handle by its (partial) Title.
    'Only top level windows that have WS_VISIBLE style are
    'searched.
    ''''''''''''''''''''''''''''''''''''''''''''''''
    'sAppTitle - string that is contained in the Window title.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    'On success returns the hWnd of the window that contains the
    'sAppTitle string; Otherwise returns 0.
    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    Dim CurrWnd As Long
    Dim Length As Long
    Dim TaskName As String
    Dim lPos As Long

    CurrWnd = GetWindow(hWndStart, GW_HWNDFIRST)

    While CurrWnd <> 0
        If CurrWnd <> hWndStart And (0 <> IsWindowVisible(CurrWnd)) And (0 =
GetWindow(CurrWnd, GW_OWNER)) Then
            'Find the length of the Title
            Length = GetWindowTextLength(CurrWnd)

            'Does the Window have any title
            If Length > 0 Then
                'Allocate enough memory for the text
                TaskName = Space$(Length + 1)
                'Get the title text of the window
                Length = GetWindowText(CurrWnd, TaskName, Length + 1)
                'Get the Window's title
                TaskName = Left$(TaskName, Len(TaskName) - 1)

                'Check if it is the window we are looking for
                If InStr(1, TaskName, sAppTitle) > 0 Then
                    'We found it
                    FindAppWindow = CurrWnd
                    Exit Function
                End If
           End If
        End If
        'Get next window handle
        CurrWnd = GetWindow(CurrWnd, GW_HWNDNEXT)
        DoEvents
    Wend
End Function

Private Sub Command1_Click()
    Dim lThreadID As Long
    Dim lProcessID As Long
    Dim hWnd As Long
    Dim sAppName As String

    'Searching for the Windows Explorer
    sAppName = "Exploring -"

    hWnd = FindAppWindow(sAppName, Me.hWnd)

    If hWnd <> 0 Then
        'Find the ThreadID and ProcessID
        'that created this window.
        lThreadID = GetWindowThreadProcessId(hWnd, lProcessID)

        Debug.Print "hWnd = "; CStr(hWnd); "  ProcessID = "; CStr(lProcessID);
        Debug.Print "  ThreadID = "; CStr(lThreadID)
    End If

End Sub

4. Start your Windows Explorer and then run the Project and press the Command1
button.

In the Debug window you should see the hWnd, ProcessID and ThreadID for the
Explorer's window.

Regards,
               Stoil

Quote:

> I'm looking to get the Process Id of a Specific App running in NT or 95/98,
> all I really have is the App Title to Identify the Process.

> Does any one know of the API to get this.

> I'm looking for the Process Id so I can either Close the App or Activate it.

> Cheers

> Simon



Fri, 10 Aug 2001 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Help - getting Process ID

2. Getting IDs for Multiple Processes

3. Getting the task/process ID for application

4. Getting running process Ids/killing task under NT

5. Getting running process Ids/killing task under NT

6. Getting The Process ID

7. Getting the Process ID

8. Getting the ID of a Process

9. Getting a Process ID from an Object handle

10. Getting the process Ids of a remote computer

11. Control process ID and Thread ID

12. Parent ID of a given Process ID under NT 4.0

 

 
Powered by phpBB® Forum Software