![]() |
VB.Net-Forum - Beitragsübersicht - |
|
Thema | V05: Visual Basic.NET und psapi.DLL (Prozessinfos) |
Von |
Cheffboss |
Datum |
23. Juli 2022 um 17:01:48 |
Frage |
Moin  Ich möchte gerne von einem beliebigen Prozess die Informationen wieviel Arbeitsspeicher dieser gerade verbraucht. Da mein Programm auch unter Windows XP ausgeführt werden muss, benötige ich einen Funktionierten Code. Die Lösung mit dem PerformanceCounter("Process", "Working Set - Private" ging leider nicht, weil XP dies nicht mehr unterstützt. Außerdem ist die Funktion auch sehr langsam… Es dauert mehre Sekunden bis, Werte angezeigt werden. Und WMI könnte mir leider auch nicht helfen. Da alle Werte die angezeigt werden, falsch sind.
Zum Glück habe ich ein Projekt mit dem Namen „Advanced Task Manager“ gefunden, dieses arbeitet mit der älteren psapi.DLL. Damit ist es mir nun möglich, die passenden Werte auszulesen. Auch unter Windows XP. Außerdem ist diese Funktion wirklich sehr schnell. Jedenfalls wenn ich das Programm starte, habe ich keine Wartezeiten.
Ich suche ich jemand, der das C++ Projekt anschaut und mir den Codeteil, den ich für meine Funktion brauche in VB.NET übersetzt.
Freue mich auf eure Hilfe. BIG THX
https://ibb.co/3c4KCXx
Quellcode zu diesem C++ Projekt:
https://easyupload.io/q25ifh
Datei von:
https://codeproject.com/Articles/12360/Advanced-Task-Manager-in-MFC
|
|
Antwort: |
Von |
Cheffboss |
Datum |
26. Juli 2022 um 21:06:39 |
Antwort |
Ich habe die Lösung nun gefunden, war nicht einfach. Auf Windows 2000 und XP erfolgreich getestet! Hier der Code für jeden mit dem gleichen Problem. Code:Option Strict On Imports System.Runtime.InteropServices Namespace ApiReference Class ApiExample Public Structure PROCESS_MEMORY_COUNTERS Public cb As UInt32 Public PageFaultCount As UInt32 Public PeakWorkingSetSize As UIntPtr Public WorkingSetSize As UIntPtr Public QuotaPeakPagedPoolUsage As UIntPtr Public QuotaPagedPoolUsage As UIntPtr Public QuotaPeakNonPagedPoolUsage As UIntPtr Public QuotaNonPagedPoolUsage As UIntPtr Public PagefileUsage As UIntPtr Public PeakPagefileUsage As UIntPtr End Structure Public Structure PROCESS_MEMORY_COUNTERS_EX Public cb As UInt32 Public PageFaultCount As UInt32 Public PeakWorkingSetSize As UIntPtr Public WorkingSetSize As UIntPtr Public QuotaPeakPagedPoolUsage As UIntPtr Public QuotaPagedPoolUsage As UIntPtr Public QuotaPeakNonPagedPoolUsage As UIntPtr Public QuotaNonPagedPoolUsage As UIntPtr Public PagefileUsage As UIntPtr Public PeakPagefileUsage As UIntPtr Public PrivateUsage As UIntPtr End Structure Public Structure COMPATIBLE_PROCESS_MEMORY_COUNTERS Public Sub New(pmc As PROCESS_MEMORY_COUNTERS) cb = pmc.cb PageFaultCount = pmc.PageFaultCount PeakWorkingSetSize = pmc.PeakWorkingSetSize WorkingSetSize = pmc.WorkingSetSize QuotaPeakPagedPoolUsage = pmc.QuotaPeakPagedPoolUsage QuotaPagedPoolUsage = pmc.QuotaPagedPoolUsage QuotaPeakNonPagedPoolUsage = pmc.QuotaPeakNonPagedPoolUsage QuotaNonPagedPoolUsage = pmc.QuotaNonPagedPoolUsage PagefileUsage = pmc.PagefileUsage PeakPagefileUsage = pmc.PeakPagefileUsage PrivateUsage = UIntPtr.Zero End Sub Public Sub New(pmcex As PROCESS_MEMORY_COUNTERS_EX) cb = pmcex.cb PageFaultCount = pmcex.PageFaultCount PeakWorkingSetSize = pmcex.PeakWorkingSetSize WorkingSetSize = pmcex.WorkingSetSize QuotaPeakPagedPoolUsage = pmcex.QuotaPeakPagedPoolUsage QuotaPagedPoolUsage = pmcex.QuotaPagedPoolUsage QuotaPeakNonPagedPoolUsage = pmcex.QuotaPeakNonPagedPoolUsage QuotaNonPagedPoolUsage = pmcex.QuotaNonPagedPoolUsage PagefileUsage = pmcex.PagefileUsage PeakPagefileUsage = pmcex.PeakPagefileUsage PrivateUsage = pmcex.PrivateUsage End Sub Public cb As UInt32 Public PageFaultCount As UInt32 Public PeakWorkingSetSize As UIntPtr Public WorkingSetSize As UIntPtr Public QuotaPeakPagedPoolUsage As UIntPtr Public QuotaPagedPoolUsage As UIntPtr Public QuotaPeakNonPagedPoolUsage As UIntPtr Public QuotaNonPagedPoolUsage As UIntPtr Public PagefileUsage As UIntPtr Public PeakPagefileUsage As UIntPtr Public PrivateUsage As UIntPtr End Structure <DllImport("psapi")> Public Shared Function GetProcessMemoryInfo(hProcess As IntPtr, ByRef cpmc As COMPATIBLE_PROCESS_MEMORY_COUNTERS, cb As Int32) As [Boolean] End Function Public Shared Sub Main(args As String()) Dim cpmc As COMPATIBLE_PROCESS_MEMORY_COUNTERS = Nothing Dim p As Process = Nothing For Each p In Process.GetProcessesByName("Notepad") ' <------- HIER DEN PROZESS EINTRAGEN! Dim Type As Int32 If Type = 0 Then cpmc.cb = CType(Marshal.SizeOf(GetType(PROCESS_MEMORY_COUNTERS)), UInt32) Else cpmc.cb = CType(Marshal.SizeOf(GetType(PROCESS_MEMORY_COUNTERS_EX)), UInt32) End If If GetProcessMemoryInfo(p.Handle, cpmc, CType(cpmc.cb, Int32)) Then Console.WriteLine("-------------------------------------") Console.WriteLine(p.ProcessName) Console.WriteLine("-------------------------------------") Console.WriteLine("PageFaultCount = {0}", cpmc.PageFaultCount) Console.WriteLine("PeakWorkingSetSize = {0}", cpmc.PeakWorkingSetSize) Console.WriteLine("WorkingSetSize = {0}", cpmc.WorkingSetSize) Console.WriteLine("QuotaPeakPagedPoolUsage = {0}", cpmc.QuotaPeakPagedPoolUsage) Console.WriteLine("QuotaPagedPoolUsage = {0}", cpmc.QuotaPagedPoolUsage) Console.WriteLine("QuotaPeakNonPagedPoolUsage = {0}", cpmc.QuotaPeakNonPagedPoolUsage) Console.WriteLine("QuotaNonPagedPoolUsage = {0}", cpmc.QuotaNonPagedPoolUsage) Console.WriteLine("PagefileUsage = {0}", cpmc.PagefileUsage) Console.WriteLine("PeakPagefileUsage = {0}", cpmc.PeakPagefileUsage) Console.WriteLine("PrivateUsage = {0}", cpmc.PrivateUsage) End If Next If p Is Nothing Then Console.WriteLine("Prozess nicht gefunden!") Console.ReadKey() End Sub End Class End Namespace |
|
[ Antwort schreiben | Zurück zum VB.Net-Forum | Forum-Hilfe ] |
|
Letzte Aktualisierung: Sonntag, 13. Dezember 2015 |
|