Page 1 of 1

Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 3:48 am
by Quin
I'm trying to use the PERFORMANCE_INFORMATION structure in my app, and getting an error that the structure's not found. I did a search on the forum, and found this. They posted it to the requested API structures and constants topic, but it doesn't seem to have ever been added. Either that, or it got removed at some point :?:

Re: Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 9:48 am
by Mesa

Code: Select all

;according to this post http://forums.purebasic.com/english/viewtopic.php?p=582637, 
;size_t is 8 bytes on windows and 4/8 on other os.


  CompilerIf #PB_Compiler_OS = #PB_OS_Windows
    
    Structure PERFORMANCE_INFORMATION
      cb.l
      CommitTotal.q
      CommitLimit.q
      CommitPeak.q
      PhysicalTotal.q
      PhysicalAvailable.q
      SystemCache.q
      KernelTotal.q
      KernelPaged.q
      KernelNonpaged.q
      PageSize.q
      HandleCount.l
      ProcessCount.l
      ThreadCount.l
    EndStructure
    
  CompilerElse
    
    CompilerIf #PB_Compiler_64Bit
      Structure PERFORMANCE_INFORMATION
        cb.l
        CommitTotal.q
        CommitLimit.q
        CommitPeak.q
        PhysicalTotal.q
        PhysicalAvailable.q
        SystemCache.q
        KernelTotal.q
        KernelPaged.q
        KernelNonpaged.q
        PageSize.q
        HandleCount.l
        ProcessCount.l
        ThreadCount.l
      EndStructure
      
    CompilerElse
      Structure PERFORMANCE_INFORMATION
        cb.l
        CommitTotal.l
        CommitLimit.l
        CommitPeak.l
        PhysicalTotal.l
        PhysicalAvailable.l
        SystemCache.l
        KernelTotal.l
        KernelPaged.l
        KernelNonpaged.l
        PageSize.l
        HandleCount.l
        ProcessCount.l
        ThreadCount.l
      EndStructure
    CompilerEndIf
  CompilerEndIf


  Define ok.PERFORMANCE_INFORMATION
  Debug SizeOf(ok\cb)
Debug SizeOf(ok\CommitTotal)
M.

Re: Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 10:05 am
by Axolotl
Hi there,

I fiddled around this stuff used the following links and came up with this stuff below
https://learn.microsoft.com/en-us/windo ... nformation
https://learn.microsoft.com/en-us/windo ... rmanceinfo

Maybe this can be a start

Code: Select all

Structure PERFORMANCE_INFORMATION Align 8  ; increases the size to the needed one. 
  cb.l                  ;
  CommitTotal.q         ;  .q is equal to .i on 64 bit (but makes it more clear to be 8 bytes long) 
  CommitLimit.q         ;
  CommitPeak.q          ; size_t is ULONG_PTR 
  PhysicalTotal.q       ;
  PhysicalAvailable.q   ;
  SystemCache.q         ;
  KernelTotal.q         ;
  KernelPaged.q         ;
  KernelNonpaged.q      ;
  PageSize.q            ;
  HandleCount.l         ;
  ProcessCount.l        ;
  ThreadCount.l         ;       
EndStructure 


Procedure ReadPerformanceInformation()   ; 
  Protected ret, *func, pi.PERFORMANCE_INFORMATION 

Debug SizeOf(pi)       ; size == 112 (otherwise we got error 24 (command length incorrect) 
  If OpenLibrary(0, "PSAPI.DLL") 
    *func = GetFunction(0, "GetPerformanceInfo")
    If *func
      pi\cb = SizeOf(PERFORMANCE_INFORMATION) 
      ret = CallFunctionFast(*func, @pi, SizeOf(PERFORMANCE_INFORMATION)) 
      If ret = 0 
        Debug " Error " + GetLastError_() 
      Else 
        Debug "Result " 
        Debug "  " + pi\PhysicalTotal     ; not sure this is correct, but it showed something !!! 
      EndIf 
    Else 
      Debug "No Function"
    EndIf
    CloseLibrary(0)
  EndIf

  ProcedureReturn ret 
EndProcedure 

ReadPerformanceInformation() 

Re: Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 10:25 am
by Axolotl
a second way of doing (tested on a 64bit system)

Code: Select all

Structure PERFORMANCE_INFORMATION Align 8  ; size == 112 bytes 
  cb.l                  ;
  CommitTotal.q         ;
  CommitLimit.q         ;
  CommitPeak.q          ; size_t is ULONG_PTR 
  PhysicalTotal.q       ;
  PhysicalAvailable.q   ;
  SystemCache.q         ;
  KernelTotal.q         ;
  KernelPaged.q         ;
  KernelNonpaged.q      ;
  PageSize.q            ;
  HandleCount.l         ;
  ProcessCount.l        ;
  ThreadCount.l         ;       
EndStructure 

Import ""
  GetPerformanceInfo_(*PI.PERFORMANCE_INFORMATION, cb.l) As "K32GetPerformanceInfo" 
EndImport 


Procedure ReadPerformanceInformation()   ; 
  Protected ret, *func, pi.PERFORMANCE_INFORMATION 

  pi\cb = SizeOf(PERFORMANCE_INFORMATION) 
  ret = GetPerformanceInfo_(@pi, SizeOf(PERFORMANCE_INFORMATION)) 
  If ret = 0 
    Debug " Error " + GetLastError_() 
  Else 
    Debug "Result " 
    Debug "  " + pi\PhysicalTotal 
  EndIf 

  ProcedureReturn ret 
EndProcedure 

ReadPerformanceInformation() 

Re: Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 3:09 pm
by Quin
Thanks all! :) got it to work

Re: Structure not found: PERFORMANCE_INFORMATION

Posted: Sat Jul 13, 2024 9:46 pm
by Fred
Moved to feature request, a missing API structure isn't a bug.