Results 1 to 5 of 5

Thread: Probably an old question

  1. #1

    Thread Starter
    Hyperactive Member parkes's Avatar
    Join Date
    Jan 1999
    Location
    Unitied Kingdom
    Posts
    303
    I know this is probably an old question but its one I could do with being answered.

    How do you check to see if another program is running?
    Thanks in advance for any help provided.

    VB 6 Enterprise Edition SP4
    ADO, SQL 7/2000, ASP and some JavaScript


    >> Life goes on, but for how long? <<
    If you can smile when things go wrong, you have someone in mind to blame

  2. #2
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    ' See if there is already and instance.
        If App.PrevInstance Then
     ' Activate the previous instance
            AppActivate App.Title
            
    ' Terminate the new instance
            Unload Me
        End If
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  3. #3

    Thread Starter
    Hyperactive Member parkes's Avatar
    Join Date
    Jan 1999
    Location
    Unitied Kingdom
    Posts
    303
    Sorry my mistake I didn't explain myself clearly.
    I would like to check if something like Word or Excel is open or just about any other program.
    Thanks in advance for any help provided.

    VB 6 Enterprise Edition SP4
    ADO, SQL 7/2000, ASP and some JavaScript


    >> Life goes on, but for how long? <<
    If you can smile when things go wrong, you have someone in mind to blame

  4. #4
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754

    check this code out...

    http://www.blackbeltvb.com/free/getwhwnd.htm

    I would have butchered the code myself to stop when you reach a certain window (i.e. your program) but I don't think Matt Hart (the author) would've liked that so take a look at the code and see what you think, it basically runs through all your windows and gets their names into a list box. There might be an easier way to do this, but this works for me
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  5. #5
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    Using Part of a title find an existance
    
    Start Notepad and minimize it
    
    This code uses EnumWindows to retrieve the title of all 
    windows in the system, compares it with the partial name, 
    and stops when a match is made.  
    
    this example looks for notepad based on a partial knowlege
    of the title..ie I look for notepa
    
    When I find it I close it.
    
    '~~~~~~~MODULE CODE~~~~~~~~~~~~ 
    
    Option Explicit 
    
    Public Const WM_CLOSE = &H10 
    Public Const MAX_PATH = 260 
    
    Declare Function FindWindow Lib "user32" Alias "FindWindowA" _ 
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long 
    Declare Function PostMessage Lib "user32" Alias "PostMessageA" _ 
        (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _ 
        ByVal lParam As Long) As Long 
    Declare Function EnumWindows Lib "user32" _ 
        (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long 
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" _ 
        (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long 
    
    Private sAppTitle As String 
    Public glHwnd As Long 
    
    Public Function fEnumWindowsCallBack(ByVal hwnd As Long, ByVal lpData As Long) As Long 
        Dim lResult    As Long 
        Dim sWndName   As String 
         
        fEnumWindowsCallBack = 1 
        sWndName = Space$(MAX_PATH) 
         
        lResult = GetWindowText(hwnd, sWndName, MAX_PATH) 
        sWndName = Left$(sWndName, lResult) 
        'Search Title for our string 
        If (InStr(1, sWndName, sAppTitle, vbTextCompare) > 0) Then 
            Debug.Print sWndName 
            glHwnd = hwnd 
            fEnumWindowsCallBack = 0 
        End If 
    End Function 
    
    Public Function SearchWindows(sApp As String, hwnd As Long) As Long 
        sAppTitle = sApp 
        glHwnd = 0 
        Call EnumWindows(AddressOf fEnumWindowsCallBack, hwnd) 
        SearchWindows = glHwnd 
    End Function
    
    '~~~~~~~FORM CODE~~~~~~~~~~~~~~ 
    
    Option Explicit 
    
    Private Sub Command1_Click() 
        Dim sApp As String 
        'Find notepad with partial name 
        sApp = "notepa" 
        glHwnd = SearchWindows(sApp, Me.hwnd) 
        
    'I End the running application if found 
       'you would put your code here to do what you want
       'to do
        If glHwnd > 0 Then 
            PostMessage glHwnd, WM_CLOSE, 0&, 0& 
        End If 
    End Sub
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

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