Structure not found: PERFORMANCE_INFORMATION

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Structure not found: PERFORMANCE_INFORMATION

Post 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 :?:
Mesa
Enthusiast
Enthusiast
Posts: 433
Joined: Fri Feb 24, 2012 10:19 am

Re: Structure not found: PERFORMANCE_INFORMATION

Post 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.
Axolotl
Addict
Addict
Posts: 804
Joined: Wed Dec 31, 2008 3:36 pm

Re: Structure not found: PERFORMANCE_INFORMATION

Post 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() 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Axolotl
Addict
Addict
Posts: 804
Joined: Wed Dec 31, 2008 3:36 pm

Re: Structure not found: PERFORMANCE_INFORMATION

Post 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() 
Just because it worked doesn't mean it works.
PureBasic 6.04 (x86) and <latest stable version and current alpha/beta> (x64) on Windows 11 Home. Now started with Linux (VM: Ubuntu 22.04).
Quin
Addict
Addict
Posts: 1131
Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:

Re: Structure not found: PERFORMANCE_INFORMATION

Post by Quin »

Thanks all! :) got it to work
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Structure not found: PERFORMANCE_INFORMATION

Post by Fred »

Moved to feature request, a missing API structure isn't a bug.
Post Reply