Results 1 to 14 of 14

Thread: Get CPU Usage

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Red face

    Hi All,

    Can anybody tell me how I get the CPU usage and then print it as a percent? Thanks
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Go to my Website and download the registry module

    Then use it like this:
    Code:
    CPU = Asc(RegVal("HKEY_DYN_DATA\PerfStats\StatData\KERNEL" & vbNullChar & "CPUUsage"))
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 1999
    Location
    Leeds, UK
    Posts
    287

    Thumbs up

    Thanks Kedaman!
    rino_2
    Visual Basic, HTML
    Please Visit my Site: Richard's VB Site

  4. #4
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    How can I do this under Winnt or Win2k?

    They don't have these settings
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  5. #5
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    Is that really a VB program? How do you do that?
    Regards,
    Laurens

    Using VB5 Enterprise edition SP3
    VB6 Enterprise edition SP5

  6. #6
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Paul>
    Nope, dyndata isn't there/hidden in registry under winnt/2k, but i'm sure there's another way.
    Laurens>
    What do you mean with vb program? you can use access it from the registry from vb.

    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  7. #7
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362

    Kedaman...

    Where did you get a listing for DYN_DATA?

    I've been looking for it for ages.


    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  8. #8
    Addicted Member LAURENS's Avatar
    Join Date
    Jan 2000
    Location
    Utrecht, the Netherlands
    Posts
    138
    I mean your CPU.exe
    Regards,
    Laurens

    Using VB5 Enterprise edition SP3
    VB6 Enterprise edition SP5

  9. #9
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715

    Re: Kedaman...

    Originally posted by tumblingdown
    Where did you get a listing for DYN_DATA?

    I've been looking for it for ages.


    td.
    Start --> Run --> Regedit.exe

    Then regedit starts, open the node HKEY_DYN_DATA, then click PerfStats, and then StatData. There are all the values like cpu usage stored.
    Oetje
    [email protected]
    93606776
    Visual Basic 6, Windows 2000

    Never pet a burning dog

  10. #10
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    soz, should have said, i'm on NT.

    You cant browse _DYN_DATA on NT.


    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  11. #11
    Fanatic Member
    Join Date
    Feb 2000
    Location
    Japan
    Posts
    840
    That's the whole point!

    It's not there, the software doesn't work under NT.

    I think you would need to call the api for perfmon, I think that's how task manager works for CPU etc
    Paul Dwyer
    Network Engineer
    Aussie In Tokyo

    Using Powerbasic 6 & VB6 SP4 (Please also add your VB Version to your signature!)

  12. #12
    Hyperactive Member tumblingdown's Avatar
    Join Date
    Mar 2000
    Posts
    362
    Not true.

    _DYN_DATA is there. NT uses it. Perfmon uses it.
    I would just like to know what other interesting stuff is in there.


    td.
    "One logical slip and an entire scientific edifice comes tumbling down." - Robert M. Pirsig


    [email protected]

    "but if Einstein is right and God is in the details, reality requires that we sometimes get religion." - Scott Meyers.

  13. #13
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754

    CPU Usage under NT

    I just butchered one of my bits of code that lists running processes to include memory usage - it might be of use, it might not. Here it is anyway.

    '1 FORM 1 LIST BOX AND 1 COMMAND BUTTON

    Code:
    'MODULE CODE
    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
    
          Public Declare Function GetVersionExA Lib "kernel32" _
             (lpVersionInformation As OSVERSIONINFO) As Integer
    
    
        Public Declare Function GetProcessMemoryInfo Lib "psapi.dll" _
            (ByVal Process As Long, ppsmemCounters As PROCESS_MEMORY_COUNTERS, ByVal cb As Long) As Boolean
        'Public Declare Function GetProcessMemoryInfo Lib "psapi.dll" _
        '(ByVal lHandle As Long, ByVal lpStructure As Long, ByVal lSize As Long) As Integer
    
          Public Type PROCESSENTRY32
             dwSize As Long
             cntUsage As Long
             th32ProcessID As Long           ' This process
             th32DefaultHeapID As Long
             th32ModuleID As Long            ' Associated exe
             cntThreads As Long
             th32ParentProcessID As Long     ' This process's parent process
             pcPriClassBase As Long          ' Base priority of process threads
             dwFlags As Long
             szExeFile As String * 260       ' MAX_PATH
          End Type
    
        Public Type PROCESS_MEMORY_COUNTERS
            cb As Long
            PageFaultCount As Long
            PeakWorkingSetSize As Long
            WorkingSetSize As Long
            QuotaPeakPagedPoolUsage As Long
            QuotaPagedPoolUsage As Long
            QuotaPeakNonPagedPoolUsage As Long
            QuotaNonPagedPoolUsage As Long
            PagefileUsage As Long
            PeakPagefileUsage As Long
        End Type
    
        Public Type OSVERSIONINFO
           dwOSVersionInfoSize As Long
           dwMajorVersion As Long
           dwMinorVersion As Long
           dwBuildNumber As Long
           dwPlatformId As Long           '1 = Windows 95.
                                          '2 = Windows NT
    
           szCSDVersion As String * 128
        End Type
    
          Public Const PROCESS_QUERY_INFORMATION = 1024
          Public Const PROCESS_VM_READ = 16
          Public Const MAX_PATH = 260
          Public Const STANDARD_RIGHTS_REQUIRED = &HF0000
          Public Const SYNCHRONIZE = &H100000
          'STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFF
          Public Const PROCESS_ALL_ACCESS = &H1F0FFF
          Public Const TH32CS_SNAPPROCESS = &H2&
          Public Const hNull = 0
    
          Function StrZToStr(s As String) As String
             StrZToStr = Left$(s, Len(s) - 1)
          End Function
    
          Public Function getVersion() As Long
             Dim osinfo As OSVERSIONINFO
             Dim retvalue As Integer
             osinfo.dwOSVersionInfoSize = 148
             osinfo.szCSDVersion = Space$(128)
             retvalue = GetVersionExA(osinfo)
             getVersion = osinfo.dwPlatformId
          End Function
    'END MODULE CODE
    
    'FORM CODE
    Option Explicit
    
          Private Sub Command1_Click()
          List1.Clear
          Select Case getVersion()
    
          Case 1 'Windows 95/98
    
             Dim f As Long, sname As String
             Dim hSnap As Long, proc As PROCESSENTRY32
             hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
             If hSnap = hNull Then Exit Sub
             proc.dwSize = Len(proc)
             ' Iterate through the processes
             f = Process32First(hSnap, proc)
             Do While f
               sname = StrZToStr(proc.szExeFile)
               List1.AddItem sname
               f = Process32Next(hSnap, proc)
             Loop
    
          Case 2 'Windows NT
    
             Dim cb As Long
             Dim cbNeeded As Long
             Dim NumElements As Long
             Dim ProcessIDs() As Long
             Dim cbNeeded2 As Long
             Dim NumElements2 As Long
             Dim Modules(1 To 200) As Long
             Dim lRet As Long
             Dim ModuleName As String
             Dim nSize As Long
             Dim hProcess As Long
             Dim i As Long
             Dim GotMemInfo As Boolean
             Dim MemInfo As PROCESS_MEMORY_COUNTERS
             'Get the array containing the process id's for each process object
             cb = 8
             cbNeeded = 96
             Do While cb <= cbNeeded
                cb = cb * 2
                ReDim ProcessIDs(cb / 4) As Long
                lRet = EnumProcesses(ProcessIDs(1), cb, cbNeeded)
             Loop
             NumElements = cbNeeded / 4
    
             For i = 1 To NumElements
                'Get a handle to the Process
                hProcess = OpenProcess(PROCESS_QUERY_INFORMATION _
                   Or PROCESS_VM_READ, 0, ProcessIDs(i))
                'Got a Process handle
                If hProcess <> 0 Then
                    GotMemInfo = GetProcessMemoryInfo(hProcess, MemInfo, Len(MemInfo))
                    'Get an array of the module handles for the specified
                    'process
                    lRet = EnumProcessModules(hProcess, Modules(1), 200, _
                                                 cbNeeded2)
                    'If the Module Array is retrieved, Get the ModuleFileName
                    If lRet <> 0 Then
                       ModuleName = Space(MAX_PATH)
                       nSize = 500
                       lRet = GetModuleFileNameExA(hProcess, Modules(1), _
                                       ModuleName, nSize)
                       List1.AddItem Left(ModuleName, lRet) & " MEM USAGE " & MemInfo.PagefileUsage
                    End If
                End If
              'Close the handle to the process
             lRet = CloseHandle(hProcess)
             Next
    
          End Select
          End Sub
    'END FORM CODE


    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  14. #14
    Hyperactive Member noble's Avatar
    Join Date
    Nov 2000
    Location
    Philly
    Posts
    471
    You're looking in the wrong place for NT
    Dyn_data doesn't exist however PerfData is found
    in a different key, don't know off hand and i'm on 9x
    machine now.

    Do a search for perfdata and u will find the information
    you need.

    Also, perfdata in NT is the source of all the process
    list information when u hit ctrl-atl-del to view processes.
    Bababooey
    Tatatoothy
    Mamamonkey

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