Solved

Calling ReadPwrScheme from powrprof.dll

Posted on 2004-10-19
4
1,609 Views
Last Modified: 2008-01-09
Hello,

I am getting exception errors when I call the ReadPwrScheme function from the powrprof.dll. I have the converted header file that defines all the structures.

Here is the code:
var
   uiIndex: UINT;
   CurrentPP : POWER_POLICY;
   
begin

   GetActivePwrScheme(@uiIndex);
   FormMain.Memo.Lines.Append(IntToStr(uiIndex));

   ZeroMemory(@CurrentPP, SizeOf(CurrentPP));
   ReadPwrScheme(uiIndex, @CurrentPP);            <----- Fails here.

end;


Here is one of the structures and the function definitions:

   POWER_POLICY = record
      user: USER_POWER_POLICY;
      mach: MACHINE_POWER_POLICY;
   end;
   PPOWER_POLICY = ^POWER_POLICY;

function ReadPwrScheme(uiID: UINT; pPowerPolicy: PPOWER_POLICY): boolean; stdcall;

function ReadPwrScheme(uiID: UINT; pPowerPolicy: PPOWER_POLICY): boolean;        external 'powrprof.dll' name 'ReadPwrScheme';

The error message says:
Project POWERPROF.EXE raised exception class EAccessViolation with message 'Access violation at address 74AD295F in module 'powrprof.dll'. Write of address 00000302, Process stopped. Use Step or Run to continue.

I would appreciate any help provided that can lead me to a solution.

Thanks
Kent




0
Comment
Question by:KentDRuddick
  • 2
  • 2
4 Comments
 
LVL 1

Accepted Solution

by:
vacerose earned 250 total points
ID: 12355530
Have you compared the size of your converted POWER_POLICY structure in Delphi to the size in C to make sure there are no header conversion issues.  The structure size should be 144 bytes.  80 for USER_POWER_POLICY and 64 for MACHINE_POWER_POLICY...
0
 

Author Comment

by:KentDRuddick
ID: 12357647
You are correct about the sizes being wrong. I get 128, 76, 52. So how do I correct the problem?

I didn't do the header conversion. I got it from the Borland site. Credit to David Caouette. I have compared it to the jwaPowrProf.pas and the SDK Description and they both look the same. How do I fix the size problem?

Can you help me just a little longer?
Thanks
Kent

Here is the header:

unit powrprof;

interface

uses
   Windows, Messages;

const
   POWER_ACTION_CRITICAL = 1;           //Forces a critical suspension.
   POWER_ACTION_DISABLE_WAKES = 2;      //Disables all wake events.
   POWER_ACTION_LIGHTEST_FIRST = 4;     //Uses the first lightest available sleep state.
   POWER_ACTION_LOCK_CONSOLE = 8 ;      //Requires entry of the system password upon resume from one of the system standby states.
   POWER_ACTION_OVERRIDE_APPS = 16;     //Ignores applications that do not respond to the PBT_APMQUERYSUSPEND event broadcast in the WM_POWERBROADCAST message.
   POWER_ACTION_QUERY_ALLOWED = 32;     //Broadcasts a PBT_APMQUERYSUSPEND event to each application to request permission to suspend operation.
   POWER_ACTION_UI_ALLOWED = 64;

   POWER_FORCE_TRIGGER_RESET = 1;       //Clears a user power button press.
   POWER_LEVEL_USER_NOTIFY_EXEC = 2;    //Specifies a program to be executed.
   POWER_LEVEL_USER_NOTIFY_SOUND = 4;   //User notified using sound.
   POWER_LEVEL_USER_NOTIFY_TEXT = 8;    //User notified using the UI.
   POWER_USER_NOTIFY_BUTTON = 16;       //Indicates that the power action is in response to a user power button press.
   POWER_USER_NOTIFY_SHUTDOWN = 32;

type

// =================== ENUMERATIONS ===================

   SYSTEM_POWER_STATE =(PowerSystemUnspecified, PowerSystemWorking, PowerSystemSleeping1,
      PowerSystemSleeping2, PowerSystemSleeping3, PowerSystemHibernate,
      PowerSystemShutdown, PowerSystemMaximum);

   POWER_ACTION = (PowerActionNone, PowerActionReserved, PowerActionSleep, PowerActionHibernate,
      PowerActionShutdown, PowerActionShutdownReset, PowerActionShutdownOff, PowerActionWarmEject);

   POWER_INFORMATION_LEVEL = (AdministratorPowerPolicy, LastSleepTime, LastWakeTime, ProcessorInformation, ProcessorPowerPolicyAc,
      ProcessorPowerPolicyCurrent, ProcessorPowerPolicyDc, SystemBatteryState, SystemExecutionState, SystemPowerCapabilities,
      SystemPowerInformation, SystemPowerPolicyAc, SystemPowerPolicyCurrent, SystemPowerPolicyDc, SystemReserveHiberFile,
      VerifyProcessorPowerPolicyAc, VerifyProcessorPowerPolicyDc, VerifySystemPolicyAc, VerifySystemPolicyDc);

// =================== STRUCTURES ===================

   POWER_ACTION_POLICY = record
      Action: POWER_ACTION;
      Flags: ULONG;
      EventCode: ULONG;
   end;
   PPOWER_ACTION_POLICY = ^POWER_ACTION_POLICY;

   SYSTEM_POWER_LEVEL = record
      Enable: boolean;
      Spare: array[0..2] of UCHAR;
      BatteryLevel: ULONG;
      PowerPolicy: POWER_ACTION_POLICY;
      MinSystemState: SYSTEM_POWER_STATE;
   end;
   PSYSTEM_POWER_LEVEL = ^SYSTEM_POWER_LEVEL;

   BATTERY_REPORTING_SCALE = record
      Granularity: ULONG;
      Capacity: ULONG;
   end;
   PBATTERY_REPORTING_SCALE = ^BATTERY_REPORTING_SCALE;

   SYSTEM_POWER_CAPABILITIES = record
      PowerButtonPresent: boolean;
      SleepButtonPresent: boolean;
      LidPresent: boolean;
      SystemS1: boolean;
      SystemS2: boolean;
      SystemS3: boolean;
      SystemS4: boolean;
      SystemS5: boolean;
      HiberFilePresent: boolean;
      FullWake: boolean;
      VideoDimPresent: boolean;
      ApmPresent: boolean;
      UpsPresent: boolean;
      ThermalControl: boolean;
      ProcessorThrottle: boolean;
      ProcessorMinThrottle: UCHAR;
      ProcessorMaxThrottle: UCHAR;
      spare2: array[0..3] of UCHAR;
      DiskSpinDown: boolean;
      spare3: array[0..7] of UCHAR;
      SystemBatteriesPresent: boolean;
      BatteriesAreShortTerm: boolean;
      BatteryScale: array[0..2] of BATTERY_REPORTING_SCALE;
      AcOnLineWake: SYSTEM_POWER_STATE;
      SoftLidWake: SYSTEM_POWER_STATE;
      RtcWake: SYSTEM_POWER_STATE;
      MinDeviceWakeState: SYSTEM_POWER_STATE;
      DefaultLowLatencyWake: SYSTEM_POWER_STATE;
   end;
   PSYSTEM_POWER_CAPABILITIES = ^SYSTEM_POWER_CAPABILITIES;

   USER_POWER_POLICY = record
      Revision: ULONG;
      IdleAc: POWER_ACTION_POLICY;
      IdleDc: POWER_ACTION_POLICY;
      IdleTimeoutAc: ULONG;
      IdleTimeoutDc: ULONG;
      IdleSensitivityAc: UCHAR;
      IdleSensitivityDc: UCHAR;
      ThrottlePolicyAc: UCHAR;
      ThrottlePolicyDc: UCHAR;
      MaxSleepAc: SYSTEM_POWER_STATE;
      MaxSleepDc: SYSTEM_POWER_STATE;
      Reserved: array[0..1] of ULONG;
      VideoTimeoutAc: ULONG;
      VideoTimeoutDc: ULONG;
      SpindownTimeoutAc: ULONG;
      SpindownTimeoutDc: ULONG;
      OptimizeForPowerAc: boolean;
      OptimizeForPowerDc: boolean;
      FanThrottleToleranceAc: UCHAR;
      FanThrottleToleranceDc: UCHAR;
      ForcedThrottleAc: UCHAR;
      ForcedThrottleDc: UCHAR;
   end;
   PUSER_POWER_POLICY = USER_POWER_POLICY;

   GLOBAL_USER_POWER_POLICY = record
      Revision: ULONG;
      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: array of SYSTEM_POWER_LEVEL;
      GlobalFlags: ULONG;
   end;
   PGLOBAL_USER_POWER_POLICY = ^GLOBAL_USER_POWER_POLICY;

   MACHINE_POWER_POLICY = record
      Revision: ULONG;
      MinSleepAc: SYSTEM_POWER_STATE;
      MinSleepDc: SYSTEM_POWER_STATE;
      ReducedLatencySleepAc: SYSTEM_POWER_STATE;
      ReducedLatencySleepDc: SYSTEM_POWER_STATE;
      DozeTimeoutAc: ULONG;
      DozeTimeoutDc: ULONG;
      DozeS4TimeoutAc: ULONG;
      DozeS4TimeoutDc: ULONG;
      MinThrottleAc: UCHAR;
      MinThrottleDc: UCHAR;
      pad1: array[0..1] of UCHAR;
      OverThrottledAc: POWER_ACTION_POLICY;
      OverThrottledDc: POWER_ACTION_POLICY;
   end;
   PMACHINE_POWER_POLICY = ^MACHINE_POWER_POLICY;

   GLOBAL_MACHINE_POWER_POLICY = record
      Revision: ULONG;
      LidOpenWakeAc: SYSTEM_POWER_STATE;
      LidOpenWakeDc: SYSTEM_POWER_STATE;
      BroadcastCapacityResolution: ULONG;
   end;
   PGLOBAL_MACHINE_POWER_POLICY = ^GLOBAL_MACHINE_POWER_POLICY;

   POWER_POLICY = record
      user: USER_POWER_POLICY;
      mach: MACHINE_POWER_POLICY;
   end;
   PPOWER_POLICY = ^POWER_POLICY;

   GLOBAL_POWER_POLICY = record
      user: GLOBAL_USER_POWER_POLICY;
      mach: GLOBAL_MACHINE_POWER_POLICY;
   end;
   PGLOBAL_POWER_POLICY = ^GLOBAL_POWER_POLICY;

   PWRSCHEMESENUMPROC = FARPROC;
   (*
      typedef BOOLEAN (CALLBACK* PWRSCHEMESENUMPROC)(
         UINT uiIndex,      // power scheme index
         DWORD dwName,      // size of the sName string, in bytes
         LPTSTR sName,      // name of the power scheme
         DWORD dwDesc,      // size of the sDesc string, in bytes
         LPTSTR sDesc,      // description string
         PPOWER_POLICY pp,  // receives the power policy
         LPARAM lParam      // user-defined value );
   *)

// =================== FUNCTIONS ===================

function CallNtPowerInformation(InformationLevel: POWER_INFORMATION_LEVEL;
   lpInputBuffer: Pointer;
   nInputBufferSize: ULONG;
   lpOutputBuffer: Pointer;
   nOutputBufferSize: ULONG): cardinal; stdcall;
function CanUserWritePwrScheme: boolean; stdcall;
function DeletePwrScheme(uiIndex: UINT): boolean; stdcall;
function EnumPwrSchemes(EnumPwrSchemesProc: PWRSCHEMESENUMPROC; lParam: LPARAM): boolean; stdcall;
function GetActivePwrScheme(uiIndex: PUINT): boolean; stdcall;
function GetCurrentPowerPolicies(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY; pPowerPolicy: PPOWER_POLICY): boolean; stdcall;
function GetPwrCapabilities(lpSystemPowerCapabilities: PSYSTEM_POWER_CAPABILITIES): boolean; stdcall;
function GetPwrDiskSpindownRange(RangeMax: PUINT; RangeMin: PUINT): boolean; stdcall;
function IsAdminOverrideActive: boolean; stdcall;
function IsPwrHibernateAllowed: boolean; stdcall;
function IsPwrShutdownAllowed: boolean; stdcall;
function IsPwrSuspendAllowed: boolean; stdcall;
//function LoadCurrentPwrScheme: boolean; stdcall;
//         16    F 00002608 MergeLegacyPwrScheme
function ReadGlobalPwrPolicy(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY): boolean; stdcall;
function ReadPwrScheme(uiID: UINT; pPowerPolicy: PPOWER_POLICY): boolean; stdcall;
function SetActivePwrScheme(uiID: UINT; lpGlobalPowerPolicy: PGLOBAL_POWER_POLICY; lpPowerPolicy: PPOWER_POLICY): boolean; stdcall;
function SetSuspendState(Hibernate: boolean; ForceCritical: boolean; DisableWakeEvents: boolean): boolean; stdcall;
//         21   14 0000279E ValidatePowerPolicies
function WriteGlobalPwrPolicy(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY): boolean; stdcall;
function WritePwrScheme(uiID: UINT; lpszName: LPSTR; lpszDescription: LPSTR; pPowerPolicy: PPOWER_POLICY): boolean; stdcall;

implementation

function CallNtPowerInformation(InformationLevel: POWER_INFORMATION_LEVEL;
   lpInputBuffer: Pointer;
   nInputBufferSize: ULONG;
   lpOutputBuffer: Pointer;
   nOutputBufferSize: ULONG): cardinal;            external 'powrprof.dll' name 'CallNtPowerInformation';
function CanUserWritePwrScheme: boolean;           external 'powrprof.dll' name 'CanUserWritePwrScheme';
function DeletePwrScheme(uiIndex: UINT): boolean;  external 'powrprof.dll' name 'DeletePwrScheme';
function EnumPwrSchemes(EnumPwrSchemesProc: PWRSCHEMESENUMPROC; lParam: LPARAM): boolean;    external 'powrprof.dll' name 'EnumPwrSchemes';
function GetActivePwrScheme(uiIndex: PUINT): boolean;                                        external 'powrprof.dll' name 'GetActivePwrScheme';
function GetCurrentPowerPolicies(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY; pPowerPolicy: PPOWER_POLICY): boolean; external 'powrprof.dll' name 'GetCurrentPowerPolicies';
function GetPwrCapabilities(lpSystemPowerCapabilities: PSYSTEM_POWER_CAPABILITIES): boolean; external 'powrprof.dll' name 'GetPwrCapabilities';
function GetPwrDiskSpindownRange(RangeMax: PUINT; RangeMin: PUINT): boolean;                 external 'powrprof.dll' name 'GetPwrDiskSpindownRange';
function IsAdminOverrideActive: boolean;           external 'powrprof.dll' name 'IsAdminOverrideActive';
function IsPwrHibernateAllowed: boolean;           external 'powrprof.dll' name 'IsPwrHibernateAllowed';
function IsPwrShutdownAllowed: boolean;            external 'powrprof.dll' name 'IsPwrShutdownAllowed';
function IsPwrSuspendAllowed: boolean;             external 'powrprof.dll' name 'IsPwrSuspendAllowed';
//function LoadCurrentPwrScheme:boolean; external 'powrprof.dll' name 'LoadCurrentPwrScheme';
//         16    F 00002608 MergeLegacyPwrScheme
function ReadGlobalPwrPolicy(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY): boolean; external 'powrprof.dll' name 'ReadGlobalPwrPolicy';
function ReadPwrScheme(uiID: UINT; pPowerPolicy: PPOWER_POLICY): boolean;        external 'powrprof.dll' name 'ReadPwrScheme';
function SetActivePwrScheme(uiID: UINT; lpGlobalPowerPolicy: PGLOBAL_POWER_POLICY; lpPowerPolicy: PPOWER_POLICY): boolean; external 'powrprof.dll' name 'SetActivePwrScheme';
function SetSuspendState(Hibernate: boolean; ForceCritical: boolean; DisableWakeEvents: boolean): boolean;                 external 'powrprof.dll' name 'SetSuspendState';
//         21   14 0000279E ValidatePowerPolicies
function WriteGlobalPwrPolicy(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY): boolean; external 'powrprof.dll' name 'WriteGlobalPwrPolicy';
function WritePwrScheme(uiID: UINT; lpszName: LPSTR; lpszDescription: LPSTR; pPowerPolicy: PPOWER_POLICY): boolean; external 'powrprof.dll' name 'WritePwrScheme';


end.  
0
 
LVL 1

Expert Comment

by:vacerose
ID: 12359107
Ok, the header problem is with the enumerated types.  In Delphi, the default size for enumerated types with less than 256 values is 1 byte, the dll is expecting a size of 4 bytes.  Adding the following to the header file will take care of the size problem.

At the top of the file add the {$Z4} compiler directive to get 4 byte enumerated types

uses
   Windows, Messages;

{$Z4}

const
   POWER_ACTION_CRITICAL = 1;           //Forces a critical suspension.


At the bottom of the file, change the size back to 1 byte to prevent possible incompatibilities with other parts of your app.

function WriteGlobalPwrPolicy(pGlobalPowerPolicy: PGLOBAL_POWER_POLICY): boolean; external 'powrprof.dll' name 'WriteGlobalPwrPolicy';
function WritePwrScheme(uiID: UINT; lpszName: LPSTR; lpszDescription: LPSTR; pPowerPolicy: PPOWER_POLICY): boolean; external 'powrprof.dll' name 'WritePwrScheme';

{$Z1}

end.

This will get the sizeof readings to be identical.  Hope it fixes the problem.
0
 

Author Comment

by:KentDRuddick
ID: 12362449
I haven't had time to try it yet, but I thank you for showing me the way. :)

Kent
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now