본문 바로가기

work/vb

프로세스 실행 여부 체크

Public Type PROCESSENTRY32 '구조체를 선언 한다
   dwSize As Long '사이즈
   cntUsage As Long '사용 중
   th32ProcessID As Long '프로 세서 아이디
   th32DefaultHeapID As Long
   th32ModuleID As Long
   cntThreads As Long '쓰레드
   th32ParentProcessID As Long
   pcPriClassBase As Long
   dwFlags As Long
   szExeFile As String * 260 'exe 파일명을 리턴 해 준다
End Type

Public Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, ByVal lProcessID As Long) As Long
Public Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long
Public Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)

'================여기까지 선언======================================
 

Public Function funcGetProcess(ByVal ExeFilePath As String) As Boolean
Dim hSnapShot As Long, hProcess As Long
Dim uProcess As PROCESSENTRY32

On Error GoTo ErrHandler

    iCurProcess = 0

    hSnapShot = CreateToolhelpSnapshot(2, 0)
    uProcess.dwSize = LenB(uProcess)
    hProcess = Process32First(hSnapShot, uProcess)

    Do While hProcess

        If Left(LCase(Trim(uProcess.szExeFile)), Len(ExeFilePath)) = LCase(ExeFilePath) Then
            iCurProcess = iCurProcess + 1
        End If

        hProcess = Process32Next(hSnapShot, uProcess)    

    Loop

   'iCurProcess 가 1 이상이면 존재 하는것이다.
   '아래의 if문은 프로세스를 여러개띄우기 위해 프로세스의 최대값과 비교하는것


    If iMaxProcess - iCurProcess > 0 Then
        funcGetProcess = True
    Else
        funcGetProcess = False
    End If

    CloseHandle hSnapShot

    Exit Function
   

ErrHandler:

    Call LogScheduler.WriteLogs("Occurred Exception : " & Error(Err))
    Err.Clear
    funcGetProcess = False

End Function