0

i need to find away to turn on the pc from c++ application , is there any way to do this? Thanks

user63898
  • 29,839
  • 85
  • 272
  • 514

7 Answers7

5

If the computer is off, it can't be executing code, and therefore can't turn itself on programmatically.

ACPI changes that somewhat, but for us to be able to help, you have to be more specific about your exact requirements.

If you need to turn on a different computer, take a look at Wake-on-LAN.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • That is only partially true, since (I'd guess) 2002 most PCs have ACPI, which allows hardware timers to be set. Even if your computer is 'off', the mainboard actually pulls a small amount of power to power the network card, listen for keyboard input (wake on lan, wake on keyboard, etc.), or wait for a timer. Unfortunately my experience is that this feature was not very reliable when I tried it, but I hope that might have changed by now. – jdm Sep 22 '11 at 13:02
  • I have a timer configured in my PC's BIOS to turn the PC on every weekday at the same time, and it works fine and reliably. – Remy Lebeau Sep 22 '11 at 20:13
3

You will not be able to write a program to turn a computer on that the program itself is installed on.

If you need to write an application that will turn on a different computer, Wake-on-LAN is the tool for you. Modern desktops have NICs that is always receiving power - even if the computer is in an S5 state. Assuming the BIOS supports it and it is enabled.

Wake-On-LAN works by sending a Magic Packet to the NIC. The details of what the payload consists of is outlined in the article.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
2

This is possibly a duplicate of C#: How to wake up system which has been shutdown? (although that is C#).

One way to do it under windows is to create a timer with CreateWaitableTimer(), set the time with SetWaitableTimer() and then do a WaitForSingleObject(). Your code will pause, and you can put the computer into standby (maybe also hibernation, but not shutdown). When the timer is reached, the PC will resume and so will your program.

See here for a complete example in C. The example shows how to calculate the time difference for the timer, and how to do the waiting in a thread (if you are writing a graphical application).

I have to add, you can also schedule the computer to wake up using the Windows Task Scheduler ('Wake the computer to run this task'). This possibly also works when the computer is shut down. There is also an option in some computers BIOS to set a wake time.

Under Linux, you can set the computer to wake up by writing to a special file:

echo 2006-02-09 23:05:00 > /proc/acpi/alarm

Note that I haven't tested all of this, and it is highly dependent on the hardware (mainboard), but some kind of wake-up should be available on all modern PCs.

See also: http://en.wikipedia.org/wiki/Real-time_clock_alarm , and here is a program that claims to do it on windows: http://www.dennisbabkin.com/wosb/

Community
  • 1
  • 1
jdm
  • 9,470
  • 12
  • 58
  • 110
1

Use strip. If you require a Windows computer to be turned on, the cross-tools i686-w64-mingw32-strip or x86_64-w64-mingw32-strip should be used. These command-line programs modify an executable, and the result is able to turn on a computer.

thiton
  • 35,651
  • 4
  • 70
  • 100
1

How could you turn on a computer from an application, when no processes are running on it when it's shut down ? You can turn on another computer (Wake on Lan), but not the one you are running.

Marc Plano-Lesay
  • 6,808
  • 10
  • 44
  • 75
0

It is possible.

First thing to do is configure Wake On Lan. Check out this post on Lifehacker on how to do it: http://lifehacker.com/348197/access-your-computer-anytime-and-save-energy-with-wake+on+lan.

(Or this link: http://hblg.info/2011/08/21/Wake-on-LAN-and-remote-login.html)

Then you need to send a magic packet from your C++ application. There are several web services that already do this from javascript (wakeonlan.me) , but it can be done from within a C++ application as well.

mihai
  • 37,072
  • 9
  • 60
  • 86
0

Chances are, that if you want to do this, you are working with servers. In such case, your mainboard may should an IMPI baseboard management controller. IPMI may be used to cycle the chassis power remotely. Generally, the BMC will have its own IP address, to which you may connect to send control messages.

iosif
  • 1