Help With CallBack

Just starting out? Need help? Post your questions and find answers here.
kruddick
User
User
Posts: 11
Joined: Mon Mar 08, 2004 1:15 am

Help With CallBack

Post by kruddick »

Hello,

I am having some problems with the following code. The code runs but I only get the first letter of the strings returned. Not sure how to get the rest of the strings. And everything I try to access the PP structure seems to fail. Any help or pointers would be appreciated.

Thanks.

Code: Select all

#EnableSysTrayBatteryMeter = $01
#EnableMultiBatteryDisplay = $02
#EnablePasswordLogon = $04
#EnableWakeOnRing = $08
#EnableVideoDimDisplay = $10
#NEWSCHEME = -1
#NTSTATUS = #LONG
#NUM_DISCHARGE_POLICIES = 4

Structure PROCESSOR_POWER_POLICY_INFO
  TimeCheck.l
  DemoteLimit.l
  PromoteLimit.l
  DemotePercent.b
  PromotePercent.b
  Spare.b[2]
  AllowDemotion:1.l
  AllowPromotion:1.l
  Reserved:30.l
EndStructure

Structure PROCESSOR_POWER_POLICY
  Revision.l
  DynamicThrottle.b
  Spare.b[3]
  DisableCStates:1.l
  Reserved1:31.l
  PolicyCount.l
  Policy.PROCESSOR_POWER_POLICY_INFO[3]
EndStructure

Structure ADMINISTRATOR_POWER_POLICY
  MinSleep.l
  MaxSleep.l
  MinVideoTimeout.l
  MaxVideoTimeout.l
  MinSpindownTimeout.l
  MaxSpindownTimeout.l
EndStructure

Structure BATTERY_REPORTING_SCALE
  Granularity.l
  Capacity.l
EndStructure

Structure SYSTEM_POWER_CAPABILITIES
  PowerButtonPresent.b
  SleepButtonPresent.b
  LidPresent.b
  SystemS1.b
  SystemS2.b
  SystemS3.b
  SystemS4.b
  SystemS5.b
  HiberFilePresent.b
  FullWake.b
  VideoDimPresent.b
  ApmPresent.b
  UpsPresent.b
  ThermalControl.b
  ProcessorThrottle.b
  ProcessorMinThrottle.b
  ProcessorMaxThrottle.b
  spare2.b[4]
  DiskSpinDown.b
  spare3.b[8]
  SystemBatteriesPresent.b
  BatteriesAreShortTerm.b
  BatteryScale.BATTERY_REPORTING_SCALE[3]
  AcOnLineWake.l
  SoftLidWake.l
  RtcWake.l
  MinDeviceWakeState.l
  DefaultLowLatencyWake.l
EndStructure

Structure SYSTEM_BATTERY_STATE
  AcOnLine.b
  BatteryPresent.b
  Charging.b
  Discharging.b
  Spare1.b[4]
  MaxCapacity.l
  RemainingCapacity.l
  Rate.l
  EstimatedTime.l
  DefaultAlert1.l
  DefaultAlert2.l
EndStructure

Structure POWER_ACTION_POLICY
  Action.l
  Flags.l
  EventCode.l
EndStructure

Structure SYSTEM_POWER_LEVEL
  Enable.b
  Spare.b[3]
  BatteryLevel.l
  PowerPolicy.POWER_ACTION_POLICY
  MinSystemState.l
EndStructure

Structure GLOBAL_MACHINE_POWER_POLICY
  Revision.l
  LidOpenWakeAc.l
  LidOpenWakeDc.l
  BroadcastCapacityResolution.l
EndStructure

Structure GLOBAL_USER_POWER_POLICY
  Revision.l
  PowerButtonAc.POWER_ACTION_POLICY
  PowerButtonDc.POWER_ACTION_POLICY
  SleepButtonAc.POWER_ACTION_POLICY
  SleepButtonDc.POWER_ACTION_POLICY
  LidCloseAc.POWER_ACTION_POLICY
  LidCloseDc.POWER_ACTION_POLICY
  DischargePolicy.SYSTEM_POWER_LEVEL[#NUM_DISCHARGE_POLICIES]
  GlobalFlags.l
EndStructure

Structure GLOBAL_POWER_POLICY
  user.GLOBAL_USER_POWER_POLICY
  mach.GLOBAL_MACHINE_POWER_POLICY
EndStructure

Structure MACHINE_POWER_POLICY
  Revision.l
  MinSleepAc.l
  MinSleepDc.l
  ReducedLatencySleepAc.l
  ReducedLatencySleepDc.l
  DozeTimeoutAc.l
  DozeTimeoutDc.l
  DozeS4TimeoutAc.l
  DozeS4TimeoutDc.l
  MinThrottleAc.b
  MinThrottleDc.b
  pad1.b[2]
  OverThrottledAc.POWER_ACTION_POLICY
  OverThrottledDc.POWER_ACTION_POLICY
EndStructure

Structure MACHINE_PROCESSOR_POWER_POLICY
  Revision.l
  ProcessorPolicyAc.PROCESSOR_POWER_POLICY
  ProcessorPolicyDc.PROCESSOR_POWER_POLICY
EndStructure

Structure USER_POWER_POLICY
  Revision.l
  IdleAc.POWER_ACTION_POLICY
  IdleDc.POWER_ACTION_POLICY
  IdleTimeoutAc.l
  IdleTimeoutDc.l
  IdleSensitivityAc.b
  IdleSensitivityDc.b
  ThrottlePolicyAc.b
  ThrottlePolicyDc.b
  MaxSleepAc.l
  MaxSleepDc.l
  Reserved.l[2]
  VideoTimeoutAc.l
  VideoTimeoutDc.l
  SpindownTimeoutAc.l
  SpindownTimeoutDc.l
  OptimizeForPowerAc.b
  OptimizeForPowerDc.b
  FanThrottleToleranceAc.b
  FanThrottleToleranceDc.b
  ForcedThrottleAc.b
  ForcedThrottleDc.b
EndStructure

Structure POWER_POLICY
  user.USER_POWER_POLICY
  mach.MACHINE_POWER_POLICY
EndStructure

*PP.POWER_POLICY

Procedure.l EnumPowerPolicy(Index.l, dwName.w, sName.s, dwDesc.w, sDesc.s, PP, UserDefine.l)
  MessageRequester("",sName.s)

ProcedureReturn 1
EndProcedure

OpenLibrary(1,"powrprof.dll"); XP path C:\Windows\system32\powrprof.dll
Result = CallFunction(1, "EnumPwrSchemes", @EnumPowerPolicy(), 1) 
CloseLibrary(1)
I have tried things like @PP\user\VideoTimeoutAc, *PP\user\VideoTimeoutAc

Not sure... I get a message that this variable does not have a structure.

Thanks Again.

Kent
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Change your code from:

Code: Select all

...

*PP.POWER_POLICY

Procedure.l EnumPowerPolicy(Index.l, dwName.w, sName.s, dwDesc.w, sDesc.s, PP, UserDefine.l)
  MessageRequester("",sName.s)

ProcedureReturn 1
EndProcedure 

...
...to:

Code: Select all

...

Procedure.l EnumPowerPolicy(Index.l, dwName.w, sName.s, dwDesc.w, sDesc.s, *PP.POWER_POLICY, UserDefine.l)
  ; ===> *PP is only valid inside the Procedure
  MessageRequester("",sName.s + " - " + Str(*PP\user\VideoTimeoutAc) )
  ProcedureReturn 1
EndProcedure 

...
kruddick
User
User
Posts: 11
Joined: Mon Mar 08, 2004 1:15 am

Post by kruddick »

Helpy Thanks. That worked correctly. Do you know why I am only getting the first letter of the string sName.s? I know there is a length value, but unsure why it is getting truncated.

Thanks again for showing me the way...

Kent
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Hi Kent,

Maybe the name is stored in Unicode. Do you have any documentation?

cu
kruddick
User
User
Posts: 11
Joined: Mon Mar 08, 2004 1:15 am

Post by kruddick »

Helpy,

You are right. I just checked the SDK:

The sName and sDesc parameters are null-terminated strings; they are Unicode strings on Windows 2000/XP and ANSI strings on Windows Me/98/95.

How do I get around this... I will try a search on the boards.

Much Appreciated.
Kent
AlGonzalez
User
User
Posts: 53
Joined: Sun Feb 15, 2004 6:04 am
Location: Easley, SC, USA

Post by AlGonzalez »

MSDN wrote:Converting Unicode and ANSI Strings

Active Accessibility uses Unicode strings as defined by the BSTR data type. If your application does not use Unicode strings, or if you want to convert strings for certain API calls, use the MultiByteToWideChar and WideCharToMultiByte Microsoft® Win32® functions to perform the necessary conversion.

Use WideCharToMultiByte to convert a Unicode string to an ANSI string. The MultiByteToWideChar function converts an ANSI string to a Unicode string.

Use SysAllocString and SysFreeString to allocate and free BSTR data types.

For more information about these string functions, see their references in the Platform SDK.
HTH
kruddick
User
User
Posts: 11
Joined: Mon Mar 08, 2004 1:15 am

Post by kruddick »

I've been looking at that function, but I am getting jibberish from the conversion...

Procedure.l EnumPowerPolicy(Index.l, dwName.l, sName2.s, dwDesc.l, sDesc.s, *PP.POWER_POLICY, UserDefine.l)
;MessageRequester("",StrU(Index.l, #LONG)+ " " + sName.s)
sName.s = Space(dwName.l)
WideCharToMultiByte_(0,0,sName2.s,dwName.l,sName.s,dwName.l,0,0)
MessageRequester("",sName.s + " - " + Str(*PP\user\VideoTimeoutAc / 60) )
ProcedureReturn 1
EndProcedure

I looked at some of the examples in the forum but none of the ones I have tried work for me...

I appreciate the resonse.

Kent
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

The following code worked for me under Windows 2000:

Code: Select all

Procedure.l EnumPowerPolicy(uiIndex.l, dwName.l, *sName.l, dwDesc.l, *sDesc.l, *pp.POWER_POLICY, UserDefine.l)
  Protected Name.s, Desc.s
  ; ===> *PP is only valid inside the Procedure
  ;MessageRequester("",sName.s + " - " + Str(*pp\user\VideoTimeoutAc) )
  Name = Space(dwName)
  WideCharToMultiByte_( 0, 0, *sName, dwName, @Name, dwName, #NULL, #NULL )
  Debug Str(dwName ) + " :: " + Name
  Desc = Space(dwDesc)
  WideCharToMultiByte_( 0, 0, *sDesc, dwDesc, @Desc, dwDesc, #NULL, #NULL )
  Debug Str(dwDesc) + " :: " + Desc
  Debug "--"
  ProcedureReturn 1
EndProcedure 
cu, helpy
kruddick
User
User
Posts: 11
Joined: Mon Mar 08, 2004 1:15 am

Post by kruddick »

Thanks Helpy.

It worked on XP also. Not sure how you knew to change from string to pointer, but I really appreciate that you fixed my code.

I really appreciate your efforts.

Kent
User avatar
NoahPhense
Addict
Addict
Posts: 1999
Joined: Thu Oct 16, 2003 8:30 pm
Location: North Florida

Re: Help With CallBack

Post by NoahPhense »

Hey Kent:

That's a sweet piece of code you got there. You don't seem to have any
problems converting the structures from the msdn.

I would love to pick your brain sometime referencing api.

All of my IM's are listed below, contact me, we'll yap...

- np
Post Reply