Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Is there any Win32 API for waking up a system that has been shut down, at a specific time? I have seen a program named Autopower On which is able to power the system on at a specified time.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
597 views
Welcome To Ask or Share your Answers For Others

1 Answer

Got the below post from a site. Any body tried this?

Nothing in the framework, so you'll have to "PInvoke" a bit. The API's you need to call are CreateWaitableTimer and SetWaitableTimer. Following is a complete (Q&D) sample that illustrates how you can set a system to be awoken from sleep/hibernate using above Win32 API's. Note that here I'm setting a relative wakeup time of 300000000 nSecs. That means that the computer will wake-up (supposing he's asleep or hibernating) within 30 seconds after setting the timer. Consult the MSDN docs for details on SetWaitableTimer and it's arguments.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Willys
{
    class Program
    {
        [DllImport("kernel32.dll")]
        public static extern IntPtr CreateWaitableTimer(IntPtr lpTimerAttributes,
        bool bManualReset, string lpTimerName);

        [DllImport("kernel32.dll")]
        public static extern bool SetWaitableTimer(IntPtr hTimer, [In] ref long
        pDueTime, int lPeriod, IntPtr pfnCompletionRoutine, IntPtr
        lpArgToCompletionRoutine, bool fResume);

        [DllImport("kernel32", SetLastError = true, ExactSpelling = true)]
        public static extern Int32 WaitForSingleObject(IntPtr handle, uint
        milliseconds);

        static void Main()
        {
            SetWaitForWakeUpTime();
        }

        static IntPtr handle;
        static void SetWaitForWakeUpTime()
        {
            long duetime = -300000000; // negative value, so a RELATIVE due time
            Console.WriteLine("{0:x}", duetime);
            handle = CreateWaitableTimer(IntPtr.Zero, true, "MyWaitabletimer");
            SetWaitableTimer(handle, ref duetime, 0, IntPtr.Zero, IntPtr.Zero, true);
            uint INFINITE = 0xFFFFFFFF;
            int ret = WaitForSingleObject(handle, INFINITE);
            MessageBox.Show("Wake up call");
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...