How to know if xyz.exe is already running in vb6

Posted on 2003-02-23
Medium Priority
Last Modified: 2012-08-14
Hi sirs,
       i'm experimenting vb6 for few week.  I've a little question: How to know easily if program is already running xyz.exe by example? I've try some program example that gives me the whole task manager task list... i'm sure there is an other way to avoid more than 100 line of code!!!
is it?

thank a lot !

Best regards.

steve monfette
Question by:steve_monfette
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

amkarambelkar earned 100 total points
ID: 8007898
if xyz.exe is your own exe then you can write in the xyz.frm as follows and detect if this exe is already running

end if

App refers to current (this) application

Assisted Solution

wnaude earned 100 total points
ID: 8007998
if you want to scan all the running tasks u can use the following :

Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Private Const GW_HWNDFIRST = 0
Private Const GW_HWNDNEXT = 2
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 Sub cmdShow_Click()
Dim CurrenthWnd As Long
Dim LengthOfString As Long
Dim TaskName As String
Dim status As Boolean
Dim task As String
task = "My Task Name"
status = False
CurrenthWnd = GetWindow(Me.hwnd, GW_HWNDFIRST)
While CurrenthWnd <> 0
    LengthOfString = GetWindowTextLength(CurrenthWnd)
    TaskName = Space(LengthOfString + 1)
    GetWindowText CurrenthWnd, TaskName, LengthOfString + 1
    TaskName = Left$(TaskName, Len(TaskName) - 1)
    If TaskName = task Then
         status = True
    End If
         CurrenthWnd = GetWindow(CurrenthWnd, GW_HWNDNEXT)
End Sub


Expert Comment

ID: 8028239
I have used this in the past.

Public Declare Function WTSEnumerateProcesses Lib "wtsapi32.dll" Alias "WTSEnumerateProcessesA" (ByVal hServer As Long, ByVal Reserved As Long, ByVal Version As Long, ByRef ppProcessInfo As Long, ByRef pCount As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public Declare Sub WTSFreeMemory Lib "wtsapi32.dll" (ByVal pMemory As Long)


    SessionID As Long
    ProcessID As Long
    pProcessName As Long
    pUserSid As Long
End Type

Private Function CheckProcess() As Boolean
   Dim RetVal As Long
   Dim Count As Long
   Dim i As Integer
   Dim lpBuffer As Long
   Dim p As Long
   Dim udtProcessInfo As WTS_PROCESS_INFO

    CheckProcess = False
    RetVal = WTSEnumerateProcesses(WTS_CURRENT_SERVER_HANDLE, 0&, 1, lpBuffer, Count)
    If RetVal Then
        p = lpBuffer
        For i = 1 To Count
            CopyMemory udtProcessInfo, ByVal p, LenB(udtProcessInfo)
            If LCase(GetStringFromLP(udtProcessInfo.pProcessName)) = "xyz.exe " Then
                CheckProcess = True
            End If
            p = p + LenB(udtProcessInfo)
        Next i
        WTSFreeMemory lpBuffer
    End If
End Function

Public Function GetStringFromLP(ByVal StrPtr As Long) As String
    Dim b As Byte
    Dim tempStr As String
    Dim bufferStr As String
    Dim Done As Boolean

    Done = False
        CopyMemory b, ByVal StrPtr, 1
        If b = 0 Then
            Done = True
            tempStr = Chr$(b)
            bufferStr = bufferStr & tempStr
            StrPtr = StrPtr + 1
        End If
    Loop Until Done
    GetStringFromLP = bufferStr
End Function

Expert Comment

ID: 8901225
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?
LVL 49

Expert Comment

ID: 8972556
Moderator, my recommended disposition is:

    Split points between: amkarambelkar and wnaude

DanRollins -- EE database cleanup volunteer

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question