Buchempfehlung
Windows System Programming
Windows System Programming
Das Kompendium liefert viele interessante Informationen zur Windows-Programmierung auf Englisch. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

fb:porticula NoPaste

Info
Info / Hilfe
Liste
Übersicht / Liste
Neu
Datei hochladen
Suche
Quellcode suchen
Download
Dateidownload

GetValueNT() / GetValue9x()

Uploader:Mitgliedcsde_rats
Datum/Zeit:11.11.2007 16:42:34

#Include "windows.bi"

Dim Shared PdhCollectQueryData As Function ( _
  ByVal QueryHandle As Long) As Long
Dim Shared PdhVbGetDoubleCounterValue As Function ( _
  ByVal CounterHandle As Long, _
  ByRef CounterStatus As Long) As Double

Dim As Any Ptr PDH=DylibLoad("PDH.DLL")
PdhCollectQueryData=DylibSymbol(PDH, "PdhCollectQueryData")
PdhVbGetDoubleCounterValue=DylibSymbol(PDH, "PdhVbGetDoubleCounterValue")

Const PDH_CSTATUS_VALID_DATA = &H0
Const PDH_CSTATUS_NEW_DATA = &H1
'Const ERROR_SUCCESS = 0
'Const VER_PLATFORM_WIN32_NT = 2

Dim Shared hQuery As Long
Dim Shared hCounter As Long
Dim Shared RetVal As Long

Dim Shared Stack() As Long
Dim Shared StackPointer As Long
Dim Shared m_StackSize As Long
Dim Shared m_Sum As Long

Dim Shared m_BandWidth As Long

Sub PushBandWidth(ByRef NewBandWidth As Long)
  Static u As Long

  u = UBound(Stack)
  If StackPointer <= u Then
    Stack(StackPointer) = NewBandWidth
    m_Sum = m_Sum + NewBandWidth
    StackPointer = StackPointer + 1

  Else
    m_Sum = m_Sum - Stack(0) + NewBandWidth
    CopyMemory(Stack(0), Stack(1), u * 4)
    Stack(u) = NewBandWidth

  End If
  m_BandWidth = m_Sum / StackPointer
End Sub

Function GetValue9x() As Long
  Dim V As Long
  Static hK As Long, sK As String
  Const KDyn As Long = &H80000006

  sK = *IIf(hK = 0, @"PerfStats\StartStat", @"PerfStats\StatData")
  If RegOpenKey(KDyn, sK, hK) Then Exit Function

  RegQueryValueEx(hK, "KERNEL\CPUUsage", 0, 4, V, 4)
  RegCloseKey(hK)

  PushBandWidth V
  GetValue9x = m_BandWidth
End Function

Function GetValueNT() As Long
  Dim dblValue As Double
  Dim pdhStatus As Long

  ' definierten Counter aktualisieren
  PdhCollectQueryData(hQuery)
  dblValue = PdhVbGetDoubleCounterValue(hCounter, pdhStatus)

  ' Wert des Counters abfragen
  If (pdhStatus = PDH_CSTATUS_VALID_DATA) Or _
    (pdhStatus = PDH_CSTATUS_NEW_DATA) Then

    PushBandWidth CLng(dblValue)
    GetValueNT = m_BandWidth
  End If
End Function

Do
    Print "CPU: " & GetValueNT() & "%"
    If InKey = Chr(27) Then Exit Do
Loop