Results 1 to 10 of 10
  1. #1
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check for running application (2K+)

    I have successfully created a macro which opens another application with a specific working file using the shell command:

    <code>Shell "D:Program FilesMicrochipMPLAB IDECoreMPLAB.exe D:Micro801375.mcw"</code>

    where MPLAB.exe is the application and 801375.mcw is the file it opens with. (The .mcw file is a variable and 801375.mcw is one of a number that could be opened.)

    However the application should only be running once. It does not support multiple files - if you attempt to open another .mcw file from within it, it needs to close the currently open file. No problem there, but as the program controls a piece of USB hardware, running it twice could cause a hardware conflict. Currently, running the macro again starts another copy of MPLAB.exe.

    Is there a way to detect MPLAB.exe is already running using VBA? If so, I can then stop the macro from completing.


    (For those interested in the background, MPLAB is an application for developing and programming PIC Microcontrollers. Us 'upstairs' doing the software development want to make life as simple as possible for them 'downstairs' who only want (and should!) open the relevant file. Creating an 'approved' list of files in a worksheet is edging closer to a foolproof environment...)

    TIA

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Check for running application (2K+)

    Does the window have a fixed title? If so, you can use the FindWindow API to test for its presence:

    At the top of a module:

    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

    In your code (simplified):

    Sub RunMPLab()
    If Not FindWindow(vbNullString, "Window caption here") = 0 Then
    MsgBox "Application already running"
    Exit Sub
    End If
    Shell ...
    End Sub

    Replace "Window caption here" with the full caption of the window. If the window caption is variable and you don't know it exactly, this won't work.

  3. #3
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check for running application (2K+)

    Progress, thanks!

    That works well, in that the window caption is relatively fixed. Currently it is "MPLAB IDE v7.00" because it is running version 7.00, but in time this will change. Is there a way to look at (say) just the first 10 characters?

    Alternatively, is there a function/macro that would return all window names? I could adapt that AND make use of it in other applications...

  4. #4
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Check for running application (2K+)

    See if you can use the code from the attached text file. As is it will list all windows whose title begins with "MPLAB IDE" in the Debug window of the Visual Basic Editor. You can modify this to suit your needs.

  5. #5
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check for running application (2K+)

    Excellent, thank you - something I can make great use of.

    That makes it back up to 17 pints of Guinness I owe you again <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Check for running application (2K+)

    Leif,
    You could also try this:
    <pre>Function IsProcNameRunning(ByVal strProcName As String) As Boolean
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strList
    IsProcNameRunning = False
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" _
    & strComputer & "rootcimv2")
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcName & "'")
    If colProcess.Count > 0 Then IsProcNameRunning = True
    End Function
    </pre>


    called with something like:
    <code>If IsProcNameRunning("MPLAB.exe") Then
    ...</code>

    or you could use:
    <pre>Sub FindAndTerminate(ByVal strProcName As String)
    Dim objWMIService, objProcess, colProcess
    Dim strComputer, strList
    strComputer = "."
    Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!" _
    & strComputer & "rootcimv2")
    Set colProcess = objWMIService.ExecQuery _
    ("Select * from Win32_Process Where Name = '" & strProcName & "'")
    If colProcess.Count > 0 Then
    For Each objProcess In colProcess
    objProcess.Terminate
    Next objProcess
    End If
    End Sub
    </pre>


    to just kill it with:
    <code>FindAndTerminate "MPLAB.exe"</code>

    FWIW
    Regards,
    Rory

    Microsoft MVP - Excel

  7. #7
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check for running application (2K+)

    I should have guessed the mention of Guinness might elicit something else - works beautifully. <img src=/S/thankyou.gif border=0 alt=thankyou width=40 height=15>

    Now Hans, about those 8 pints I owe you........

  8. #8
    WS Lounge VIP rory's Avatar
    Join Date
    Dec 2000
    Location
    Burwash, East Sussex, United Kingdom
    Posts
    6,280
    Thanks
    3
    Thanked 191 Times in 177 Posts

    Re: Check for running application (2K+)

    It doesn't take much to get my attention! <img src=/S/grin.gif border=0 alt=grin width=15 height=15>
    Regards,
    Rory

    Microsoft MVP - Excel

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Check for running application (2K+)

    What happened to that half pint? <img src=/S/evilgrin.gif border=0 alt=evilgrin width=15 height=15>

  10. #10
    Plutonium Lounger Leif's Avatar
    Join Date
    Dec 2000
    Location
    U.K.
    Posts
    14,010
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Check for running application (2K+)

    Er.. You drank it. Don't you remember...? <img src=/S/innocent.gif border=0 alt=innocent width=20 height=20>


    <img src=/S/yum.gif border=0 alt=yum width=15 height=15>

Posting Permissions

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