Check if a process is running on Win7 64bit-VBForums
Results 1 to 6 of 6

Thread: Check if a process is running on Win7 64bit

  1. #1

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Check if a process is running on Win7 64bit

    I have a vb6 function that determines if a process is running. This worked on 32bit systems, but it is not working 64bit Win7. How can I check if a process is running on a 64bit OS?

    Thanks!
    Brian

  2. #2
    Frenzied Member Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,079

    Re: Check if a process is running on Win7 64bit

    What are you using now?

  3. #3

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Check if a process is running on Win7 64bit

    Here is the code I have...

    Public Function GetProcesses(ByVal EXEName As String) As Boolean

    Dim booResult As Boolean
    Dim lngLength As Long
    Dim lngProcessID As Long
    Dim strProcessName As String
    Dim lngSnapHwnd As Long
    Dim udtProcEntry As PROCESSENTRY32
    Dim lngCBSize As Long 'Specifies the size, In bytes, of the lpidProcess array
    Dim lngCBSizeReturned As Long 'Receives the number of bytes returned
    Dim lngNumElements As Long
    Dim lngProcessIDs() As Long
    Dim lngCBSize2 As Long
    Dim lngModules(1 To 200) As Long
    Dim lngReturn As Long
    Dim strModuleName As String
    Dim lngSize As Long
    Dim lngHwndProcess As Long
    Dim lngLoop As Long
    Dim b As Long
    Dim c As Long
    Dim e As Long
    Dim d As Long
    Dim pmc As PROCESS_MEMORY_COUNTERS
    Dim lret As Long
    Dim strProcName2 As String
    Dim strProcName As String
    Dim MyPos_Instr As Integer

    'Turn on Error handler
    On Error GoTo Error_handler

    booResult = False

    GetProcesses = False

    EXEName = UCase$(Trim$(EXEName))
    lngLength = Len(EXEName)

    'ProcessInfo.bolRunning = False

    ' Select Case getVersion()
    ' 'I'm not bothered about windows 95/98 becasue this class probably wont be used on it anyway.
    ' Case WIN95_System_Found 'Windows 95/98
    '
    ' Case WINNT_System_Found 'Windows NT

    lngCBSize = 8 ' Really needs To be 16, but Loop will increment prior to calling API
    lngCBSizeReturned = 96

    Do While lngCBSize <= lngCBSizeReturned
    DoEvents
    'Increment Size
    lngCBSize = lngCBSize * 2
    'Allocate Memory for Array
    ReDim lngProcessIDs(lngCBSize / 4) As Long
    'Get Process ID's
    lngReturn = EnumProcesses(lngProcessIDs(1), lngCBSize, lngCBSizeReturned)
    Loop

    'Count number of processes returned
    lngNumElements = lngCBSizeReturned / 4
    'Loop thru each process

    For lngLoop = 1 To lngNumElements
    DoEvents

    'Get a handle to the Process and Open
    lngHwndProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, lngProcessIDs(lngLoop))

    If lngHwndProcess <> 0 Then
    'Get an array of the module handles for the specified process
    lngReturn = EnumProcessModules(lngHwndProcess, lngModules(1), 200, lngCBSize2)

    'If the Module Array is retrieved, Get the ModuleFileName
    If lngReturn <> 0 Then

    'Buffer with spaces first to allocate memory for byte array
    strModuleName = Space(MAX_PATH)

    'Must be set prior to calling API
    lngSize = 500

    'Get Process Name
    lngReturn = GetModuleFileNameExA(lngHwndProcess, lngModules(1), strModuleName, lngSize)

    'Remove trailing spaces
    strProcessName = Left(strModuleName, lngReturn)

    'Check for Matching Upper case result
    strProcessName = UCase$(Trim$(strProcessName))

    strProcName2 = GetElement(Trim(Replace(strProcessName, Chr$(0), "")), "\", 0, 0, GetNumElements(Trim(Replace(strProcessName, Chr$(0), "")), "\") - 1)

    If strProcName2 = EXEName Then
    'Get the Site of the Memory Structure
    'pmc.cb = LenB(pmc)
    'lret = GetProcessMemoryInfo(lngHwndProcess, pmc, pmc.cb)
    'Debug.Print EXEName & "::" & CStr(pmc.WorkingSetSize / 1024)
    'MsgBox EXEName & "::" & CStr(pmc.WorkingSetSize / 1024)
    GetProcesses = True
    End If
    End If
    End If
    'Close the handle to this process
    lngReturn = CloseHandle(lngHwndProcess)
    DoEvents
    Next

    ' End Select

    IsProcessRunning_Exit:

    'Exit early to avoid error handler
    Exit Function
    Error_handler:
    Err.Raise Err, Err.Source, "ProcessInfo", Error
    Resume Next
    End Function

  4. #4
    Frenzied Member Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,079

    Re: Check if a process is running on Win7 64bit

    Ugh, please use Code-Tags and mark the line where the error occurs.

    On a guess, i'd say it has to do with you wanting to look up a 64-Bit-Process from a 32-Bit application

    Maybe this will give you some clues (Scroll down until the end where the comments are): http://msdn.microsoft.com/en-us/libr...=vs.85%29.aspx

  5. #5

    Thread Starter
    Hyperactive Member BrianPaul's Avatar
    Join Date
    Aug 2007
    Posts
    294

    Re: Check if a process is running on Win7 64bit

    For some strange reason, I don't have the code tag. I have tags for links, email, and images, but the code tag seems to be missing. I've used it many times in previous posts, but it seems to be missing for me at the moment. Anyway, I found this code which works. If I can figure out why the code tag is missing, I'll come back and edit this so it doesn't look so messy.

    Function getProcessInfo(ByVal EXEName As String) As Boolean
    Dim objProcess As Object, process As Object, strNameOfUser As String, ComputerName As String
    getProcessInfo = False
    ComputerName = "."
    Set objProcess = GetObject("winmgmts:{impersonationLevel=impersonate}\\" _
    & ComputerName & "\root\cimv2").ExecQuery("Select * From Win32_Process")
    For Each process In objProcess
    If process.Name = EXEName Then '<> "System Idle Process" And process.Name <> "System" Then
    getProcessInfo = True
    End If
    Next

    Set objProcess = Nothing
    End Function

  6. #6
    Frenzied Member Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    1,079

    Re: Check if a process is running on Win7 64bit

    You can insert Code-Tags manually

    [ code]
    [/ code]
    without the blanks

    Ugh, the damned WMI!
    Anyway: scroll down to the last post
    http://social.msdn.microsoft.com/For...2-398e8f03238a
    Last edited by Zvoni; Sep 4th, 2012 at 08:57 AM.

Tags for this Thread

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.