Get PowerScheme Procedure

Share your advanced PureBasic knowledge/code with the community.
roachofdeath
User
User
Posts: 78
Joined: Sun Apr 24, 2005 3:22 am

Get PowerScheme Procedure

Post by roachofdeath »

Code updated For 5.20+

this is a very simple procedure, but works nonetheless.
It finds the power scheme name and description, even if you have a custom scheme.

Code: Select all

Procedure.s Reg_GetValue(topKey, sKeyName.s, sValueName.s, ComputerName.s)
  lpData.s ;Reg_GetValue() by NoahPhense
  GetValue.s =""
  If Left(sKeyName, 1) = "\"
    sKeyName = Right(sKeyName, Len(sKeyName) - 1)
  EndIf
  If ComputerName = ""
    GetHandle = RegOpenKeyEx_(topKey, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  Else
    lReturnCode = RegConnectRegistry_(ComputerName, topKey, @lhRemoteRegistry)
    GetHandle = RegOpenKeyEx_(lhRemoteRegistry, sKeyName, 0, #KEY_ALL_ACCESS, @hKey)
  EndIf
  If GetHandle = #ERROR_SUCCESS
    lpcbData = 255
    lpData = Space(255)
    GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
    If GetHandle = #ERROR_SUCCESS
      Select lType
        Case #REG_SZ
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lType, @lpData, @lpcbData)
          If GetHandle = 0
            GetValue = Left(lpData, lpcbData - 1)
          Else
            GetValue = ""
          EndIf
        Case #REG_DWORD
          GetHandle = RegQueryValueEx_(hKey, sValueName, 0, @lpType, @lpDataDWORD, @lpcbData)
          If GetHandle = 0
            GetValue = Str(lpDataDWORD)
          Else
            GetValue = "0"
          EndIf
      EndSelect
    EndIf
  EndIf
  RegCloseKey_(hKey)
  ProcedureReturn GetValue
EndProcedure

Procedure.s GetPowerScheme(ret)
  buff_sel$   = Reg_GetValue(#HKEY_CURRENT_USER, "Control Panel\PowerCfg", "CurrentPowerPolicy", "")
  If ret = 0
    buff_title$ = Reg_GetValue(#HKEY_CURRENT_USER,"Control Panel\PowerCfg\PowerPolicies\"+buff_sel$,"Name","")
    ProcedureReturn buff_title$
  Else
    buff_descr$ = Reg_GetValue(#HKEY_CURRENT_USER,"Control Panel\PowerCfg\PowerPolicies\"+buff_sel$,"Description","")
    ProcedureReturn buff_descr$
  EndIf
EndProcedure

;---[EXAMPLE]---;

If OpenWindow(0,100,100,50,105,"Power Scheme",0)
  
  StringGadget(1,5,5,90,20,"")
  EditorGadget(2,5,25,90,50)
  ButtonGadget(3,5,75,90,20,"Display")
  
  Repeat
    Select WaitWindowEvent()
      Case #PB_Event_CloseWindow: End
      Case #PB_Event_Gadget
        Select EventGadget()
          Case 3
            SetGadgetText(1,GetPowerScheme(0))
            SetGadgetText(2,GetPowerScheme(1))
        EndSelect
    EndSelect
  ForEver
  
EndIf