Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1093
  • Last Modified:

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

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
2 Solutions
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
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

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
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?
Moderator, my recommended disposition is:

    Split points between: amkarambelkar and wnaude

DanRollins -- EE database cleanup volunteer

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now