Visual Basic Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old April 13th, 2003, 08:00 AM
X-2-X X-2-X is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: Lagos,NIGERIA.
Posts: 21 X-2-X User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
enumerate Win32 processes from VB

Hello, can somebody help with a method of enumerating running processes

I have successfully enumerated open "windows" but i cant figure out how to enumerate windowless processes
Please help

Reply With Quote
  #2  
Old April 13th, 2003, 03:56 PM
epl epl is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2001
Location: Dublin
Posts: 413 epl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 18 m 18 sec
Reputation Power: 13
I think this should work:
Code:
Option Compare Text: Option Explicit: Option Base 0

Private Type PROCESSENTRY32
  size As Long
  usage As Long
  processId As Long
  defaultHeapId As Long
  moduleId As Long
  cntThreads As Long
  parentProcessId As Long
  classBase As Long
  flags As Long
  exeFile As String * 500
End Type

Private Const INVALID_HANDLE_VALUE As Long = -1
Private Const TH32CS_SNAPPROCESS As Long = 2

Private Declare Function kCloseHandle Lib "kernel32.dll" Alias "CloseHandle" (ByVal hObject As Long) As Long
Private Declare Function kCreateToolhelpSnapshot Lib "kernel32.dll" Alias "CreateToolhelp32Snapshot" (ByVal flags As Long, ByVal processId As Long) As Long
Private Declare Function kProcessFirst Lib "kernel32.dll" Alias "Process32First" (ByVal hSnapshot As Long, processEntry As PROCESSENTRY32) As Long
Private Declare Function kProcessNext Lib "kernel32.dll" Alias "Process32Next" (ByVal hSnapshot As Long, processEntry As PROCESSENTRY32) As Long

Public Sub main(): Call printProcessNames: End Sub

Public Sub printProcessNames()
Dim processes() As String, i As Long
  If getProcessNames(processes) Then
    For i = LBound(processes) To UBound(processes)
      Debug.Print i, processes(i)
    Next i
  End If
End Sub

Private Function getProcessNames(ByRef processes() As String, Optional ByVal sortResults As Boolean = False) As Long
Dim retVal As Long, hSnapshot As Long, processEntry As PROCESSENTRY32
  Let hSnapshot = kCreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0)
  If hSnapshot = INVALID_HANDLE_VALUE Then
    Let retVal = 0
  Else: Let processEntry.size = Len(processEntry)
    If kProcessFirst(hSnapshot, processEntry) Then
      ReDim processes(0 To 0): Do: Call ensureCapacityString(processes, retVal)
        Let processes(inc(retVal)) = getNullTerminatedString(processEntry.exeFile)
      Loop While kProcessNext(hSnapshot, processEntry)
      If retVal <= UBound(processes) Then ReDim Preserve processes(0 To retVal - 1)
    Else: Let retVal = 0
    End If: Call kCloseHandle(hSnapshot)
    'If sortResults Then Call shellSortString(processes)
  End If: Let getProcessNames = retVal
End Function
 
Private Sub ensureCapacityString(ByRef vector() As String, ByVal size As Long, Optional ByVal chunk As Long = 256)
Dim oldCapacity As Long, newCapacity As Long
  Let oldCapacity = 1 - LBound(vector) + UBound(vector)
  Let newCapacity = IIf(size And 255 = 0, size, 256 * (1 + size \ 256))
  If oldCapacity < newCapacity Then ReDim Preserve vector(LBound(vector) To LBound(vector) - 1 + newCapacity)
End Sub

Private Function getNullTerminatedString(ByRef str As String) As String
Dim i As Long
  Let i = InStr(str, vbNullChar)
  Let getNullTerminatedString = Left$(str, IIf(i = 0, Len(str), i - 1))
End Function

Private Function inc(ByRef i As Long) As Long: Let inc = i: Let i = i + 1: End Function

I never finalised the sorting bit though - fell free to post if you get it implemented. Or I'll put it up if I get a chance.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreVisual Basic Programming > enumerate Win32 processes from VB

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap