Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleStart and stop another program
DescriptionThis example shows how to start and stop another program in Visual Basic 6.
Keywordsstart application, stop application, API, WM_CLOSE, SendMessage
CategoriesAPI, Software Engineering
 
Thanks to Md. Shahadat Hossain Khan Razon.

When you click the Open button, the program uses Shell to start the other application. It uses the function InstanceToWnd to get the new application's handle from the process ID and saves the handle in a global variable.

 
Private m_Pid As Long

Private Sub Command1_Click()
  If Text1.Text <> Empty Then
    Dim h_wnd As Long
    Dim buf As String
    Dim buf_len As Long

    ' Start the program.
    m_Pid = Shell(Text1.Text, vbNormalFocus)
    If m_Pid = 0 Then
        MsgBox "Error starting program"
        Exit Sub
    End If

    ' Get the window handle.
     pCurrentPlayerHandle = InstanceToWnd(m_Pid)

    ' Display the program's caption.
    buf = Space$(256)
    buf_len = GetWindowText(pCurrentPlayerHandle, buf, _
        Len(buf))
    buf = Left$(buf, buf_len)

    Command2.Enabled = True
  End If
End Sub
 
Function InstanceToWnd looks through porocesses to find a target process ID and returns its handle.
 
Private Function InstanceToWnd(ByVal target_pid As Long) As _
    Long
Dim app_hWnd As Long
Dim app_parent As Long
Dim app_owner As Long
Dim app_visible As Boolean
Dim app_style As Long
Dim app_text As String
Dim app_class As String
Dim wid As Single
Dim col_wid() As Single
Dim r As Integer
Dim c As Integer
Dim test_pid As Long
Dim test_thread_id As Long

    GetWindowInfo app_hWnd, app_parent, app_owner, _
        app_visible, app_style, app_text, app_class

    app_hWnd = GetTopWindow(0)
    r = 1
    Do While app_hWnd <> 0
        ' Get information about this window.
        GetWindowInfo app_hWnd, app_parent, app_owner, _
            app_visible, app_style, app_text, app_class

        ' See if this window is interesting.
        If app_visible And _
            app_parent = 0 And _
            app_owner = 0 And _
            Len(app_text) > 0 And _
            Left$(app_text, 8) <> "VBBubble" And _
            (Left$(app_class, 7) <> "Progman" Or _
                (app_style And WS_OVERLAPPEDWINDOW) <> 0) _
        Then
            If GetParent(app_hWnd) = 0 Then
                ' This is a top-level window. See if
                ' it has the target instance handle.
                test_thread_id = _
                    GetWindowThreadProcessId(app_hWnd, _
                    test_pid)

                If test_pid = target_pid Then
                    ' This is the target.
                    InstanceToWnd = app_hWnd
                    Exit Do
                End If
            End If
            r = r + 1
        End If

        app_hWnd = GetNextWindow(app_hWnd, GW_HWNDNEXT)
    Loop
End Function
 
When you click the Close button, the program sends the WM_CLOSE message to the other application's saved handle.
 
Private Sub Command2_Click()
  'SendMessage pCurrentPlayerHandle, WM_CLOSE, 0, 0
  'Command2.Enabled = False

  ' WJK fix.
  ' 4/19/2k6 variable pid is now public as long
  ' Get the window handle.
  pCurrentPlayerHandle = InstanceToWnd(m_Pid)  '4/19/2k6
  SendMessage pCurrentPlayerHandle, WM_CLOSE, 0, 0
  Command2.Enabled = False
End Sub
 
WJK found that if he started the Win Calculator and changed the view from standard to scientific, then the close routine no longer works.

It seems that changing the view changes the form's window handle. He fixed this by re-finding the window handle from the PID before closing the calculator.

 
 
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated