CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Processes

  1. #1
    Join Date
    Mar 2000
    Location
    India
    Posts
    9

    Processes

    How can i get all the running processes along with the name of the applications.


  2. #2
    Join Date
    May 1999
    Posts
    3,332

    Re: Processes

    here is Aaron Young's code:

    'In a Standard Module..
    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
    End Type

    private Type OSVERSIONINFO
    dwOSVersionInfoSize as Long
    dwMajorVersion as Long
    dwMinorVersion as Long
    dwBuildNumber as Long
    dwPlatformId as Long
    szCSDVersion as string * 128
    End Type

    public Declare Function Process32First Lib "kernel32" (byval hSnapshot as Long, lppe as PROCESSENTRY32) as Long
    public Declare Function Process32Next Lib "kernel32" (byval hSnapshot as Long, lppe as PROCESSENTRY32) as Long
    public Declare Function CloseHandle Lib "Kernel32.dll" (byval Handle as Long) as Long
    public Declare Function OpenProcess Lib "Kernel32.dll" (byval dwDesiredAccessas as Long, byval bInheritHandle as Long, byval dwProcId as Long) as Long
    public Declare Function EnumProcesses Lib "psapi.dll" (byref lpidProcess as Long, byval cb as Long, byref cbNeeded as Long) as Long
    public Declare Function GetModuleFileNameExA Lib "psapi.dll" (byval hProcess as Long, byval hModule as Long, byval ModuleName as string, byval nSize as Long) as Long
    public Declare Function EnumProcessModules Lib "psapi.dll" (byval hProcess as Long, byref lphModule as Long, byval cb as Long, byref cbNeeded as Long) as Long
    public Declare Function CreateToolhelp32Snapshot Lib "kernel32" (byval dwFlags as Long, byval th32ProcessID as Long) as Long
    private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (lpVersionInformation as OSVERSIONINFO) as Long

    public Const VER_PLATFORM_WIN32_WINDOWS = 1
    public Const PROCESS_QUERY_INFORMATION = 1024
    public Const PROCESS_VM_READ = 16
    public Const TH32CS_SNAPPROCESS = &H2

    public Function CheckVersion() as Long
    Dim tOS as OSVERSIONINFO
    tOS.dwOSVersionInfoSize = len(tOS)
    Call GetVersionEx(tOS)
    CheckVersion = tOS.dwPlatformId
    End Function
    'In a Form with a Listbox and a Command Button..
    private Sub Command1_Click()
    Dim aPID() as Long
    Dim lProcesses as Long
    Dim lProcess as Long
    Dim lModule as Long
    Dim sName as string
    Dim iIndex as Integer
    Dim bCopied as Long
    Dim lSnapShot as Long
    Dim tPE as PROCESSENTRY32

    List1.Clear

    If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS then
    'Windows 9x
    'Create a SnapShot of the Currently Running Processes
    lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If lSnapShot < 0 then Exit Sub
    tPE.dwSize = len(tPE)
    'Buffer the First Processes Info..
    bCopied = Process32First(lSnapShot, tPE)
    While bCopied
    'While there are Processes List them..
    List1.AddItem _
    Right$("000" & tPE.th32ProcessID, 3) & " - " & _
    Left$(tPE.szExeFile, InStr(proc.szExeFile, Chr(0)) - 1)
    bCopied = Process32Next(lSnapShot, tPE)
    Wend
    else
    'Windows NT
    'The EnumProcesses Function doesn't indicate how many Process there are,
    'so you need to pass a large array and trim off the empty elements
    'as cbNeeded will return the no. of Processes copied.
    ReDim aPID(255)
    Call EnumProcesses(aPID(0), 1024, lProcesses)
    lProcesses = lProcesses / 4
    ReDim Preserve aPID(lProcesses)

    for iIndex = 0 to lProcesses - 1
    'get the Process Handle, by Opening the Process
    lProcess = OpenProcess(PROCESS_QUERY_INFORMATION Or PROCESS_VM_READ, 0, aPID(iIndex))
    If lProcess then
    'Just get the First Module, all we need is the Handle to get
    'the Filename..
    If EnumProcessModules(lProcess, lModule, 4, 0&) then
    sName = Space(260)
    Call GetModuleFileNameExA(lProcess, lModule, sName, len(sName))
    While InStr(sName, "\")
    sName = mid$(sName, InStr(sName, "\") + 1)
    Wend
    List1.AddItem Right$("000" & aPID(iIndex), 3) & " - " & sName
    End If
    'Close the Process Handle
    lRet = CloseHandle(lProcess)
    End If
    next
    End If
    End Sub








  3. #3
    Join Date
    Jun 2005
    Location
    gandhinagar,gujarat
    Posts
    51

    Talking Re: Processes

    tx for such a nice code
    I want to draw attantion on some syntex error which is as belw

    ;;;;;
    ;;;
    If CheckVersion() = VER_PLATFORM_WIN32_WINDOWS Then
    'Windows 9x
    'Create a SnapShot of the Currently Running Processes
    lSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
    If lSnapShot & lt = 0 Then Exit Sub
    (If lSnapShot < 0 then Exit Sub)...............................this gives syntex error
    tPE.dwSize = Len(tPE)
    'Buffer the First Processes Info..
    bCopied = Process32First(lSnapShot, tPE)
    While bCopied
    'While there are Processes List them..
    List1.AddItem

    ;;;;;
    ;;
    I have pated partly of code which contains error in above code

    Ok thank again for such anice code...but can I get the path of a processes from the modified code? if yes then how" please show it...I am using windows xp..
    Last edited by mynick; March 1st, 2006 at 06:42 AM. Reason: meaning full error !!!!!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured