Change PowerScheme/ Power profile in Windows XP and Windows 7 programming with Delphi 6


I am developping a delphi application (borland delphi 6).
My application is a windows service. I launch a cmd/bat file for doing this:

powercfg.exe /create myenergy

powercfg.exe /change myenergy /monitor-timeout-ac %1
powercfg.exe /change myenergy /monitor-timeout-dc %2

powercfg.exe /change myenergy /disk-timeout-ac %3
powercfg.exe /change myenergy /disk-timeout-dc %4

powercfg.exe /change myenergy /standby-timeout-ac %5
powercfg.exe /change upvenergy /standby-timeout-dc %6

powercfg.exe /hibernate on
powercfg.exe /change myenergy /hibernate-timeout-ac %7
powercfg.exe /change myenergy /hibernate-timeout-dc %8

powercfg.exe /setactive myenergy

This configuration is applied to computer without problems when I run it as a bat/cmd script from system prompt (cmd.exe), but when my service is running and launch the batch file in background, it doesn't nothing.

I launch:
cmd /c "c:\myapplication\script.cmd 10 5 20 10 15 10 25 20". If I launch "calc.exe", the process appears loaded into memory (in background, I can't interact with it).

I know that in Windows XP and Windows 2000 is possible invoke pwrprof.dll library functions:


In Windows SDK for Vista and Windows 7, the funcions are others.

But I don't know how use these routines for creating a new user power profile. I need fix monitor timeout, disk timeout, standby timeout and suspend timeout for each user log in system.

The experts-exchange site contains code for hibernating computer, but I haven't found examples for changing power scheme of logged user (I need the all right code for both systems: XP and Vista/Windows 7

operating systems).

Thank you very much.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

BrunoyAuthor Commented:
I need borland delphi code for changing the hard disk, monitor, suspend and hibernation time.

I prefer resolve the problem without use powercfg.exe because this tool is different in Windows XP and Windows Vista / 7. Theremore Windows 2000 doesn't include it.

My problem is about programming, and access pwrprof.dll.

Thanks in advance.
Did you look into these documents? They all are about programming. I know Delphi is not the most favorite Microsoft language but porting samples should be easy.
Of course you can wait for ready solution. It's your choice.
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

BrunoyAuthor Commented:
Link One: ACPI / Power Management - Architecture and Driver Support

Link Two: Application Power Management Best Practices for Windows Vista

Your links are too generic.

I don't know porting samples to Delphi 6. Theremore, for invocating pwrprof.dll functions is necessary declare constants, types and externals declarations of procedures and functions. I don't know invocating external dlls. I always have created account appplications and no system applications.

Thank you.
BrunoyAuthor Commented:
I know now the reason.

My script is all right, but I was changing power scheme / power profile of user that runs my service: LOCAL SYSTEM and no of logged user.

I need launch my batch as logged user if I want change his power scheme / power profile.

I'm probing CreateProcessAsUser function, but I am not getting a solution.

The best solution is programming a new power scheme with Windows API and not launching a batch file. So, my question will be open.

But, if I want change power profile of user, I need impersonate him. Naturally I haven´t his password.
This problem is opened as other question.

Thank you very much.
BrunoyAuthor Commented:
Finally, I have impersonated logged user.

I will find this solution in experts-exchange knowledge base. I'm executing a batch script for changing power scheme profile of users. I launch powercfg.exe command with some parameters.

This solution is worse that make it with SDK API, but I didn´t find other solution.

Thank you.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.