9

是否有任何 Win32 API 用于在特定时间唤醒已关闭的系统?我见过一个名为 Autopower On 的程序,它能够在指定的时间打开系统电源。

4

4 回答 4

11

从一个网站得到以下帖子。有没有人试过这个?

框架中没有任何内容,因此您必须稍微“PInvoke”。您需要调用的 API 是 CreateWaitableTimer 和 SetWaitableTimer。以下是一个完整的 (Q&D) 示例,说明了如何使用上述 Win32 API 将系统设置为从睡眠/休眠中唤醒。请注意,这里我设置了 300000000 nSecs 的相对唤醒时间。这意味着计算机将在设置定时器后的 30 秒内唤醒(假设他正在睡觉或休眠)。有关 SetWaitableTimer 及其参数的详细信息,请参阅 MSDN 文档。

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");
        }
    }
}
于 2010-10-31T04:50:32.497 回答
2

这个LAN 唤醒 (WOL)代码项目帖子可能有用(主板和 NIC 都必须支持 WOL)

于 2010-10-31T04:32:13.260 回答
2

一旦您真正关闭了计算机(不是睡眠或休眠),您就无法直接从 C#、C++ 代码中将其唤醒。毕竟操作系统本身是关闭的。

唯一的机会是您的主板支持某种计时器机制。并且通过一些 C++ 函数能够将一些标志写入 BIOS 并设置该计时器。

于 2010-11-01T13:38:40.513 回答
0

使用 Windows 的“计划任务”菜单。看:

http://www.howtogeek.com/119028/how-to-make-your-pc-wake-from-sleep-automatically/

您创建一个要在唤醒时运行的程序,然后在特定时间后第一次将其手动输入到 Windows 计划任务列表中以唤醒。

当您的程序唤醒时,我不确定您是否需要您的程序调用 Windows API 以阻止它再次被唤醒,如果您希望您的程序在启动另一个计划任务后关闭 PC 以再次唤醒它,则同上。需要为此研究有哪些 Windows API。

于 2014-11-23T00:30:57.060 回答