Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 17,587 times

Related Categories

Get Running Programs

Here's some code that creates a snapshot of the current system state, lists all the running programs, and finds their exe paths.

'Module Code
Option Explicit
Public Const TH32CS_SNAPPROCESS As Long = 2&
Public Const MAX_PATH As Integer = 260
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 * MAX_PATH
End Type
Public Declare Function CreateToolhelpSnapshot Lib "Kernel32" _
Alias "CreateToolhelp32Snapshot" _
(ByVal lFlags As Long, ByVal lProcessID As Long) As Long

Public Declare Function ProcessFirst Lib "Kernel32" _
Alias "Process32First" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Function ProcessNext Lib "Kernel32" _
Alias "Process32Next" _
(ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As Long

Public Declare Sub CloseHandle Lib "Kernel32" _
(ByVal hPass As Long)

'Form Code
Private Sub Command1_Click()
    Dim hSnapShot As Long
    Dim uProcess As PROCESSENTRY32
    Dim r As Long
    hSnapShot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
    If hSnapShot = 0 Then
        Exit Sub
    End If
    uProcess.dwSize = Len(uProcess)
    r = ProcessFirst(hSnapShot, uProcess)
    Do While r
        List1.AddItem uProcess.szExeFile
        r = ProcessNext(hSnapShot, uProcess)
    Loop
    Call CloseHandle(hSnapShot)
End Sub

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments

  • Posted by Smile005 on 29 Jul 2003

    Lol, sorry.
    I haven't got AllAPI's software on my pc and cba to go to their site. But look on their software or site and see what it says about the API.

  • Posted by HyperHacker on 29 Jul 2003

    [quote][1]Posted by [b]HyperHacker[/b] on 28 Jul 2003 07:22 AM[/1]
    I'm using WinXP.
    [/quote] :D

  • Posted by Smile005 on 29 Jul 2003

    I doubt that [code]
    r = ProcessFirst(hSnapShot, uProcess)[/code]
    this is returning a process at all, so the Do Loop it skipped and the program ends.
    What OS are you running it on? It might not work...

  • Posted by HyperHacker on 28 Jul 2003

    That's exactly what I did. It doesn't exit there, it keeps going until "Do While r" and then skips to "Call CloseHandle(hSnapShot)".

  • Posted by Smile005 on 28 Jul 2003

    Lol, we need a bit more information than that.
    Have you tried putting a break point a few statments before the do loop and then debugging from there?

    Oh sorry, I didn't realise this was a comment....